Skip to content

Commit

Permalink
Fix broken Windows zipapp
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Oct 17, 2024
1 parent 228b615 commit 526f470
Showing 1 changed file with 11 additions and 36 deletions.
47 changes: 11 additions & 36 deletions tasks/__main__zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import os
import sys
import zipfile
from importlib.abc import SourceLoader
from importlib.util import spec_from_file_location

ABS_HERE = os.path.abspath(os.path.dirname(__file__))
NEW_IMPORT_SYSTEM = sys.version_info[0] >= 3 # noqa: PLR2004


class VersionPlatformSelect:
Expand Down Expand Up @@ -113,41 +114,15 @@ def locate_file(self, path):
return _VER_DISTRIBUTION_CLASS


if NEW_IMPORT_SYSTEM:
from importlib.abc import SourceLoader
from importlib.util import spec_from_file_location

class VersionedFindLoad(VersionPlatformSelect, SourceLoader):
def find_spec(self, fullname, path, target=None): # noqa: ARG002
zip_path = self.find_mod(fullname)
if zip_path is not None:
return spec_from_file_location(name=fullname, loader=self)
return None

def module_repr(self, module):
raise NotImplementedError

else:
from imp import new_module

class VersionedFindLoad(VersionPlatformSelect):
def find_module(self, fullname, path=None): # noqa: ARG002
return self if self.find_mod(fullname) else None

def load_module(self, fullname):
filename = self.get_filename(fullname)
code = self.get_data(filename)
mod = sys.modules.setdefault(fullname, new_module(fullname))
mod.__file__ = filename
mod.__loader__ = self
is_package = filename.endswith("__init__.py")
if is_package:
mod.__path__ = [os.path.dirname(filename)]
mod.__package__ = fullname
else:
mod.__package__ = fullname.rpartition(".")[0]
exec(code, mod.__dict__) # noqa: S102
return mod
class VersionedFindLoad(VersionPlatformSelect, SourceLoader):
def find_spec(self, fullname, path, target=None): # noqa: ARG002
zip_path = self.find_mod(fullname)
if zip_path is not None:
return spec_from_file_location(name=fullname, loader=self)
return None

def module_repr(self, module):
raise NotImplementedError


def run():
Expand Down

0 comments on commit 526f470

Please sign in to comment.