Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the import of `*.so` files from ZIP files disallowed in Python?

Why is the import of *.so files from ZIP files disallowed in Python?

The documentation (https://docs.python.org/2/library/zipimport.html) is very clear:

Any files may be present in the ZIP archive, but only files .py and .py[co] are available for import. ZIP import of dynamic modules (.pyd, .so) is disallowed.

But the documentation doesn't name any reason for this strange limitation. Is is because importing from ZIP files is generally discouraged in Python? Or is it because of security reasons? If so, which ones? Is the any official statement about this?

like image 526
vog Avatar asked May 29 '17 18:05

vog


People also ask

How do I import a ZIP file into Python?

Use of 'zipimport' module makes it possible to import Python modules and packages from ZIP-format archives. This module also allows an item of sys. path to be a string naming a ZIP file archive. Any files may be present in the ZIP archive, but only files .

Can Python read files from zip?

To work on zip files using python, we will use an inbuilt python module called zipfile. print ( 'Done!' ) The above program extracts a zip file named “my_python_files.

How do I unzip a ZIP file in Python?

To unzip a file in Python, use the ZipFile. extractall() method. The extractall() method takes a path, members, pwd as an argument and extracts all the contents. To work on zip files using Python, we will use an inbuilt python module called zipfile.


1 Answers

From PEP 273, Subdirectory Equivalence:

You can't satisfy dynamic modules from a zip file. Dynamic modules have extensions like .dll, .pyd, and .so. They are operating system dependent, and probably can't be loaded except from a file. It might be possible to extract the dynamic module from the zip file, write it to a plain file and load it. But that would mean creating temporary files, and dealing with all the dynload_*.c, and that's probably not a good idea.

My interpretation is that this decision was made to avoid having the interpreter extract the dynamic module and save it to disk, as the OS's dynamic library facilities don't allow for loading from within a zip (See Windows' LoadLibraryA and Linux's dlopen).

While it looks like it's not technically impossible, a reason why doing the work to implement this functionality may not be worthwhile is the platform-dependence of these libraries. A .zip containing a dynamic module that is distributed with the code that relies on it may not work in a 32-bit environment if it was created from a 64-bit environment, and wouldn't work in Linux if it was created in Windows. Disallowing dynamic modules in .zips may be a conscious decision to ensure that .zips containing Python modules will work cross-platform.

like image 106
Eric Finn Avatar answered Sep 20 '22 19:09

Eric Finn