SeaHOH / memimport

Helps import Python extensions from memory, e.g. extensions from Zip files or Web.
Other
2 stars 1 forks source link

Namespace import busted. #3

Closed AraHaan closed 2 months ago

AraHaan commented 3 months ago

I have a python file that does something along the lines of

from discord.ext.commands import Bot

And it seems that with this on that zip file the import is busted and it becomes unable to find that module.

AraHaan commented 3 months ago

It seems it happens when doing that import because discord\ext folder does not have an empty __init__.py and when building the zip does not result in an empty __init__.pyc file to tell the importer to import it all properly.

SeaHOH commented 3 months ago

It seems a bug in official zipimport.py module, _is_dir function only check existing entries, the dir path were not be compressed in as an entry in most cases.

AraHaan commented 3 months ago

So, basically contribute a patch that checks for directories inside of that directory that might have python files ()check nested directories) in the case that it does not find any python files directly in the directory that it is checking? Sounds like a good patch to me.

SeaHOH commented 3 months ago

In my way, I may put some fake directory entries like this:

# Directory entries may be not included which can cause namespace import fail,
# add them here.
def _fix_up_directory(files):
    for path in tuple(files.keys()):
        dirpath = _path_dirname(path) + '\\'
        if dirpath not in files:
            files[dirpath] = (_path_dirname(files[path][0]), 0, 0, 0, 0, 0, 0, 0)

def _read_directory_fixed(archive):
    return _fix_up_directory(_read_directory(archive))
SeaHOH commented 3 months ago

After bug fixed, here will add backports for older versions if official does not.