Commit 548fd537 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #28350: String constants with null character no longer interned.

parent 94cc5633
...@@ -135,19 +135,27 @@ class CodeTest(unittest.TestCase): ...@@ -135,19 +135,27 @@ class CodeTest(unittest.TestCase):
self.assertEqual(co.co_name, "funcname") self.assertEqual(co.co_name, "funcname")
self.assertEqual(co.co_firstlineno, 15) self.assertEqual(co.co_firstlineno, 15)
def isinterned(s):
return s is sys.intern(('_' + s + '_')[1:-1])
class CodeConstsTest(unittest.TestCase): class CodeConstsTest(unittest.TestCase):
def find_const(self, consts, value): def find_const(self, consts, value):
for v in consts: for v in consts:
if v == value: if v == value:
return v return v
self.assertIn(value, consts) # rises an exception self.assertIn(value, consts) # raises an exception
self.fail('Should be never reached') self.fail('Should never be reached')
def assertIsInterned(self, s): def assertIsInterned(self, s):
if s is not sys.intern(s): if not isinterned(s):
self.fail('String %r is not interned' % (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 @cpython_only
def test_interned_string(self): def test_interned_string(self):
co = compile('res = "str_value"', '?', 'exec') co = compile('res = "str_value"', '?', 'exec')
...@@ -172,6 +180,12 @@ class CodeConstsTest(unittest.TestCase): ...@@ -172,6 +180,12 @@ class CodeConstsTest(unittest.TestCase):
return a return a
self.assertIsInterned(f()) 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): class CodeWeakRefTest(unittest.TestCase):
......
...@@ -10,6 +10,8 @@ Release date: TBA ...@@ -10,6 +10,8 @@ Release date: TBA
Core and Builtins 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 #27942: String constants now interned recursively in tuples and frozensets.
- Issue #21578: Fixed misleading error message when ImportError called with - Issue #21578: Fixed misleading error message when ImportError called with
......
...@@ -11,21 +11,21 @@ static int ...@@ -11,21 +11,21 @@ static int
all_name_chars(PyObject *o) all_name_chars(PyObject *o)
{ {
static char ok_name_char[256]; 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;
PyUnicodeObject *u = (PyUnicodeObject *)o; const unsigned char *s, *e;
const unsigned char *s;
if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 || if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
PyUnicode_MAX_CHAR_VALUE(u) >= 128) !PyUnicode_IS_ASCII(o))
return 0; return 0;
if (ok_name_char[*name_chars] == 0) { if (ok_name_char[*name_chars] == 0) {
unsigned char *p; const unsigned char *p;
for (p = name_chars; *p; p++) for (p = name_chars; *p; p++)
ok_name_char[*p] = 1; ok_name_char[*p] = 1;
} }
s = PyUnicode_1BYTE_DATA(u); s = PyUnicode_1BYTE_DATA(o);
while (*s) { e = s + PyUnicode_GET_LENGTH(o);
while (s != e) {
if (ok_name_char[*s++] == 0) if (ok_name_char[*s++] == 0)
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment