Skip to content

Commit 67edf73

Browse files
Issue #27942: String constants now interned recursively in tuples and frozensets.
1 parent 8d7fa40 commit 67edf73

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

‎Lib/test/test_code.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ def test_newempty(self):
112112
self.assertEqual(co.co_name, "funcname")
113113
self.assertEqual(co.co_firstlineno, 15)
114114

115+
class CodeConstsTest(unittest.TestCase):
116+
117+
def find_const(self, consts, value):
118+
for v in consts:
119+
if v == value:
120+
return v
121+
self.assertIn(value, consts) # rises an exception
122+
self.fail('Should be never reached')
123+
124+
def assertIsInterned(self, s):
125+
if s is not intern(s):
126+
self.fail('String %r is not interned' % (s,))
127+
128+
@cpython_only
129+
def test_interned_string(self):
130+
co = compile('res = "str_value"', '?', 'exec')
131+
v = self.find_const(co.co_consts, 'str_value')
132+
self.assertIsInterned(v)
133+
134+
@cpython_only
135+
def test_interned_string_in_tuple(self):
136+
co = compile('res = ("str_value",)', '?', 'exec')
137+
v = self.find_const(co.co_consts, ('str_value',))
138+
self.assertIsInterned(v[0])
139+
140+
@cpython_only
141+
def test_interned_string_default(self):
142+
def f(a='str_value'):
143+
return a
144+
self.assertIsInterned(f())
145+
115146

116147
class CodeWeakRefTest(unittest.TestCase):
117148

@@ -141,7 +172,7 @@ def callback(code):
141172
def test_main(verbose=None):
142173
from test import test_code
143174
run_doctest(test_code, verbose)
144-
run_unittest(CodeTest, CodeWeakRefTest)
175+
run_unittest(CodeTest, CodeConstsTest, CodeWeakRefTest)
145176

146177

147178
if __name__ == "__main__":

‎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 2.7.13?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #27942: String constants now interned recursively in tuples and frozensets.
14+
1315
- Issue #15578: Correctly incref the parent module while importing.
1416

1517
- Issue #26307: The profile-opt build now applys PGO to the built-in modules.

‎Objects/codeobject.c‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,50 @@ intern_strings(PyObject *tuple)
3939
}
4040
}
4141

42+
/* Intern selected string constants */
43+
static int
44+
intern_string_constants(PyObject *tuple)
45+
{
46+
int modified = 0;
47+
Py_ssize_t i;
48+
49+
for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
50+
PyObject *v = PyTuple_GET_ITEM(tuple, i);
51+
if (PyString_CheckExact(v)) {
52+
if (all_name_chars((unsigned char *)PyString_AS_STRING(v))) {
53+
PyObject *w = v;
54+
PyString_InternInPlace(&v);
55+
if (w != v) {
56+
PyTuple_SET_ITEM(tuple, i, v);
57+
modified = 1;
58+
}
59+
}
60+
}
61+
else if (PyTuple_CheckExact(v)) {
62+
intern_string_constants(v);
63+
}
64+
else if (PyFrozenSet_CheckExact(v)) {
65+
PyObject *tmp = PySequence_Tuple(v);
66+
if (tmp == NULL) {
67+
PyErr_Clear();
68+
continue;
69+
}
70+
if (intern_string_constants(tmp)) {
71+
v = PyFrozenSet_New(tmp);
72+
if (v == NULL) {
73+
PyErr_Clear();
74+
}
75+
else {
76+
PyTuple_SET_ITEM(tuple, i, v);
77+
modified = 1;
78+
}
79+
}
80+
Py_DECREF(tmp);
81+
}
82+
}
83+
return modified;
84+
}
85+
4286

4387
PyCodeObject *
4488
PyCode_New(int argcount, int nlocals, int stacksize, int flags,
@@ -68,15 +112,7 @@ PyCode_New(int argcount, int nlocals, int stacksize, int flags,
68112
intern_strings(varnames);
69113
intern_strings(freevars);
70114
intern_strings(cellvars);
71-
/* Intern selected string constants */
72-
for (i = PyTuple_Size(consts); --i >= 0; ) {
73-
PyObject *v = PyTuple_GetItem(consts, i);
74-
if (!PyString_Check(v))
75-
continue;
76-
if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
77-
continue;
78-
PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
79-
}
115+
intern_string_constants(consts);
80116
co = PyObject_NEW(PyCodeObject, &PyCode_Type);
81117
if (co != NULL) {
82118
co->co_argcount = argcount;

0 commit comments

Comments
 (0)