Add zipimport hook at the end just in case (#280)

In Python, the zipimport path hook is usually the first entry in
sys.path_hooks, so that any zip files in sys.path can be handled
correctly. In the APE, the zipimport hook was removed because it was
relatively slow compared to Cosmopolitan when it came to handling
imports from the APE's internal zip store.

However, some python scripts (for example when pip installs some
packages) modify sys.path to consider a local zip file, and then attempt
to import from it. This change prevents potential "unable to import"
errors in such cases, so that Actually Portable Python can be more of a
drop-in improved replacement.
This commit is contained in:
Gautham 2021-09-29 14:20:34 +05:30 committed by GitHub
parent 9cabdaabfd
commit 2fe8571010
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View file

@ -152,8 +152,9 @@ _PyImportZip_Init(void)
"# can't import zipimport.zipimporter\n");
}
else {
/* sys.path_hooks.insert(0, zipimporter) */
err = PyList_Insert(path_hooks, 0, zipimporter);
/* sys.path_hooks.append(zipimporter) */
/* add this hook in case of import from external zip */
err = PyList_Append(path_hooks, zipimporter);
Py_DECREF(zipimporter);
if (err < 0) {
goto error;

View file

@ -295,6 +295,8 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
Py_XDECREF(warnings_module);
}
_PyImportZip_Init();
if (!Py_NoSiteFlag)
_Py_InitSite(); /* Module site */
}