Skip to content

Commit 00a0fc1

Browse files
Issue python#27942: String constants now interned recursively in tuples and frozensets.
1 parent 55f3ae6 commit 00a0fc1

File tree

5 files changed

+951
-875
lines changed

5 files changed

+951
-875
lines changed

Lib/test/test_code.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
103103
"""
104104

105+
import sys
105106
import unittest
106107
import weakref
107108
from test.support import run_doctest, run_unittest, cpython_only
@@ -134,6 +135,43 @@ def test_newempty(self):
134135
self.assertEqual(co.co_name, "funcname")
135136
self.assertEqual(co.co_firstlineno, 15)
136137

138+
class CodeConstsTest(unittest.TestCase):
139+
140+
def find_const(self, consts, value):
141+
for v in consts:
142+
if v == value:
143+
return v
144+
self.assertIn(value, consts) # rises an exception
145+
self.fail('Should be never reached')
146+
147+
def assertIsInterned(self, s):
148+
if s is not sys.intern(s):
149+
self.fail('String %r is not interned' % (s,))
150+
151+
@cpython_only
152+
def test_interned_string(self):
153+
co = compile('res = "str_value"', '?', 'exec')
154+
v = self.find_const(co.co_consts, 'str_value')
155+
self.assertIsInterned(v)
156+
157+
@cpython_only
158+
def test_interned_string_in_tuple(self):
159+
co = compile('res = ("str_value",)', '?', 'exec')
160+
v = self.find_const(co.co_consts, ('str_value',))
161+
self.assertIsInterned(v[0])
162+
163+
@cpython_only
164+
def test_interned_string_in_frozenset(self):
165+
co = compile('res = a in {"str_value"}', '?', 'exec')
166+
v = self.find_const(co.co_consts, frozenset(('str_value',)))
167+
self.assertIsInterned(tuple(v)[0])
168+
169+
@cpython_only
170+
def test_interned_string_default(self):
171+
def f(a='str_value'):
172+
return a
173+
self.assertIsInterned(f())
174+
137175

138176
class CodeWeakRefTest(unittest.TestCase):
139177

@@ -163,7 +201,7 @@ def callback(code):
163201
def test_main(verbose=None):
164202
from test import test_code
165203
run_doctest(test_code, verbose)
166-
run_unittest(CodeTest, CodeWeakRefTest)
204+
run_unittest(CodeTest, CodeConstsTest, CodeWeakRefTest)
167205

168206

169207
if __name__ == "__main__":

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 #27942: String constants now interned recursively in tuples and frozensets.
14+
1315
- Issue #21578: Fixed misleading error message when ImportError called with
1416
invalid keyword args.
1517

Objects/codeobject.c

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,50 @@ intern_strings(PyObject *tuple)
4646
}
4747
}
4848

49+
/* Intern selected string constants */
50+
static int
51+
intern_string_constants(PyObject *tuple)
52+
{
53+
int modified = 0;
54+
Py_ssize_t i;
55+
56+
for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
57+
PyObject *v = PyTuple_GET_ITEM(tuple, i);
58+
if (PyUnicode_CheckExact(v)) {
59+
if (all_name_chars(v)) {
60+
PyObject *w = v;
61+
PyUnicode_InternInPlace(&v);
62+
if (w != v) {
63+
PyTuple_SET_ITEM(tuple, i, v);
64+
modified = 1;
65+
}
66+
}
67+
}
68+
else if (PyTuple_CheckExact(v)) {
69+
intern_string_constants(v);
70+
}
71+
else if (PyFrozenSet_CheckExact(v)) {
72+
PyObject *tmp = PySequence_Tuple(v);
73+
if (tmp == NULL) {
74+
PyErr_Clear();
75+
continue;
76+
}
77+
if (intern_string_constants(tmp)) {
78+
v = PyFrozenSet_New(tmp);
79+
if (v == NULL) {
80+
PyErr_Clear();
81+
}
82+
else {
83+
PyTuple_SET_ITEM(tuple, i, v);
84+
modified = 1;
85+
}
86+
}
87+
Py_DECREF(tmp);
88+
}
89+
}
90+
return modified;
91+
}
92+
4993

5094
PyCodeObject *
5195
PyCode_New(int argcount, int kwonlyargcount,
@@ -84,13 +128,7 @@ PyCode_New(int argcount, int kwonlyargcount,
84128
intern_strings(varnames);
85129
intern_strings(freevars);
86130
intern_strings(cellvars);
87-
/* Intern selected string constants */
88-
for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
89-
PyObject *v = PyTuple_GetItem(consts, i);
90-
if (!all_name_chars(v))
91-
continue;
92-
PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
93-
}
131+
intern_string_constants(consts);
94132
/* Create mapping between cells and arguments if needed. */
95133
if (n_cellvars) {
96134
Py_ssize_t total_args = argcount + kwonlyargcount +

Python/importlib.h

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,70 +1920,69 @@ const unsigned char _Py_M__importlib[] = {
19201920
115,32,109,117,115,116,32,98,101,32,101,120,112,108,105,99,
19211921
105,116,108,121,32,112,97,115,115,101,100,32,105,110,46,10,
19221922
10,32,32,32,32,114,141,0,0,0,114,34,0,0,0,78,
1923-
114,62,0,0,0,41,1,122,9,95,119,97,114,110,105,110,
1924-
103,115,41,16,114,57,0,0,0,114,14,0,0,0,114,13,
1925-
0,0,0,114,21,0,0,0,218,5,105,116,101,109,115,114,
1926-
177,0,0,0,114,76,0,0,0,114,150,0,0,0,114,82,
1927-
0,0,0,114,160,0,0,0,114,132,0,0,0,114,137,0,
1928-
0,0,114,1,0,0,0,114,200,0,0,0,114,5,0,0,
1929-
0,114,77,0,0,0,41,12,218,10,115,121,115,95,109,111,
1930-
100,117,108,101,218,11,95,105,109,112,95,109,111,100,117,108,
1931-
101,90,11,109,111,100,117,108,101,95,116,121,112,101,114,15,
1932-
0,0,0,114,89,0,0,0,114,99,0,0,0,114,88,0,
1933-
0,0,90,11,115,101,108,102,95,109,111,100,117,108,101,90,
1934-
12,98,117,105,108,116,105,110,95,110,97,109,101,90,14,98,
1935-
117,105,108,116,105,110,95,109,111,100,117,108,101,90,13,116,
1936-
104,114,101,97,100,95,109,111,100,117,108,101,90,14,119,101,
1937-
97,107,114,101,102,95,109,111,100,117,108,101,114,10,0,0,
1938-
0,114,10,0,0,0,114,11,0,0,0,218,6,95,115,101,
1939-
116,117,112,61,4,0,0,115,50,0,0,0,0,9,6,1,
1940-
6,3,12,1,28,1,15,1,15,1,9,1,15,1,9,2,
1941-
3,1,15,1,17,3,13,1,13,1,15,1,15,2,13,1,
1942-
20,3,3,1,16,1,13,2,11,1,16,3,12,1,114,204,
1943-
0,0,0,99,2,0,0,0,0,0,0,0,3,0,0,0,
1944-
3,0,0,0,67,0,0,0,115,87,0,0,0,116,0,0,
1945-
124,0,0,124,1,0,131,2,0,1,116,1,0,106,2,0,
1946-
106,3,0,116,4,0,131,1,0,1,116,1,0,106,2,0,
1947-
106,3,0,116,5,0,131,1,0,1,100,1,0,100,2,0,
1948-
108,6,0,125,2,0,124,2,0,97,7,0,124,2,0,106,
1949-
8,0,116,1,0,106,9,0,116,10,0,25,131,1,0,1,
1950-
100,2,0,83,41,3,122,50,73,110,115,116,97,108,108,32,
1951-
105,109,112,111,114,116,108,105,98,32,97,115,32,116,104,101,
1952-
32,105,109,112,108,101,109,101,110,116,97,116,105,111,110,32,
1953-
111,102,32,105,109,112,111,114,116,46,114,33,0,0,0,78,
1954-
41,11,114,204,0,0,0,114,14,0,0,0,114,174,0,0,
1955-
0,114,113,0,0,0,114,150,0,0,0,114,160,0,0,0,
1956-
218,26,95,102,114,111,122,101,110,95,105,109,112,111,114,116,
1957-
108,105,98,95,101,120,116,101,114,110,97,108,114,119,0,0,
1958-
0,218,8,95,105,110,115,116,97,108,108,114,21,0,0,0,
1959-
114,1,0,0,0,41,3,114,202,0,0,0,114,203,0,0,
1960-
0,114,205,0,0,0,114,10,0,0,0,114,10,0,0,0,
1961-
114,11,0,0,0,114,206,0,0,0,108,4,0,0,115,12,
1962-
0,0,0,0,2,13,2,16,1,16,3,12,1,6,1,114,
1963-
206,0,0,0,41,51,114,3,0,0,0,114,119,0,0,0,
1964-
114,12,0,0,0,114,16,0,0,0,114,17,0,0,0,114,
1965-
59,0,0,0,114,41,0,0,0,114,48,0,0,0,114,31,
1966-
0,0,0,114,32,0,0,0,114,53,0,0,0,114,54,0,
1967-
0,0,114,56,0,0,0,114,63,0,0,0,114,65,0,0,
1968-
0,114,75,0,0,0,114,81,0,0,0,114,84,0,0,0,
1969-
114,90,0,0,0,114,101,0,0,0,114,102,0,0,0,114,
1970-
106,0,0,0,114,85,0,0,0,218,6,111,98,106,101,99,
1971-
116,90,9,95,80,79,80,85,76,65,84,69,114,132,0,0,
1972-
0,114,137,0,0,0,114,144,0,0,0,114,97,0,0,0,
1973-
114,86,0,0,0,114,148,0,0,0,114,149,0,0,0,114,
1974-
87,0,0,0,114,150,0,0,0,114,160,0,0,0,114,165,
1975-
0,0,0,114,171,0,0,0,114,173,0,0,0,114,176,0,
1976-
0,0,114,181,0,0,0,114,191,0,0,0,114,182,0,0,
1977-
0,114,184,0,0,0,114,185,0,0,0,114,186,0,0,0,
1978-
114,194,0,0,0,114,196,0,0,0,114,199,0,0,0,114,
1979-
200,0,0,0,114,204,0,0,0,114,206,0,0,0,114,10,
1980-
0,0,0,114,10,0,0,0,114,10,0,0,0,114,11,0,
1981-
0,0,218,8,60,109,111,100,117,108,101,62,8,0,0,0,
1982-
115,96,0,0,0,6,17,6,2,12,8,12,4,19,20,6,
1983-
2,6,3,22,4,19,68,19,21,19,19,12,19,12,19,12,
1984-
11,18,8,12,11,12,12,12,16,12,36,19,27,19,101,24,
1985-
26,9,3,18,45,18,60,12,18,12,17,12,25,12,29,12,
1986-
23,12,16,19,73,19,77,19,13,12,9,12,9,15,40,12,
1987-
17,6,1,10,2,12,27,12,6,18,24,12,32,12,15,24,
1988-
35,12,7,12,47,
1923+
114,62,0,0,0,41,1,114,141,0,0,0,41,16,114,57,
1924+
0,0,0,114,14,0,0,0,114,13,0,0,0,114,21,0,
1925+
0,0,218,5,105,116,101,109,115,114,177,0,0,0,114,76,
1926+
0,0,0,114,150,0,0,0,114,82,0,0,0,114,160,0,
1927+
0,0,114,132,0,0,0,114,137,0,0,0,114,1,0,0,
1928+
0,114,200,0,0,0,114,5,0,0,0,114,77,0,0,0,
1929+
41,12,218,10,115,121,115,95,109,111,100,117,108,101,218,11,
1930+
95,105,109,112,95,109,111,100,117,108,101,90,11,109,111,100,
1931+
117,108,101,95,116,121,112,101,114,15,0,0,0,114,89,0,
1932+
0,0,114,99,0,0,0,114,88,0,0,0,90,11,115,101,
1933+
108,102,95,109,111,100,117,108,101,90,12,98,117,105,108,116,
1934+
105,110,95,110,97,109,101,90,14,98,117,105,108,116,105,110,
1935+
95,109,111,100,117,108,101,90,13,116,104,114,101,97,100,95,
1936+
109,111,100,117,108,101,90,14,119,101,97,107,114,101,102,95,
1937+
109,111,100,117,108,101,114,10,0,0,0,114,10,0,0,0,
1938+
114,11,0,0,0,218,6,95,115,101,116,117,112,61,4,0,
1939+
0,115,50,0,0,0,0,9,6,1,6,3,12,1,28,1,
1940+
15,1,15,1,9,1,15,1,9,2,3,1,15,1,17,3,
1941+
13,1,13,1,15,1,15,2,13,1,20,3,3,1,16,1,
1942+
13,2,11,1,16,3,12,1,114,204,0,0,0,99,2,0,
1943+
0,0,0,0,0,0,3,0,0,0,3,0,0,0,67,0,
1944+
0,0,115,87,0,0,0,116,0,0,124,0,0,124,1,0,
1945+
131,2,0,1,116,1,0,106,2,0,106,3,0,116,4,0,
1946+
131,1,0,1,116,1,0,106,2,0,106,3,0,116,5,0,
1947+
131,1,0,1,100,1,0,100,2,0,108,6,0,125,2,0,
1948+
124,2,0,97,7,0,124,2,0,106,8,0,116,1,0,106,
1949+
9,0,116,10,0,25,131,1,0,1,100,2,0,83,41,3,
1950+
122,50,73,110,115,116,97,108,108,32,105,109,112,111,114,116,
1951+
108,105,98,32,97,115,32,116,104,101,32,105,109,112,108,101,
1952+
109,101,110,116,97,116,105,111,110,32,111,102,32,105,109,112,
1953+
111,114,116,46,114,33,0,0,0,78,41,11,114,204,0,0,
1954+
0,114,14,0,0,0,114,174,0,0,0,114,113,0,0,0,
1955+
114,150,0,0,0,114,160,0,0,0,218,26,95,102,114,111,
1956+
122,101,110,95,105,109,112,111,114,116,108,105,98,95,101,120,
1957+
116,101,114,110,97,108,114,119,0,0,0,218,8,95,105,110,
1958+
115,116,97,108,108,114,21,0,0,0,114,1,0,0,0,41,
1959+
3,114,202,0,0,0,114,203,0,0,0,114,205,0,0,0,
1960+
114,10,0,0,0,114,10,0,0,0,114,11,0,0,0,114,
1961+
206,0,0,0,108,4,0,0,115,12,0,0,0,0,2,13,
1962+
2,16,1,16,3,12,1,6,1,114,206,0,0,0,41,51,
1963+
114,3,0,0,0,114,119,0,0,0,114,12,0,0,0,114,
1964+
16,0,0,0,114,17,0,0,0,114,59,0,0,0,114,41,
1965+
0,0,0,114,48,0,0,0,114,31,0,0,0,114,32,0,
1966+
0,0,114,53,0,0,0,114,54,0,0,0,114,56,0,0,
1967+
0,114,63,0,0,0,114,65,0,0,0,114,75,0,0,0,
1968+
114,81,0,0,0,114,84,0,0,0,114,90,0,0,0,114,
1969+
101,0,0,0,114,102,0,0,0,114,106,0,0,0,114,85,
1970+
0,0,0,218,6,111,98,106,101,99,116,90,9,95,80,79,
1971+
80,85,76,65,84,69,114,132,0,0,0,114,137,0,0,0,
1972+
114,144,0,0,0,114,97,0,0,0,114,86,0,0,0,114,
1973+
148,0,0,0,114,149,0,0,0,114,87,0,0,0,114,150,
1974+
0,0,0,114,160,0,0,0,114,165,0,0,0,114,171,0,
1975+
0,0,114,173,0,0,0,114,176,0,0,0,114,181,0,0,
1976+
0,114,191,0,0,0,114,182,0,0,0,114,184,0,0,0,
1977+
114,185,0,0,0,114,186,0,0,0,114,194,0,0,0,114,
1978+
196,0,0,0,114,199,0,0,0,114,200,0,0,0,114,204,
1979+
0,0,0,114,206,0,0,0,114,10,0,0,0,114,10,0,
1980+
0,0,114,10,0,0,0,114,11,0,0,0,218,8,60,109,
1981+
111,100,117,108,101,62,8,0,0,0,115,96,0,0,0,6,
1982+
17,6,2,12,8,12,4,19,20,6,2,6,3,22,4,19,
1983+
68,19,21,19,19,12,19,12,19,12,11,18,8,12,11,12,
1984+
12,12,16,12,36,19,27,19,101,24,26,9,3,18,45,18,
1985+
60,12,18,12,17,12,25,12,29,12,23,12,16,19,73,19,
1986+
77,19,13,12,9,12,9,15,40,12,17,6,1,10,2,12,
1987+
27,12,6,18,24,12,32,12,15,24,35,12,7,12,47,
19891988
};

0 commit comments

Comments
 (0)