diff --git a/src/debugpy/launcher/handlers.py b/src/debugpy/launcher/handlers.py index e1be768dd..26aaf39c9 100644 --- a/src/debugpy/launcher/handlers.py +++ b/src/debugpy/launcher/handlers.py @@ -78,7 +78,7 @@ def property_or_debug_option(prop_name, flag_name): process_name = request("processName", compat.filename(sys.executable)) env = os.environ.copy() - env_changes = request("env", json.object(unicode)) + env_changes = request("env", json.object((unicode, type(None)))) if sys.platform == "win32": # Environment variables are case-insensitive on Win32, so we need to normalize # both dicts to make sure that env vars specified in the debug configuration @@ -94,6 +94,7 @@ def property_or_debug_option(prop_name, flag_name): # applied to the debuggee, since it will conflict with pydevd. env.pop("COV_CORE_SOURCE", None) env.update(env_changes) + env = {k: v for k, v in env.items() if v is not None} if request("gevent", False): env["GEVENT_SUPPORT"] = "True" diff --git a/tests/debugpy/test_env.py b/tests/debugpy/test_env.py index fe572ff8a..09fc7919c 100644 --- a/tests/debugpy/test_env.py +++ b/tests/debugpy/test_env.py @@ -12,9 +12,10 @@ from tests.debug import runners +@pytest.mark.parametrize("new_value", [None, "42"]) @pytest.mark.parametrize("case", ["match_case", "mismatch_case"]) @pytest.mark.parametrize("run", runners.all_launch) -def test_env_replace_var(pyfile, target, run, case): +def test_env_replace_var(pyfile, target, run, case, new_value): @pyfile def code_to_debug(): import os @@ -29,25 +30,36 @@ def code_to_debug(): with debug.Session() as session: backchannel = session.open_backchannel() - session.config.env[varname if case == "match_case" else varname.lower()] = "42" + session.config.env[varname if case == "match_case" else varname.lower()] = new_value os.environ[varname] = "1" - with run(session, target(code_to_debug)): - pass - del os.environ[varname] + try: + with run(session, target(code_to_debug)): + pass + finally: + del os.environ[varname] env = backchannel.receive() if case == "match_case": # If case matches, debug config should replace global env var regardless # of the platform. - assert env[varname] == "42" + if new_value is None: + assert varname not in env + else: + assert env[varname] == "42" elif sys.platform == "win32": # On Win32, variable names are case-insensitive, so debug config should # replace the global env var even if there is a case mismatch. - assert env[varname] == "42" + if new_value is None: + assert varname not in env + else: + assert env[varname] == "42" assert varname.lower() not in env else: # On other platforms, variable names are case-sensitive, so case mismatch # should result in two different variables. assert env[varname] == "1" - assert env[varname.lower()] == "42" + if new_value is None: + assert varname.lower() not in env + else: + assert env[varname.lower()] == "42"