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

index 9268bafd98e6ec8d972020c2635f5a9ed18adc40..142261ead26c234e72d949a93d7ecee84bd42078 100644 (file)
@@ -112,19 +112,27 @@ class CodeTest(unittest.TestCase):
         self.assertEqual(co.co_name, "funcname")
         self.assertEqual(co.co_firstlineno, 15)
 
+
+def isinterned(s):
+    return s is 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 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')
@@ -143,6 +151,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 044241920d3b89b96f49bc07abd345c6b0ff000c..fc1e185597647c8c191b2b2863750d5c866ffbea 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 2.7.13?
 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 #15578: Correctly incref the parent module while importing.
index 827fafaf227c7c1370a75adeb204ccfe56eba5f6..b7c4059976e62290af3efaf6d5a345caff422261 100644 (file)
@@ -8,17 +8,20 @@
 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
 
 static int
-all_name_chars(unsigned char *s)
+all_name_chars(PyObject *o)
 {
     static char ok_name_char[256];
-    static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
+    static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
+    const unsigned char *s, *e;
 
     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;
     }
-    while (*s) {
+    s = (unsigned char *)PyString_AS_STRING(o);
+    e = s + PyString_GET_SIZE(o);
+    while (s != e) {
         if (ok_name_char[*s++] == 0)
             return 0;
     }
@@ -49,7 +52,7 @@ intern_string_constants(PyObject *tuple)
     for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
         PyObject *v = PyTuple_GET_ITEM(tuple, i);
         if (PyString_CheckExact(v)) {
-            if (all_name_chars((unsigned char *)PyString_AS_STRING(v))) {
+            if (all_name_chars(v)) {
                 PyObject *w = v;
                 PyString_InternInPlace(&v);
                 if (w != v) {