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

China☆狼群 提交于 2020-06-12 12:57:13

问题


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?


回答1:


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 this effort 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.



来源:https://stackoverflow.com/questions/44248393/why-is-the-import-of-so-files-from-zip-files-disallowed-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!