Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 801d955

Browse files
Issue #12983: Bytes literals with invalid \x escape now raise a SyntaxError
and a full traceback including line number.
2 parents 6427358 + 5e61f14 commit 801d955

4 files changed

Lines changed: 51 additions & 9 deletions

File tree

Lib/test/test_strlit.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
assert ord(f) == 0x1881
5151
g = r'\u1881'
5252
assert list(map(ord, g)) == [92, 117, 49, 56, 56, 49]
53+
h = '\U0001d120'
54+
assert ord(h) == 0x1d120
55+
i = r'\U0001d120'
56+
assert list(map(ord, i)) == [92, 85, 48, 48, 48, 49, 100, 49, 50, 48]
5357
"""
5458

5559

@@ -82,6 +86,24 @@ def test_eval_str_normal(self):
8286
self.assertEqual(eval(""" '\x81' """), chr(0x81))
8387
self.assertEqual(eval(r""" '\u1881' """), chr(0x1881))
8488
self.assertEqual(eval(""" '\u1881' """), chr(0x1881))
89+
self.assertEqual(eval(r""" '\U0001d120' """), chr(0x1d120))
90+
self.assertEqual(eval(""" '\U0001d120' """), chr(0x1d120))
91+
92+
def test_eval_str_incomplete(self):
93+
self.assertRaises(SyntaxError, eval, r""" '\x' """)
94+
self.assertRaises(SyntaxError, eval, r""" '\x0' """)
95+
self.assertRaises(SyntaxError, eval, r""" '\u' """)
96+
self.assertRaises(SyntaxError, eval, r""" '\u0' """)
97+
self.assertRaises(SyntaxError, eval, r""" '\u00' """)
98+
self.assertRaises(SyntaxError, eval, r""" '\u000' """)
99+
self.assertRaises(SyntaxError, eval, r""" '\U' """)
100+
self.assertRaises(SyntaxError, eval, r""" '\U0' """)
101+
self.assertRaises(SyntaxError, eval, r""" '\U00' """)
102+
self.assertRaises(SyntaxError, eval, r""" '\U000' """)
103+
self.assertRaises(SyntaxError, eval, r""" '\U0000' """)
104+
self.assertRaises(SyntaxError, eval, r""" '\U00000' """)
105+
self.assertRaises(SyntaxError, eval, r""" '\U000000' """)
106+
self.assertRaises(SyntaxError, eval, r""" '\U0000000' """)
85107

86108
def test_eval_str_raw(self):
87109
self.assertEqual(eval(""" r'x' """), 'x')
@@ -91,6 +113,8 @@ def test_eval_str_raw(self):
91113
self.assertEqual(eval(""" r'\x81' """), chr(0x81))
92114
self.assertEqual(eval(r""" r'\u1881' """), '\\' + 'u1881')
93115
self.assertEqual(eval(""" r'\u1881' """), chr(0x1881))
116+
self.assertEqual(eval(r""" r'\U0001d120' """), '\\' + 'U0001d120')
117+
self.assertEqual(eval(""" r'\U0001d120' """), chr(0x1d120))
94118

95119
def test_eval_bytes_normal(self):
96120
self.assertEqual(eval(""" b'x' """), b'x')
@@ -100,6 +124,12 @@ def test_eval_bytes_normal(self):
100124
self.assertRaises(SyntaxError, eval, """ b'\x81' """)
101125
self.assertEqual(eval(r""" b'\u1881' """), b'\\' + b'u1881')
102126
self.assertRaises(SyntaxError, eval, """ b'\u1881' """)
127+
self.assertEqual(eval(r""" b'\U0001d120' """), b'\\' + b'U0001d120')
128+
self.assertRaises(SyntaxError, eval, """ b'\U0001d120' """)
129+
130+
def test_eval_bytes_incomplete(self):
131+
self.assertRaises(SyntaxError, eval, r""" b'\x' """)
132+
self.assertRaises(SyntaxError, eval, r""" b'\x0' """)
103133

104134
def test_eval_bytes_raw(self):
105135
self.assertEqual(eval(""" br'x' """), b'x')
@@ -116,6 +146,10 @@ def test_eval_bytes_raw(self):
116146
self.assertEqual(eval(r""" rb'\u1881' """), b"\\" + b"u1881")
117147
self.assertRaises(SyntaxError, eval, """ br'\u1881' """)
118148
self.assertRaises(SyntaxError, eval, """ rb'\u1881' """)
149+
self.assertEqual(eval(r""" br'\U0001d120' """), b"\\" + b"U0001d120")
150+
self.assertEqual(eval(r""" rb'\U0001d120' """), b"\\" + b"U0001d120")
151+
self.assertRaises(SyntaxError, eval, """ br'\U0001d120' """)
152+
self.assertRaises(SyntaxError, eval, """ rb'\U0001d120' """)
119153
self.assertRaises(SyntaxError, eval, """ bb'' """)
120154
self.assertRaises(SyntaxError, eval, """ rr'' """)
121155
self.assertRaises(SyntaxError, eval, """ brr'' """)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #12983: Bytes literals with invalid \x escape now raise a SyntaxError
16+
and a full traceback including line number.
17+
1518
- Issue #17173: Remove uses of locale-dependent C functions (isalpha() etc.)
1619
in the interpreter.
1720

Objects/bytesobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ PyObject *PyBytes_DecodeEscape(const char *s,
465465
break;
466466
}
467467
if (!errors || strcmp(errors, "strict") == 0) {
468-
PyErr_SetString(PyExc_ValueError,
469-
"invalid \\x escape");
468+
PyErr_Format(PyExc_ValueError,
469+
"invalid \\x escape at position %d",
470+
s - 2 - (end - len));
470471
goto failed;
471472
}
472473
if (strcmp(errors, "replace") == 0) {

Python/ast.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,20 +1829,24 @@ ast_for_atom(struct compiling *c, const node *n)
18291829
case STRING: {
18301830
PyObject *str = parsestrplus(c, n, &bytesmode);
18311831
if (!str) {
1832-
if (PyErr_ExceptionMatches(PyExc_UnicodeError)) {
1832+
const char *errtype = NULL;
1833+
if (PyErr_ExceptionMatches(PyExc_UnicodeError))
1834+
errtype = "unicode error";
1835+
else if (PyErr_ExceptionMatches(PyExc_ValueError))
1836+
errtype = "value error";
1837+
if (errtype) {
1838+
char buf[128];
18331839
PyObject *type, *value, *tback, *errstr;
18341840
PyErr_Fetch(&type, &value, &tback);
18351841
errstr = PyObject_Str(value);
18361842
if (errstr) {
1837-
char *s = "";
1838-
char buf[128];
1839-
s = _PyUnicode_AsString(errstr);
1840-
PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1841-
ast_error(c, n, buf);
1843+
char *s = _PyUnicode_AsString(errstr);
1844+
PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
18421845
Py_DECREF(errstr);
18431846
} else {
1844-
ast_error(c, n, "(unicode error) unknown error");
1847+
PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
18451848
}
1849+
ast_error(c, n, buf);
18461850
Py_DECREF(type);
18471851
Py_DECREF(value);
18481852
Py_XDECREF(tback);

0 commit comments

Comments
 (0)