]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #28350: String constants with null character no longer interned.
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 4 Oct 2016 15:17:22 +0000 (18:17 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 4 Oct 2016 15:17:22 +0000 (18:17 +0300)
Lib/test/test_code.py
Misc/NEWS
Objects/codeobject.c

index 2be17a2a850b11a1c916156edc52d2cb9e7e87d8..7975ea0ef5e17d01b1e1f2c01982d22a32a52074 100644 (file)
@@ -135,19 +135,27 @@ class CodeTest(unittest.TestCase):
         self.assertEqual(co.co_name, "funcname")
         self.assertEqual(co.co_firstlineno, 15)
 
+
+def isinterned(s):
+    return s is sys.intern(('_' + s + '_')[1:-1])
+
 class CodeConstsTest(unittest.TestCase):
 
     def find_const(self, consts, value):
         for v in consts:
             if v == value:
                 return v
-        self.assertIn(value, consts)  # rises an exception
-        self.fail('Should be never reached')
+        self.assertIn(value, consts)  # raises an exception
+        self.fail('Should never be reached')
 
     def assertIsInterned(self, s):
-        if s is not sys.intern(s):
+        if not isinterned(s):
             self.fail('String %r is not interned' % (s,))
 
+    def assertIsNotInterned(self, s):
+        if isinterned(s):
+            self.fail('String %r is interned' % (s,))
+
     @cpython_only
     def test_interned_string(self):
         co = compile('res = "str_value"', '?', 'exec')
@@ -172,6 +180,12 @@ class CodeConstsTest(unittest.TestCase):
             return a
         self.assertIsInterned(f())
 
+    @cpython_only
+    def test_interned_string_with_null(self):
+        co = compile(r'res = "str\0value!"', '?', 'exec')
+        v = self.find_const(co.co_consts, 'str\0value!')
+        self.assertIsNotInterned(v)
+
 
 class CodeWeakRefTest(unittest.TestCase):
 
index e87ed71e7a05d18f8fbddc3e6f6b246ff40916ba..6fa7fd4fbdaaa9e4e64dd4191202e98de31dea81 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Release date: TBA
 Core and Builtins
 -----------------
 
+- Issue #28350: String constants with null character no longer interned.
+
 - Issue #27942: String constants now interned recursively in tuples and frozensets.
 
 - Issue #21578: Fixed misleading error message when ImportError called with
index e50f7306efb3b9610c18dfa8c1ba297154c428b6..9df87b04f8510dd4bb84ca330d44c3d42daf8c86 100644 (file)
@@ -11,21 +11,21 @@ static int
 all_name_chars(PyObject *o)
 {
     static char ok_name_char[256];
-    static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
-    PyUnicodeObject *u = (PyUnicodeObject *)o;
-    const unsigned char *s;
+    static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
+    const unsigned char *s, *e;
 
-    if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
-        PyUnicode_MAX_CHAR_VALUE(u) >= 128)
+    if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
+        !PyUnicode_IS_ASCII(o))
         return 0;
 
     if (ok_name_char[*name_chars] == 0) {
-        unsigned char *p;
+        const unsigned char *p;
         for (p = name_chars; *p; p++)
             ok_name_char[*p] = 1;
     }
-    s = PyUnicode_1BYTE_DATA(u);
-    while (*s) {
+    s = PyUnicode_1BYTE_DATA(o);
+    e = s + PyUnicode_GET_LENGTH(o);
+    while (s != e) {
         if (ok_name_char[*s++] == 0)
             return 0;
     }