From c1cf73111920c7e10a4ba27dddd84b7daa2de6b1 Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Wed, 29 Sep 2021 04:49:13 +0530 Subject: [PATCH] Add zipimport hook at the end just in case 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. --- third_party/python/Python/import.c | 5 +++-- third_party/python/Python/pylifecycle.c | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/third_party/python/Python/import.c b/third_party/python/Python/import.c index c64612697..70721ee71 100644 --- a/third_party/python/Python/import.c +++ b/third_party/python/Python/import.c @@ -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; diff --git a/third_party/python/Python/pylifecycle.c b/third_party/python/Python/pylifecycle.c index e8553e878..6ddc484dd 100644 --- a/third_party/python/Python/pylifecycle.c +++ b/third_party/python/Python/pylifecycle.c @@ -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 */ }