mirror of https://github.com/python/cpython
Issue #12983: Bytes literals with invalid \x escape now raise a SyntaxError
and a full traceback including line number.
This commit is contained in:
commit
801d955f04
|
@ -50,6 +50,10 @@ f = '\u1881'
|
||||||
assert ord(f) == 0x1881
|
assert ord(f) == 0x1881
|
||||||
g = r'\u1881'
|
g = r'\u1881'
|
||||||
assert list(map(ord, g)) == [92, 117, 49, 56, 56, 49]
|
assert list(map(ord, g)) == [92, 117, 49, 56, 56, 49]
|
||||||
|
h = '\U0001d120'
|
||||||
|
assert ord(h) == 0x1d120
|
||||||
|
i = r'\U0001d120'
|
||||||
|
assert list(map(ord, i)) == [92, 85, 48, 48, 48, 49, 100, 49, 50, 48]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -82,6 +86,24 @@ class TestLiterals(unittest.TestCase):
|
||||||
self.assertEqual(eval(""" '\x81' """), chr(0x81))
|
self.assertEqual(eval(""" '\x81' """), chr(0x81))
|
||||||
self.assertEqual(eval(r""" '\u1881' """), chr(0x1881))
|
self.assertEqual(eval(r""" '\u1881' """), chr(0x1881))
|
||||||
self.assertEqual(eval(""" '\u1881' """), chr(0x1881))
|
self.assertEqual(eval(""" '\u1881' """), chr(0x1881))
|
||||||
|
self.assertEqual(eval(r""" '\U0001d120' """), chr(0x1d120))
|
||||||
|
self.assertEqual(eval(""" '\U0001d120' """), chr(0x1d120))
|
||||||
|
|
||||||
|
def test_eval_str_incomplete(self):
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\x' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\x0' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\u' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\u0' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\u00' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\u000' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U0' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U00' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U000' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U0000' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U00000' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U000000' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" '\U0000000' """)
|
||||||
|
|
||||||
def test_eval_str_raw(self):
|
def test_eval_str_raw(self):
|
||||||
self.assertEqual(eval(""" r'x' """), 'x')
|
self.assertEqual(eval(""" r'x' """), 'x')
|
||||||
|
@ -91,6 +113,8 @@ class TestLiterals(unittest.TestCase):
|
||||||
self.assertEqual(eval(""" r'\x81' """), chr(0x81))
|
self.assertEqual(eval(""" r'\x81' """), chr(0x81))
|
||||||
self.assertEqual(eval(r""" r'\u1881' """), '\\' + 'u1881')
|
self.assertEqual(eval(r""" r'\u1881' """), '\\' + 'u1881')
|
||||||
self.assertEqual(eval(""" r'\u1881' """), chr(0x1881))
|
self.assertEqual(eval(""" r'\u1881' """), chr(0x1881))
|
||||||
|
self.assertEqual(eval(r""" r'\U0001d120' """), '\\' + 'U0001d120')
|
||||||
|
self.assertEqual(eval(""" r'\U0001d120' """), chr(0x1d120))
|
||||||
|
|
||||||
def test_eval_bytes_normal(self):
|
def test_eval_bytes_normal(self):
|
||||||
self.assertEqual(eval(""" b'x' """), b'x')
|
self.assertEqual(eval(""" b'x' """), b'x')
|
||||||
|
@ -100,6 +124,12 @@ class TestLiterals(unittest.TestCase):
|
||||||
self.assertRaises(SyntaxError, eval, """ b'\x81' """)
|
self.assertRaises(SyntaxError, eval, """ b'\x81' """)
|
||||||
self.assertEqual(eval(r""" b'\u1881' """), b'\\' + b'u1881')
|
self.assertEqual(eval(r""" b'\u1881' """), b'\\' + b'u1881')
|
||||||
self.assertRaises(SyntaxError, eval, """ b'\u1881' """)
|
self.assertRaises(SyntaxError, eval, """ b'\u1881' """)
|
||||||
|
self.assertEqual(eval(r""" b'\U0001d120' """), b'\\' + b'U0001d120')
|
||||||
|
self.assertRaises(SyntaxError, eval, """ b'\U0001d120' """)
|
||||||
|
|
||||||
|
def test_eval_bytes_incomplete(self):
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" b'\x' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, r""" b'\x0' """)
|
||||||
|
|
||||||
def test_eval_bytes_raw(self):
|
def test_eval_bytes_raw(self):
|
||||||
self.assertEqual(eval(""" br'x' """), b'x')
|
self.assertEqual(eval(""" br'x' """), b'x')
|
||||||
|
@ -116,6 +146,10 @@ class TestLiterals(unittest.TestCase):
|
||||||
self.assertEqual(eval(r""" rb'\u1881' """), b"\\" + b"u1881")
|
self.assertEqual(eval(r""" rb'\u1881' """), b"\\" + b"u1881")
|
||||||
self.assertRaises(SyntaxError, eval, """ br'\u1881' """)
|
self.assertRaises(SyntaxError, eval, """ br'\u1881' """)
|
||||||
self.assertRaises(SyntaxError, eval, """ rb'\u1881' """)
|
self.assertRaises(SyntaxError, eval, """ rb'\u1881' """)
|
||||||
|
self.assertEqual(eval(r""" br'\U0001d120' """), b"\\" + b"U0001d120")
|
||||||
|
self.assertEqual(eval(r""" rb'\U0001d120' """), b"\\" + b"U0001d120")
|
||||||
|
self.assertRaises(SyntaxError, eval, """ br'\U0001d120' """)
|
||||||
|
self.assertRaises(SyntaxError, eval, """ rb'\U0001d120' """)
|
||||||
self.assertRaises(SyntaxError, eval, """ bb'' """)
|
self.assertRaises(SyntaxError, eval, """ bb'' """)
|
||||||
self.assertRaises(SyntaxError, eval, """ rr'' """)
|
self.assertRaises(SyntaxError, eval, """ rr'' """)
|
||||||
self.assertRaises(SyntaxError, eval, """ brr'' """)
|
self.assertRaises(SyntaxError, eval, """ brr'' """)
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 3.3.1?
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #12983: Bytes literals with invalid \x escape now raise a SyntaxError
|
||||||
|
and a full traceback including line number.
|
||||||
|
|
||||||
- Issue #17173: Remove uses of locale-dependent C functions (isalpha() etc.)
|
- Issue #17173: Remove uses of locale-dependent C functions (isalpha() etc.)
|
||||||
in the interpreter.
|
in the interpreter.
|
||||||
|
|
||||||
|
|
|
@ -465,8 +465,9 @@ PyObject *PyBytes_DecodeEscape(const char *s,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!errors || strcmp(errors, "strict") == 0) {
|
if (!errors || strcmp(errors, "strict") == 0) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_Format(PyExc_ValueError,
|
||||||
"invalid \\x escape");
|
"invalid \\x escape at position %d",
|
||||||
|
s - 2 - (end - len));
|
||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
if (strcmp(errors, "replace") == 0) {
|
if (strcmp(errors, "replace") == 0) {
|
||||||
|
|
18
Python/ast.c
18
Python/ast.c
|
@ -1829,20 +1829,24 @@ ast_for_atom(struct compiling *c, const node *n)
|
||||||
case STRING: {
|
case STRING: {
|
||||||
PyObject *str = parsestrplus(c, n, &bytesmode);
|
PyObject *str = parsestrplus(c, n, &bytesmode);
|
||||||
if (!str) {
|
if (!str) {
|
||||||
if (PyErr_ExceptionMatches(PyExc_UnicodeError)) {
|
const char *errtype = NULL;
|
||||||
|
if (PyErr_ExceptionMatches(PyExc_UnicodeError))
|
||||||
|
errtype = "unicode error";
|
||||||
|
else if (PyErr_ExceptionMatches(PyExc_ValueError))
|
||||||
|
errtype = "value error";
|
||||||
|
if (errtype) {
|
||||||
|
char buf[128];
|
||||||
PyObject *type, *value, *tback, *errstr;
|
PyObject *type, *value, *tback, *errstr;
|
||||||
PyErr_Fetch(&type, &value, &tback);
|
PyErr_Fetch(&type, &value, &tback);
|
||||||
errstr = PyObject_Str(value);
|
errstr = PyObject_Str(value);
|
||||||
if (errstr) {
|
if (errstr) {
|
||||||
char *s = "";
|
char *s = _PyUnicode_AsString(errstr);
|
||||||
char buf[128];
|
PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
|
||||||
s = _PyUnicode_AsString(errstr);
|
|
||||||
PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
|
|
||||||
ast_error(c, n, buf);
|
|
||||||
Py_DECREF(errstr);
|
Py_DECREF(errstr);
|
||||||
} else {
|
} else {
|
||||||
ast_error(c, n, "(unicode error) unknown error");
|
PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
|
||||||
}
|
}
|
||||||
|
ast_error(c, n, buf);
|
||||||
Py_DECREF(type);
|
Py_DECREF(type);
|
||||||
Py_DECREF(value);
|
Py_DECREF(value);
|
||||||
Py_XDECREF(tback);
|
Py_XDECREF(tback);
|
||||||
|
|
Loading…
Reference in New Issue