Skip to content

Commit

Permalink
Merge 377ed8d into 32c1dd7
Browse files Browse the repository at this point in the history
  • Loading branch information
lorencarvalho committed May 9, 2019
2 parents 32c1dd7 + 377ed8d commit dd56bc4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/shiv/bootstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import shutil
import zipfile

from contextlib import suppress
from functools import partial
from importlib import import_module
from pathlib import Path

Expand All @@ -14,6 +16,18 @@
from .interpreter import execute_interpreter


def run(module):
# delete the entry point/script env vars
#
# If a single pyz has multiple callers, we want to remove these vars as we no longer need them
# and they can cause subprocesses to fail with a ModuleNotFoundError.
with suppress(KeyError):
del os.environ[Environment.MODULE]
del os.environ[Environment.ENTRY_POINT]

sys.exit(module())


def current_zipfile():
"""A function to vend the current zipfile, if any"""
if zipfile.is_zipfile(sys.argv[0]):
Expand Down Expand Up @@ -155,15 +169,15 @@ def bootstrap(): # pragma: no cover
if env.entry_point is not None:
mod = import_string(env.entry_point)
try:
sys.exit(mod())
run(mod)
except TypeError:
# catch "<module> is not callable", which is thrown when the entry point's
# callable shares a name with it's parent module
# e.g. "from foo.bar import bar; bar()"
sys.exit(getattr(mod, env.entry_point.replace(":", ".").split(".")[1])())
run(getattr(mod, env.entry_point.replace(":", ".").split(".")[1]))

elif env.script is not None:
sys.exit(runpy.run_path(site_packages / "bin" / env.script, run_name="__main__"))
run(partial(runpy.run_path, site_packages / "bin" / env.script, run_name="__main__"))

# all other options exhausted, drop into interactive mode
execute_interpreter()
Expand Down

0 comments on commit dd56bc4

Please sign in to comment.