Skip to content

Commit 09f3d08

Browse files
Issue #28350: String constants with null character no longer interned.
1 parent b47c9d2 commit 09f3d08

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

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 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #28350: String constants with null character no longer interned.
14+
1315
- Issue #27942: String constants now interned recursively in tuples and frozensets.
1416

1517
- Issue #21578: Fixed misleading error message when ImportError called with

Objects/codeobject.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ static int
1111
all_name_chars(PyObject *o)
1212
{
1313
static char ok_name_char[256];
14-
static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
15-
PyUnicodeObject *u = (PyUnicodeObject *)o;
16-
const unsigned char *s;
14+
static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
15+
const unsigned char *s, *e;
1716

18-
if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
19-
PyUnicode_MAX_CHAR_VALUE(u) >= 128)
17+
if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
18+
!PyUnicode_IS_ASCII(o))
2019
return 0;
2120

2221
if (ok_name_char[*name_chars] == 0) {
23-
unsigned char *p;
22+
const unsigned char *p;
2423
for (p = name_chars; *p; p++)
2524
ok_name_char[*p] = 1;
2625
}
27-
s = PyUnicode_1BYTE_DATA(u);
28-
while (*s) {
26+
s = PyUnicode_1BYTE_DATA(o);
27+
e = s + PyUnicode_GET_LENGTH(o);
28+
while (s != e) {
2929
if (ok_name_char[*s++] == 0)
3030
return 0;
3131
}

0 commit comments

Comments
 (0)