mirror of https://github.com/python/cpython
bpo-46430: Fix memory leak in interned strings of deep-frozen modules (GH-31549)
This commit is contained in:
parent
042f31da55
commit
4dc746310b
|
@ -65,7 +65,7 @@ extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
|
||||||
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
|
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
|
||||||
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
|
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
|
||||||
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
|
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
|
||||||
|
extern void _Py_Deepfreeze_Init(void);
|
||||||
|
|
||||||
/* Various internal finalizers */
|
/* Various internal finalizers */
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix memory leak in interned strings of deep-frozen modules.
|
|
@ -14,9 +14,13 @@
|
||||||
#include "Python/frozen_modules/importlib._bootstrap_external.h"
|
#include "Python/frozen_modules/importlib._bootstrap_external.h"
|
||||||
/* End includes */
|
/* End includes */
|
||||||
|
|
||||||
/* Empty finalizer for deepfrozen modules*/
|
/* Empty initializer for deepfrozen modules */
|
||||||
|
void _Py_Deepfreeze_Init(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
/* Empty finalizer for deepfrozen modules */
|
||||||
void
|
void
|
||||||
_Py_Deepfreeze_Fini(void)
|
_Py_Deepfreeze_Fini(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,10 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Empty initializer for deepfrozen modules */
|
||||||
|
void _Py_Deepfreeze_Init(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
/* Empty finalizer for deepfrozen modules */
|
/* Empty finalizer for deepfrozen modules */
|
||||||
void
|
void
|
||||||
_Py_Deepfreeze_Fini(void)
|
_Py_Deepfreeze_Fini(void)
|
||||||
|
|
|
@ -754,7 +754,6 @@ pycore_init_types(PyInterpreterState *interp)
|
||||||
if (_PyStatus_EXCEPTION(status)) {
|
if (_PyStatus_EXCEPTION(status)) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _PyStatus_OK();
|
return _PyStatus_OK();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -827,7 +826,10 @@ pycore_interp_init(PyThreadState *tstate)
|
||||||
if (_PyStatus_EXCEPTION(status)) {
|
if (_PyStatus_EXCEPTION(status)) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
// Intern strings in deep-frozen modules first so that others
|
||||||
|
// can use it instead of creating a heap allocated string.
|
||||||
|
_Py_Deepfreeze_Init();
|
||||||
|
|
||||||
status = pycore_init_types(interp);
|
status = pycore_init_types(interp);
|
||||||
if (_PyStatus_EXCEPTION(status)) {
|
if (_PyStatus_EXCEPTION(status)) {
|
||||||
goto done;
|
goto done;
|
||||||
|
|
|
@ -110,6 +110,7 @@ class Printer:
|
||||||
self.hits, self.misses = 0, 0
|
self.hits, self.misses = 0, 0
|
||||||
self.patchups: list[str] = []
|
self.patchups: list[str] = []
|
||||||
self.deallocs: list[str] = []
|
self.deallocs: list[str] = []
|
||||||
|
self.interns: list[str] = []
|
||||||
self.write('#include "Python.h"')
|
self.write('#include "Python.h"')
|
||||||
self.write('#include "internal/pycore_gc.h"')
|
self.write('#include "internal/pycore_gc.h"')
|
||||||
self.write('#include "internal/pycore_code.h"')
|
self.write('#include "internal/pycore_code.h"')
|
||||||
|
@ -279,7 +280,7 @@ class Printer:
|
||||||
self.write(f".co_cellvars = {co_cellvars},")
|
self.write(f".co_cellvars = {co_cellvars},")
|
||||||
self.write(f".co_freevars = {co_freevars},")
|
self.write(f".co_freevars = {co_freevars},")
|
||||||
self.deallocs.append(f"_PyStaticCode_Dealloc(&{name});")
|
self.deallocs.append(f"_PyStaticCode_Dealloc(&{name});")
|
||||||
self.patchups.append(f"_PyStaticCode_InternStrings(&{name});")
|
self.interns.append(f"_PyStaticCode_InternStrings(&{name});")
|
||||||
return f"& {name}.ob_base"
|
return f"& {name}.ob_base"
|
||||||
|
|
||||||
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
|
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
|
||||||
|
@ -446,6 +447,9 @@ def generate(args: list[str], output: TextIO) -> None:
|
||||||
with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
|
with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
|
||||||
for p in printer.deallocs:
|
for p in printer.deallocs:
|
||||||
printer.write(p)
|
printer.write(p)
|
||||||
|
with printer.block(f"void\n_Py_Deepfreeze_Init(void)"):
|
||||||
|
for p in printer.interns:
|
||||||
|
printer.write(p)
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")
|
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue