@@ -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
4387PyCodeObject *
4488PyCode_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