Skip to content

Commit e81b0d3

Browse files
Issue #28350: String constants with null character no longer interned.
2 parents 8044809 + 09f3d08 commit e81b0d3

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

Lib/test/test_code.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,27 @@ def test_newempty(self):
135135
self.assertEqual(co.co_name, "funcname")
136136
self.assertEqual(co.co_firstlineno, 15)
137137

138+
139+
def isinterned(s):
140+
return s is sys.intern(('_' + s + '_')[1:-1])
141+
138142
class CodeConstsTest(unittest.TestCase):
139143

140144
def find_const(self, consts, value):
141145
for v in consts:
142146
if v == value:
143147
return v
144-
self.assertIn(value, consts) # rises an exception
145-
self.fail('Should be never reached')
148+
self.assertIn(value, consts) # raises an exception
149+
self.fail('Should never be reached')
146150

147151
def assertIsInterned(self, s):
148-
if s is not sys.intern(s):
152+
if not isinterned(s):
149153
self.fail('String %r is not interned' % (s,))
150154

155+
def assertIsNotInterned(self, s):
156+
if isinterned(s):
157+
self.fail('String %r is interned' % (s,))
158+
151159
@cpython_only
152160
def test_interned_string(self):
153161
co = compile('res = "str_value"', '?', 'exec')
@@ -172,6 +180,12 @@ def f(a='str_value'):
172180
return a
173181
self.assertIsInterned(f())
174182

183+
@cpython_only
184+
def test_interned_string_with_null(self):
185+
co = compile(r'res = "str\0value!"', '?', 'exec')
186+
v = self.find_const(co.co_consts, 'str\0value!')
187+
self.assertIsNotInterned(v)
188+
175189

176190
class CodeWeakRefTest(unittest.TestCase):
177191

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.6.0 beta 2
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #28350: String constants with null character no longer interned.
14+
1315
- Issue #26617: Fix crash when GC runs during weakref callbacks.
1416

1517
- Issue #27942: String constants now interned recursively in tuples and frozensets.

Objects/codeobject.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ static int
1919
all_name_chars(PyObject *o)
2020
{
2121
static char ok_name_char[256];
22-
static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
23-
PyUnicodeObject *u = (PyUnicodeObject *)o;
24-
const unsigned char *s;
22+
static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
23+
const unsigned char *s, *e;
2524

26-
if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
27-
PyUnicode_MAX_CHAR_VALUE(u) >= 128)
25+
if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
26+
!PyUnicode_IS_ASCII(o))
2827
return 0;
2928

3029
if (ok_name_char[*name_chars] == 0) {
31-
unsigned char *p;
30+
const unsigned char *p;
3231
for (p = name_chars; *p; p++)
3332
ok_name_char[*p] = 1;
3433
}
35-
s = PyUnicode_1BYTE_DATA(u);
36-
while (*s) {
34+
s = PyUnicode_1BYTE_DATA(o);
35+
e = s + PyUnicode_GET_LENGTH(o);
36+
while (s != e) {
3737
if (ok_name_char[*s++] == 0)
3838
return 0;
3939
}

0 commit comments

Comments
 (0)