Skip to content

Commit 56cb465

Browse files
bpo-31825: Fixed OverflowError in the 'unicode-escape' codec (#4058)
and in codecs.escape_decode() when decode an escaped non-ascii byte.
1 parent 525f40d commit 56cb465

File tree

5 files changed

+9
-3
lines changed

5 files changed

+9
-3
lines changed

‎Lib/test/test_codecs.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,8 @@ def test_escape(self):
12031203
check(br"\8", b"\\8")
12041204
with self.assertWarns(DeprecationWarning):
12051205
check(br"\9", b"\\9")
1206+
with self.assertWarns(DeprecationWarning):
1207+
check(b"\\\xfa", b"\\\xfa")
12061208

12071209
def test_errors(self):
12081210
decode = codecs.escape_decode
@@ -2474,6 +2476,8 @@ def test_escape_decode(self):
24742476
check(br"\8", "\\8")
24752477
with self.assertWarns(DeprecationWarning):
24762478
check(br"\9", "\\9")
2479+
with self.assertWarns(DeprecationWarning):
2480+
check(b"\\\xfa", "\\\xfa")
24772481

24782482
def test_decode_errors(self):
24792483
decode = codecs.unicode_escape_decode
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed OverflowError in the 'unicode-escape' codec and in
2+
codecs.escape_decode() when decode an escaped non-ascii byte.

‎Objects/bytesobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ PyObject *PyBytes_DecodeEscape(const char *s,
12571257
if (first_invalid_escape != NULL) {
12581258
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
12591259
"invalid escape sequence '\\%c'",
1260-
*first_invalid_escape) < 0) {
1260+
(unsigned char)*first_invalid_escape) < 0) {
12611261
Py_DECREF(result);
12621262
return NULL;
12631263
}

‎Objects/unicodeobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6136,7 +6136,7 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
61366136
if (first_invalid_escape != NULL) {
61376137
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
61386138
"invalid escape sequence '\\%c'",
6139-
*first_invalid_escape) < 0) {
6139+
(unsigned char)*first_invalid_escape) < 0) {
61406140
Py_DECREF(result);
61416141
return NULL;
61426142
}

‎Python/ast.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4147,7 +4147,7 @@ decode_utf8(struct compiling *c, const char **sPtr, const char *end)
41474147

41484148
static int
41494149
warn_invalid_escape_sequence(struct compiling *c, const node *n,
4150-
char first_invalid_escape_char)
4150+
unsigned char first_invalid_escape_char)
41514151
{
41524152
PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
41534153
first_invalid_escape_char);

0 commit comments

Comments
 (0)