PyDev.Debugger
PyDev.Debugger copied to clipboard
Wrong sys.argv parsing in patch_args(argv) of file pydev_monkey.py
Hello
When a subprocess is started inside a debug session of pydevd, the arguments of the subprocess are patched by the patch_args(argv)
function in pydev_monkey.py
. In a very special case the argument parsing inside the function fails due to not accurate conditions applied.
Example:
A subprocess should be called with the following arguments:
python -m sub_main -m test
This works very good. The first -m
argument is detected from patch_args
and correctly translated to the --module
argument of pydev. Thus the second -m
argument is ignored and just be passed to the sub_main module of the subprocess.
However, when the same subprocess is started like that:
python -msub_main -m test
It will fail. In this case the second -m
argument is wrongly misinterpreted as --module
flag and thus the -m test
part of the command is not passed to the sub_main
module.
A minimal example is attached to this issue. Just start the main.py. When you run it without pydevd applied, everything works. But when you run it from a pydevd session the second subprocess call will fail. pydevd_destroys_argv.zip
Maybe you think, that is a very rare corner case and it is easy to workaround, just be using -m sub_main
instead of -msub_main
, then I must remember, that the example in the ZIP is a small example, just to demonstrate the issue. It really can happen, if you use pytest together with the testdir-fixture, which really starts a subprocess with -mpytest
. Since -m
is an important argument for pytest to control the markers inside a test, exactly this issue happens then.
Maybe you can find a more stable way of parsing the command line arguments for a python module command? Or maybe this error description helps anyone to avoid long debug-sessions, when using pytest, testdir-fixture and pydevd and markers together.
Workaround for pytest-testdir-fixture:
If you found this error-description from a search-engine and you wonder how you can workaround this, I can give you a hint. Just overwrite the method _getpytestargs
of the testdir class (defined in _pytest/pytester.py
) to not work like that:
def _getpytestargs(self):
return sys.executable, "-mpytest"
but like that:
def _getpytestargs(self):
return sys.executable, "-m", "pytest"
(You can simply assign a new method definition to the Testdir class from outside, so you don't need to change the code inside the pytest package.)
Best regards, André