joerick/pyinstrument

Incorrect Session.program when running a module

matthiasdiener opened this issue · 3 comments

Due to sys.argv modifications (in __main__.py?), the program name is stored incorrectly in Session.program:

actual output:

$ pyinstrument -m mpi4py t.py -a 
Program: mpi4py -a

expected output:

$ pyinstrument -m mpi4py t.py -a 
Program: mpi4py t.py -a

Here is what I get with Python 3.10 for the following program:

import sys

print(f"{sys.argv=}")
print(f"{' | '.join(sys.argv)=}")

Output:

$ python t.py --arg
sys.argv=['t.py', '--arg']
' | '.join(sys.argv)='t.py | --arg'

$ python -m mpi4py t.py --arg
sys.argv=['t.py', '--arg']
' | '.join(sys.argv)='t.py | --arg'

$ pyinstrument t.py --arg
sys.argv=['t.py', '--arg']
' | '.join(sys.argv)='t.py | --arg'

  _     ._   __/__   _ _  _  _ _/_   Recorded: 09:25:19  Samples:  0
 /_//_/// /_\ / //_// / //_'/ //     Duration: 0.001     CPU time: 0.001
/   _/                      v4.4.0

Program: t.py --arg

$ pyinstrument -m mpi4py t.py --arg
sys.argv=['t.py', '--arg']
' | '.join(sys.argv)='t.py | --arg'

  _     ._   __/__   _ _  _  _ _/_   Recorded: 09:25:26  Samples:  1
 /_//_/// /_\ / //_// / //_'/ //     Duration: 0.001     CPU time: 0.001
/   _/                      v4.4.0

Program: mpi4py --arg

ie, neither seems to affect sys.argv, but pyinstrument -m does pass down incorrect args to the Session.

One potential solution would be to pass down a copy of the original sys.argv down to the Session constructor, another way could be to use psutil.Process.cmdline.

I think the issue here is that changing sys.argv is pretty common in these styles of wrapper scripts. pyinstrument, mpi4py, cProfile all do it. Each time it happens, each script doesn't reset the value once the script has finished running, because each assumes it's the top-level script.

My thought is that we should consider changing sys.argv a 'patch' - i.e. it should be reset to it's previous value once the code has finished. I'll push a PR of a fix that I think works, and should make more sense.