bpo-32030: Add _PyImport_Fini2() (#4737) · pythoncapi/cpython@92a3c6f · GitHub
Skip to content

Commit 92a3c6f

Browse files
authored
bpo-32030: Add _PyImport_Fini2() (python#4737)
PyImport_ExtendInittab() now uses PyMem_RawRealloc() rather than PyMem_Realloc(). PyImport_ExtendInittab() can be called before Py_Initialize() whereas only the PyMem_Raw allocator is supposed to be used before Py_Initialize(). Add _PyImport_Fini2() to release the memory allocated by PyImport_ExtendInittab() at exit. PyImport_ExtendInittab() now forces the usage of the default raw allocator, to be able to release memory in _PyImport_Fini2(). Don't export these functions anymore to be C API, only to Py_BUILD_CORE: * _PyExc_Fini() * _PyImport_Fini() * _PyGC_DumpShutdownStats() * _PyGC_Fini() * _PyType_Fini() * _Py_HashRandomization_Fini()
1 parent 1b4587a commit 92a3c6f

3 files changed

Lines changed: 78 additions & 35 deletions

File tree

Include/pylifecycle.h

Lines changed: 11 additions & 6 deletions

Modules/main.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -446,17 +446,18 @@ pymain_optlist_clear(_Py_OptList *list)
446446
list->options = NULL;
447447
}
448448

449-
static void
450-
pymain_free_impl(_PyMain *pymain)
451-
{
452-
_Py_CommandLineDetails *cmdline = &pymain->cmdline;
453-
pymain_optlist_clear(&cmdline->warning_options);
454-
pymain_optlist_clear(&cmdline->xoptions);
455-
PyMem_RawFree(cmdline->command);
456449

457-
pymain_optlist_clear(&pymain->env_warning_options);
458-
Py_CLEAR(pymain->main_importer_path);
450+
/* Free global variables which cannot be freed in Py_Finalize():
451+
configuration options set before Py_Initialize() which should
452+
remain valid after Py_Finalize(), since Py_Initialize()/Py_Finalize() can
453+
be called multiple times.
459454
455+
Called with the current memory allocators. */
456+
static void
457+
pymain_free_globals(_PyMain *pymain)
458+
{
459+
_PyPathConfig_Clear(&_Py_path_config);
460+
_PyImport_Fini2();
460461
_PyMainInterpreterConfig_Clear(&pymain->config);
461462

462463
#ifdef __INSURE__
@@ -473,19 +474,33 @@ pymain_free_impl(_PyMain *pymain)
473474
#endif /* __INSURE__ */
474475
}
475476

477+
478+
static void
479+
pymain_free_pymain(_PyMain *pymain)
480+
{
481+
_Py_CommandLineDetails *cmdline = &pymain->cmdline;
482+
pymain_optlist_clear(&cmdline->warning_options);
483+
pymain_optlist_clear(&cmdline->xoptions);
484+
PyMem_RawFree(cmdline->command);
485+
486+
pymain_optlist_clear(&pymain->env_warning_options);
487+
Py_CLEAR(pymain->main_importer_path);
488+
489+
}
490+
476491
static void
477492
pymain_free(_PyMain *pymain)
478493
{
479494
/* Force the allocator used by pymain_parse_cmdline_envvars() */
480495
PyMemAllocatorEx old_alloc;
481496
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
482497

483-
pymain_free_impl(pymain);
498+
pymain_free_pymain(pymain);
499+
pymain_free_globals(pymain);
484500

485501
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
486502
}
487503

488-
489504
static int
490505
pymain_run_main_from_importer(_PyMain *pymain)
491506
{
@@ -1719,13 +1734,6 @@ pymain_impl(_PyMain *pymain)
17191734
pymain->status = 120;
17201735
}
17211736

1722-
/* _PyPathConfig_Clear() cannot be called in Py_FinalizeEx().
1723-
Py_Initialize() and Py_Finalize() can be called multiple times, but it
1724-
must not "forget" parameters set by Py_SetProgramName(), Py_SetPath() or
1725-
Py_SetPythonHome(), whereas _PyPathConfig_Clear() clear all these
1726-
parameters. */
1727-
_PyPathConfig_Clear(&_Py_path_config);
1728-
17291737
return 0;
17301738
}
17311739

Python/import.c

Lines changed: 41 additions & 11 deletions

0 commit comments

Comments
 (0)