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

Conversation

joerick
Copy link
Owner

@joerick joerick commented Jul 23, 2023

Fixes #241.

Using trace as an example of a script that changes sys.argv:

Before:

$ python -m pyinstrument -m trace --trace wikipedia_article_word_count.py
...
  _     ._   __/__   _ _  _  _ _/_   Recorded: 17:06:48  Samples:  476
 /_//_/// /_\ / //_// / //_'/ //     Duration: 0.819     CPU time: 0.437
/   _/                      v4.5.1

Program: trace

0.818 <module>  trace.py:1
...

After:

$ python -m pyinstrument -m trace --trace wikipedia_article_word_count.py
...
  _     ._   __/__   _ _  _  _ _/_   Recorded: 17:08:07  Samples:  482
 /_//_/// /_\ / //_// / //_'/ //     Duration: 0.821     CPU time: 0.444
/   _/                      v4.5.1

Program: /Users/joerick/Projects/pyinstrument/pyinstrument/__main__.py -m trace --trace wikipedia_article_word_count.py

0.821 <module>  trace.py:1
...

aside: Note this doesn't change pyinstrument's way of passing sys.argv to children. The behaviour is that all of

  • python script.py arg1 arg2
  • python -m script.py arg1 arg2
  • pyinstrument script.py arg1 arg2

and

  • python -m module arg1 arg2
  • python -m pyinstrument -m module arg1 arg2
  • pyinstrument -m module arg1 arg2

look the same from the child's perspective. That's verified in the test

def test_script_execution_details(self, pyinstrument_invocation, tmp_path: Path):

.

That test was faulty as written, I've also fixed that in this PR.

@matthiasdiener
Copy link
Contributor

Thanks @joerick, this looks good!

@joerick joerick merged commit 59e5b4a into main Aug 4, 2023
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect Session.program when running a module
2 participants