Skip to content

Commit c0a5ebe

Browse files
bpo-46430: Intern strings in deep-frozen modules (GH-30683)
1 parent 128ab09 commit c0a5ebe

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

Include/internal/pycore_code.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
279279

280280
/* Deallocator function for static codeobjects used in deepfreeze.py */
281281
void _PyStaticCode_Dealloc(PyCodeObject *co);
282+
/* Function to intern strings of codeobjects */
283+
void _PyStaticCode_InternStrings(PyCodeObject *co);
282284

283285
#ifdef Py_STATS
284286

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Intern strings in deep-frozen modules. Patch by Kumar Aditya.

Objects/codeobject.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,3 +1924,15 @@ _PyStaticCode_Dealloc(PyCodeObject *co)
19241924
co->co_weakreflist = NULL;
19251925
}
19261926
}
1927+
1928+
void
1929+
_PyStaticCode_InternStrings(PyCodeObject *co)
1930+
{
1931+
int res = intern_strings(co->co_names);
1932+
assert(res == 0);
1933+
res = intern_string_constants(co->co_consts, NULL);
1934+
assert(res == 0);
1935+
res = intern_strings(co->co_localsplusnames);
1936+
assert(res == 0);
1937+
(void)res;
1938+
}

Tools/scripts/deepfreeze.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
279279
self.write(f".co_cellvars = {co_cellvars},")
280280
self.write(f".co_freevars = {co_freevars},")
281281
self.deallocs.append(f"_PyStaticCode_Dealloc(&{name});")
282+
self.patchups.append(f"_PyStaticCode_InternStrings(&{name});")
282283
return f"& {name}.ob_base"
283284

284285
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:

0 commit comments

Comments
 (0)