From 2c524faacdceeb7e44ab2b26317defec197cbc85 Mon Sep 17 00:00:00 2001 From: Pavel Minaev Date: Fri, 31 Jul 2020 02:19:24 -0700 Subject: [PATCH] Fix #305: Add "pythonArgs" config property for interpreter arguments Expose "pythonArgs" to clients. Make "python" usable in tests in lieu of "pythonPath", and make the runners use it. Add tests for all combinations of "python"/"pythonPath" and "pythonArgs". --- src/debugpy/adapter/clients.py | 2 ++ tests/debug/config.py | 2 ++ tests/debug/runners.py | 15 +++++++-------- tests/debugpy/test_run.py | 26 +++++++++++++++++++++----- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/debugpy/adapter/clients.py b/src/debugpy/adapter/clients.py index 09f98214..47d0f3bd 100644 --- a/src/debugpy/adapter/clients.py +++ b/src/debugpy/adapter/clients.py @@ -302,6 +302,8 @@ class Client(components.Component): python = request(python_key, json.array(unicode, vectorize=True, size=(0,))) if not len(python): python = [compat.filename(sys.executable)] + + python += request("pythonArgs", json.array(unicode, size=(0,))) request.arguments["pythonArgs"] = python[1:] program = module = code = () diff --git a/tests/debug/config.py b/tests/debug/config.py index ff5550ce..7581b8c8 100644 --- a/tests/debug/config.py +++ b/tests/debug/config.py @@ -42,6 +42,8 @@ class DebugConfig(collections.MutableMapping): "postDebugTask": (), "preLaunchTask": (), "pyramid": False, + "python": (), + "pythonArgs": [], "pythonPath": (), "redirectOutput": False, "rules": [], diff --git a/tests/debug/runners.py b/tests/debug/runners.py index c7e5e6d7..ef75963f 100644 --- a/tests/debug/runners.py +++ b/tests/debug/runners.py @@ -117,23 +117,22 @@ def _runner(f): @_runner -def launch(session, target, console="integratedTerminal", cwd=None): - assert console in ("internalConsole", "integratedTerminal", "externalTerminal") +def launch(session, target, console=None, cwd=None): + assert console in (None, "internalConsole", "integratedTerminal", "externalTerminal") log.info("Launching {0} in {1} using {2!j}.", target, session, console) target.configure(session) config = session.config config.setdefaults( - { - "console": "externalTerminal", - "internalConsoleOptions": "neverOpen", - "pythonPath": sys.executable, - } + {"console": "externalTerminal", "internalConsoleOptions": "neverOpen"} ) - config["console"] = console + if console is not None: + config["console"] = console if cwd is not None: config["cwd"] = cwd + if "python" not in config and "pythonPath" not in config: + config["python"] = sys.executable env = ( session.spawn_adapter.env diff --git a/tests/debugpy/test_run.py b/tests/debugpy/test_run.py index 36ac4cdf..f8bb9f78 100644 --- a/tests/debugpy/test_run.py +++ b/tests/debugpy/test_run.py @@ -138,7 +138,10 @@ def test_sudo(pyfile, tmpdir, run, target): @pytest.mark.parametrize("run", runners.all_launch_terminal) -def test_custom_python(pyfile, run, target): +@pytest.mark.parametrize("python_args", ["", "-v"]) +@pytest.mark.parametrize("python", ["", "custompy", "custompy -O"]) +@pytest.mark.parametrize("python_key", ["python", "pythonPath"]) +def test_custom_python(pyfile, run, target, python_key, python, python_args): @pyfile def code_to_debug(): import sys @@ -146,19 +149,32 @@ def test_custom_python(pyfile, run, target): from debuggee import backchannel debuggee.setup() - backchannel.send(sys.executable) + backchannel.send([sys.executable, sys.flags.optimize, sys.flags.verbose]) + + python = python.split() + python_args = python_args.split() + python_cmd = (python if len(python) else [sys.executable]) + python_args class Session(debug.Session): def run_in_terminal(self, args, cwd, env): - assert args[:2] == ["CUSTOMPY", "-O"] + assert args[: len(python_cmd)] == python_cmd args[0] = sys.executable return super(Session, self).run_in_terminal(args, cwd, env) with Session() as session: - session.config["pythonPath"] = ["CUSTOMPY", "-O"] + session.config.pop("python", None) + session.config.pop("pythonPath", None) + if len(python): + session.config[python_key] = python[0] if len(python) == 1 else python + if len(python_args): + session.config["pythonArgs"] = python_args backchannel = session.open_backchannel() with run(session, target(code_to_debug)): pass - assert backchannel.receive() == sys.executable + assert backchannel.receive() == [ + sys.executable, + "-O" in python_cmd, + "-v" in python_cmd, + ]