Skip to content

Commit f7e415d

Browse files
committed
Un-escape backslashes before populating os.environ
Related to #1656
1 parent 13a9807 commit f7e415d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/tox/config/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ def __setitem__(self, name, value):
408408
self.definitions[name] = value
409409
self.resolved[name] = value
410410

411+
def items(self):
412+
return [(name, self.__getitem__(name)) for name in self.definitions]
413+
414+
def export(self):
415+
# post-process items to avoid internal syntax/semantics
416+
# such as {} being escaped using \{\}, suitable for use with
417+
# os.environ .
418+
return {
419+
name: re.sub(r"\\({|})", r"\1", value)
420+
for name, value in self.items()
421+
if value is not self._DUMMY
422+
}
423+
411424

412425
@tox.hookimpl
413426
def tox_addoption(parser):

src/tox/venv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def _get_os_environ(self, is_test_command=False):
493493
env = os.environ.copy()
494494

495495
# in any case we honor per-testenv setenv configuration
496-
env.update(self.envconfig.setenv)
496+
env.update(self.envconfig.setenv.export())
497497

498498
env["VIRTUAL_ENV"] = str(self.path)
499499
return env

tests/unit/test_venv.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,17 @@ def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatc
777777
monkeypatch.setenv("YY", "456")
778778
config = newconfig(
779779
[],
780-
"""\
780+
r"""
781781
[testenv:python]
782782
commands=python -V
783783
passenv = x123
784784
setenv =
785785
ENV_VAR = value
786+
ESCAPED_VAR = \{value\}
787+
ESCAPED_VAR2 = \\{value\\}
786788
PYTHONPATH = value
789+
TTY_VAR = {tty:ON_VALUE:OFF_VALUE}
790+
COLON = {:}
787791
""",
788792
)
789793
mocksession._clearmocks()
@@ -799,6 +803,10 @@ def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatc
799803
assert env is not None
800804
assert "ENV_VAR" in env
801805
assert env["ENV_VAR"] == "value"
806+
assert env["ESCAPED_VAR"] == "{value}"
807+
assert env["ESCAPED_VAR2"] == r"\{value\}"
808+
assert env["COLON"] == ";" if sys.platform == "win32" else ":"
809+
assert env["TTY_VAR"] == "OFF_VALUE"
802810
assert env["VIRTUAL_ENV"] == str(venv.path)
803811
assert env["X123"] == "123"
804812
assert "PYTHONPATH" in env

0 commit comments

Comments
 (0)