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

Reset sys.argv after running child script/module #258

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions pyinstrument/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ def dash_m_callback(option: str, opt: str, value: str, parser: optparse.OptionPa
# when called with '-m', search the cwd for that module
sys.path[0] = os.path.abspath(".")

sys.argv[:] = [options.module_name] + options.module_args
argv = [options.module_name] + options.module_args
code = "run_module(modname, run_name='__main__', alter_sys=True)"
globs = {"run_module": runpy.run_module, "modname": options.module_name}
else:
sys.argv[:] = args
argv = args
if options.from_path:
progname = shutil.which(args[0])
if progname is None:
Expand All @@ -322,10 +322,14 @@ def dash_m_callback(option: str, opt: str, value: str, parser: optparse.OptionPa

profiler.start()

old_argv = sys.argv.copy()
try:
sys.argv[:] = argv
exec(code, globs, None)
except (SystemExit, KeyboardInterrupt):
pass
finally:
sys.argv[:] = old_argv

session = profiler.stop()

Expand Down
2 changes: 1 addition & 1 deletion pyinstrument/magic/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def pyinstrument(self, line, cell=None):
)
as_text = _active_profiler.output_text(timeline=args.timeline)
# repr_html may be a bit fragile, but it's been stable for a while
display({"text/html": as_iframe._repr_html_(), "text/plain": as_text}, raw=True)
display({"text/html": as_iframe._repr_html_(), "text/plain": as_text}, raw=True) # type: ignore

assert not _active_profiler.is_running
_active_profiler = None
Expand Down
5 changes: 3 additions & 2 deletions test/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,22 @@ def test_module_execution_details(self, pyinstrument_invocation, tmp_path: Path)

process_pyi = subprocess.run(
[*pyinstrument_invocation, "-m", "test_module", "arg1", "arg2"],
# stderr=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
cwd=tmp_path,
text=True,
)
process_native = subprocess.run(
[sys.executable, "-m", "test_module", "arg1", "arg2"],
# stderr=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
cwd=tmp_path,
text=True,
)

print("process_pyi.stderr", process_pyi.stderr)
print("process_native.stderr", process_native.stderr)
assert process_native.stderr
assert process_pyi.stderr == process_native.stderr

def test_path_execution_details(self, pyinstrument_invocation, tmp_path: Path, monkeypatch):
Expand Down