cpython: 305210a08fc9 (original) (raw)

--- a/Lib/test/test_strlit.py +++ b/Lib/test/test_strlit.py @@ -50,6 +50,10 @@ f = '\u1881' assert ord(f) == 0x1881 g = r'\u1881' 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(r""" '\u1881' """), chr(0x1881)) self.assertEqual(eval(""" '\u1881' """), chr(0x1881))

+

def test_eval_str_raw(self): 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""" r'\u1881' """), '\' + 'u1881') self.assertEqual(eval(""" r'\u1881' """), chr(0x1881))

def test_eval_bytes_normal(self): self.assertEqual(eval(""" b'x' """), b'x') @@ -100,6 +124,12 @@ class TestLiterals(unittest.TestCase): self.assertRaises(SyntaxError, eval, """ b'\x81' """) self.assertEqual(eval(r""" b'\u1881' """), b'\' + b'u1881') self.assertRaises(SyntaxError, eval, """ b'\u1881' """)

+

def test_eval_bytes_raw(self): self.assertEqual(eval(""" br'x' """), b'x') @@ -109,6 +139,12 @@ class TestLiterals(unittest.TestCase): self.assertRaises(SyntaxError, eval, """ br'\x81' """) self.assertEqual(eval(r""" br'\u1881' """), b"\" + b"u1881") self.assertRaises(SyntaxError, eval, """ br'\u1881' """)

def check_encoding(self, encoding, extra=""): modname = "xx_" + encoding.replace("-", "_")

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.2.4 Core and Builtins ----------------- +- Issue #12983: Bytes literals with invalid \x escape now raise a SyntaxError

--- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -469,8 +469,9 @@ PyObject *PyBytes_DecodeEscape(const cha break; } if (!errors || strcmp(errors, "strict") == 0) {

--- a/Python/ast.c +++ b/Python/ast.c @@ -1368,20 +1368,24 @@ ast_for_atom(struct compiling *c, const case STRING: { PyObject *str = parsestrplus(c, n, &bytesmode); if (!str) {