mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-09 19:30:29 +00:00
make test_atexit pass in MODE=
_testcapi.run_in_subinterp makes a questionable PyImport_Cleanup(), which we work around by having a separate cleanup function for the lookup tables. also added a separate initialization function for the lookup tables for symmetry. some commented out code in the previous commit messed with GC, it was made visible again.
This commit is contained in:
parent
113aa300f2
commit
f7e9253e85
4 changed files with 39 additions and 32 deletions
2
third_party/python/Include/import.h
vendored
2
third_party/python/Include/import.h
vendored
|
@ -73,6 +73,8 @@ PyObject * PyImport_GetImporter(PyObject *path);
|
|||
PyObject * PyImport_Import(PyObject *name);
|
||||
PyObject * PyImport_ReloadModule(PyObject *m);
|
||||
void PyImport_Cleanup(void);
|
||||
void _PyImportLookupTables_Init(void);
|
||||
void _PyImportLookupTables_Cleanup(void);
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
|
||||
int PyImport_ImportFrozenModuleObject(
|
||||
PyObject *name
|
||||
|
|
1
third_party/python/Python/finalize.c
vendored
1
third_party/python/Python/finalize.c
vendored
|
@ -96,6 +96,7 @@ Py_FinalizeEx(void)
|
|||
#endif
|
||||
/* Destroy all modules */
|
||||
PyImport_Cleanup();
|
||||
_PyImportLookupTables_Cleanup();
|
||||
|
||||
/* Flush sys.stdout and sys.stderr (again, in case more was printed) */
|
||||
if (_Py_FlushStdFiles() < 0) {
|
||||
|
|
67
third_party/python/Python/import.c
vendored
67
third_party/python/Python/import.c
vendored
|
@ -154,8 +154,6 @@ static int cmp_initentry(const void *_x, const void *_y) {
|
|||
void
|
||||
_PyImport_Init(void)
|
||||
{
|
||||
size_t i, n;
|
||||
|
||||
PyInterpreterState *interp = PyThreadState_Get()->interp;
|
||||
initstr = PyUnicode_InternFromString("__init__");
|
||||
if (initstr == NULL)
|
||||
|
@ -163,27 +161,44 @@ _PyImport_Init(void)
|
|||
interp->builtins_copy = PyDict_Copy(interp->builtins);
|
||||
if (interp->builtins_copy == NULL)
|
||||
Py_FatalError("Can't backup builtins dict");
|
||||
}
|
||||
|
||||
for(n=0; PyImport_Inittab[n].name; n++);
|
||||
Builtins_Lookup.n = n;
|
||||
Builtins_Lookup.entries = malloc(sizeof(initentry) * n);
|
||||
for(i=0; i < n; i++) {
|
||||
Builtins_Lookup.entries[i].name = PyImport_Inittab[i].name;
|
||||
Builtins_Lookup.entries[i].tab = &(PyImport_Inittab[i]);
|
||||
void _PyImportLookupTables_Init(void) {
|
||||
size_t i, n;
|
||||
if (Builtins_Lookup.entries == NULL) {
|
||||
for(n=0; PyImport_Inittab[n].name; n++);
|
||||
Builtins_Lookup.n = n;
|
||||
Builtins_Lookup.entries = malloc(sizeof(initentry) * n);
|
||||
for(i=0; i < n; i++) {
|
||||
Builtins_Lookup.entries[i].name = PyImport_Inittab[i].name;
|
||||
Builtins_Lookup.entries[i].tab = &(PyImport_Inittab[i]);
|
||||
}
|
||||
qsort(Builtins_Lookup.entries, Builtins_Lookup.n, sizeof(initentry), cmp_initentry);
|
||||
}
|
||||
qsort(Builtins_Lookup.entries, Builtins_Lookup.n, sizeof(initentry), cmp_initentry);
|
||||
|
||||
for(n=0; PyImport_FrozenModules[n].name; n++);
|
||||
Frozens_Lookup.n = n;
|
||||
Frozens_Lookup.entries = malloc(sizeof(initentry) * n);
|
||||
for(i=0; i<n; i++) {
|
||||
Frozens_Lookup.entries[i].name = PyImport_FrozenModules[i].name;
|
||||
Frozens_Lookup.entries[i].frz = &(PyImport_FrozenModules[i]);
|
||||
if (Frozens_Lookup.entries == NULL) {
|
||||
for(n=0; PyImport_FrozenModules[n].name; n++);
|
||||
Frozens_Lookup.n = n;
|
||||
Frozens_Lookup.entries = malloc(sizeof(initentry) * n);
|
||||
for(i=0; i<n; i++) {
|
||||
Frozens_Lookup.entries[i].name = PyImport_FrozenModules[i].name;
|
||||
Frozens_Lookup.entries[i].frz = &(PyImport_FrozenModules[i]);
|
||||
}
|
||||
qsort(Frozens_Lookup.entries, Frozens_Lookup.n, sizeof(initentry), cmp_initentry);
|
||||
}
|
||||
qsort(Frozens_Lookup.entries, Frozens_Lookup.n, sizeof(initentry), cmp_initentry);
|
||||
qsort(ZipCdir_Lookup.entries, ZipCdir_Lookup.n, sizeof(initentry), cmp_initentry);
|
||||
}
|
||||
|
||||
void _PyImportLookupTables_Cleanup(void) {
|
||||
if (Builtins_Lookup.entries != NULL) {
|
||||
free(Builtins_Lookup.entries);
|
||||
Builtins_Lookup.entries = NULL;
|
||||
}
|
||||
if (Frozens_Lookup.entries != NULL) {
|
||||
free(Frozens_Lookup.entries);
|
||||
Frozens_Lookup.entries = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
_PyImportHooks_Init(void)
|
||||
{
|
||||
|
@ -473,15 +488,6 @@ PyImport_Cleanup(void)
|
|||
PyObject *weaklist = NULL;
|
||||
const char * const *p;
|
||||
|
||||
if (Builtins_Lookup.entries != NULL) {
|
||||
free(Builtins_Lookup.entries);
|
||||
Builtins_Lookup.entries = NULL;
|
||||
}
|
||||
if (Frozens_Lookup.entries != NULL) {
|
||||
free(Frozens_Lookup.entries);
|
||||
Frozens_Lookup.entries = NULL;
|
||||
}
|
||||
|
||||
if (modules == NULL)
|
||||
return; /* Already done */
|
||||
|
||||
|
@ -623,7 +629,6 @@ PyImport_Cleanup(void)
|
|||
|
||||
/* Once more */
|
||||
_PyGC_CollectNoFail();
|
||||
|
||||
#undef CLEAR_MODULE
|
||||
#undef STORE_MODULE_WEAKREF
|
||||
}
|
||||
|
@ -1085,11 +1090,11 @@ static const struct _frozen * find_frozen(PyObject *);
|
|||
static int
|
||||
is_builtin(PyObject *name)
|
||||
{
|
||||
static initentry key;
|
||||
initentry key;
|
||||
initentry *res;
|
||||
key.name = PyUnicode_AsUTF8(name);
|
||||
key.tab = NULL;
|
||||
if(!key.name)
|
||||
if(!name || !key.name)
|
||||
return 0;
|
||||
res = bsearch(&key, Builtins_Lookup.entries, Builtins_Lookup.n, sizeof(initentry), cmp_initentry);
|
||||
if (res) {
|
||||
|
@ -1196,14 +1201,12 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* all builtins are static */
|
||||
/*
|
||||
mod = _PyImport_FindExtensionObject(name, name);
|
||||
if (mod || PyErr_Occurred()) {
|
||||
Py_DECREF(name);
|
||||
Py_XINCREF(mod);
|
||||
return mod;
|
||||
} */
|
||||
}
|
||||
|
||||
namestr = PyUnicode_AsUTF8(name);
|
||||
if (namestr == NULL) {
|
||||
|
|
1
third_party/python/Python/pylifecycle.c
vendored
1
third_party/python/Python/pylifecycle.c
vendored
|
@ -248,6 +248,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
|
|||
PySys_SetObject("__stderr__", pstderr);
|
||||
Py_DECREF(pstderr);
|
||||
|
||||
_PyImportLookupTables_Init();
|
||||
_PyImport_Init();
|
||||
|
||||
_PyImportHooks_Init();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue