Package list python-procrunner / fbc4ce4
Stop overridden environment variables leaking Work around interesting (read: unexpected, stupid) behaviour of os.environ: >>> import os, copy >>> "x" in os.environ False >>> copy.copy(os.environ) is os.environ False >>> copy.copy(os.environ)["x"] = "1" >>> "x" in os.environ True Markus Gerstel 2 years ago
3 changed file(s) with 11 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
00 =======
11 History
22 =======
3
4 1.0.2 (2019-05-20)
5 ------------------
6
7 * Stop environment override variables leaking into the process environment
38
49 1.0.1 (2019-04-16)
510 ------------------
22 from __future__ import absolute_import, division, print_function
33
44 import codecs
5 import copy
65 import logging
76 import os
87 import select
437436 if environment is not None:
438437 env = {key: _path_resolve(environment[key]) for key in environment}
439438 else:
440 env = os.environ
439 env = {key: value for key, value in os.environ.items()}
441440 if environment_override:
442 env = copy.copy(env)
443441 env.update(
444442 {
445443 key: str(_path_resolve(environment_override[key]))
5757 sentinel_value = b"sentinel"
5858 tmpdir.join("tempfile").write(sentinel_value)
5959 tmpdir.join("reader.py").write("print(open('tempfile').read())")
60 assert "LEAK_DETECTOR" not in os.environ
6061 result = procrunner.run(
6162 [sys.executable, tmpdir.join("reader.py")],
62 environment_override={"PYTHONHASHSEED": "random"},
63 environment_override={"PYTHONHASHSEED": "random", "LEAK_DETECTOR": "1"},
6364 working_directory=tmpdir,
6465 )
6566 assert result.returncode == 0
6667 assert not result.stderr
6768 assert sentinel_value == result.stdout.strip()
69 assert (
70 "LEAK_DETECTOR" not in os.environ
71 ), "overridden environment variable leaked into parent process"