diff --git a/src/debugpy/server/api.py b/src/debugpy/server/api.py index 2772ab57e..54664fca7 100644 --- a/src/debugpy/server/api.py +++ b/src/debugpy/server/api.py @@ -81,6 +81,8 @@ def configure(properties=None, **kwargs): properties = dict(properties) properties.update(kwargs) + # Validate everything before changing anything, so that a call which raises + # leaves the existing configuration untouched. for k, v in properties.items(): if k not in _config: raise ValueError("Unknown property {0!r}".format(k)) @@ -90,7 +92,8 @@ def configure(properties=None, **kwargs): valid_values = _config_valid_values.get(k) if (valid_values is not None) and (v not in valid_values): raise ValueError("{0!r} must be one of: {1!r}".format(k, valid_values)) - _config[k] = v + + _config.update(properties) def _starts_debugging(func): diff --git a/tests/debugpy/server/test_api.py b/tests/debugpy/server/test_api.py index f8bd7b581..e4dd2dfd1 100644 --- a/tests/debugpy/server/test_api.py +++ b/tests/debugpy/server/test_api.py @@ -82,3 +82,25 @@ def test_listen_empty_adapter_read_raises(monkeypatch): str(exc_info.value) == "error retrieving adapter endpoints: EOF while reading adapter endpoints" ) + + +@pytest.mark.parametrize( + "properties", + [ + {"qt": "auto", "subProcess": "yes"}, + {"qt": "pyside", "unknown": 1}, + {"subProcess": False, "qt": "not-a-qt-mode"}, + ], + ids=["bad-type", "unknown-property", "bad-value"], +) +def test_configure_failure_leaves_config_unchanged(monkeypatch, properties): + # configure() raises ValueError for a property it cannot accept. When it does, + # none of the other properties passed in the same call may have been applied, + # or the caller is left with a configuration it never asked for. + monkeypatch.setattr(api, "ensure_logging", lambda: None) + monkeypatch.setattr(api, "_config", {"qt": "none", "subProcess": True}) + + with pytest.raises(ValueError): + api.configure(properties) + + assert api._config == {"qt": "none", "subProcess": True}