Skip to content

Commit 1e78ed6

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
bpo-31825: Fixed OverflowError in the 'unicode-escape' codec (GH-4058) (#4059)
and in codecs.escape_decode() when decode an escaped non-ascii byte. (cherry picked from commit 56cb465)
1 parent 9c23b17 commit 1e78ed6

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
@@ -1255,7 +1255,7 @@ PyObject *PyBytes_DecodeEscape(const char *s,
12551255
if (first_invalid_escape != NULL) {
12561256
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
12571257
"invalid escape sequence '\\%c'",
1258-
*first_invalid_escape) < 0) {
1258+
(unsigned char)*first_invalid_escape) < 0) {
12591259
Py_DECREF(result);
12601260
return NULL;
12611261
}

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6174,7 +6174,7 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
61746174
if (first_invalid_escape != NULL) {
61756175
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
61766176
"invalid escape sequence '\\%c'",
6177-
*first_invalid_escape) < 0) {
6177+
(unsigned char)*first_invalid_escape) < 0) {
61786178
Py_DECREF(result);
61796179
return NULL;
61806180
}

Python/ast.c

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

41284128
static int
41294129
warn_invalid_escape_sequence(struct compiling *c, const node *n,
4130-
char first_invalid_escape_char)
4130+
unsigned char first_invalid_escape_char)
41314131
{
41324132
PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
41334133
first_invalid_escape_char);

0 commit comments

Comments
 (0)