Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the script's module dictionary being printed to stderr #241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/shiv/bootstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import zipfile

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

Expand All @@ -18,13 +17,11 @@
from .interpreter import execute_interpreter


def run(module): # pragma: no cover
"""Run a module in a scrubbed environment.
def scrub_environment(): # pragma: no cover
"""Scrubs the environment.

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.

:param Callable module: The entry point to invoke the pyz with.
"""
with suppress(KeyError):
del os.environ[Environment.MODULE]
Expand All @@ -35,8 +32,6 @@ def run(module): # pragma: no cover
with suppress(KeyError):
del os.environ[Environment.CONSOLE_SCRIPT]

sys.exit(module())


@contextmanager
def current_zipfile():
Expand Down Expand Up @@ -250,10 +245,14 @@ def bootstrap(): # pragma: no cover

# do entry point import and call
if env.entry_point is not None and not env.script:
run(import_string(env.entry_point))
scrub_environment()
entry_callable = import_string(env.entry_point)
sys.exit(entry_callable())

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

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