* Provide option to pass environment variables to Environment and CompiledSubprocess (subprocess.Popen)

* Extend this option to find_system_enviornments and get_system_environment without breaking API
This commit is contained in:
Ryan Clary
2020-06-20 10:47:36 -07:00
parent a3410f124a
commit 7ec8454fc1
2 changed files with 31 additions and 21 deletions

View File

@@ -156,11 +156,21 @@ class CompiledSubprocess(object):
# Start with 2, gets set after _get_info.
_pickle_protocol = 2
def __init__(self, executable):
def __init__(self, executable, env_vars={}):
self._executable = executable
self._env_vars = dict(env_vars)
self._inference_state_deletion_queue = queue.deque()
self._cleanup_callable = lambda: None
# Use explicit envionment to ensure reliable results (#1540)
if os.name == 'nt':
# if SYSTEMROOT (or case variant) exists in environment,
# ensure it goes to subprocess
for k, v in os.environ.items():
if 'SYSTEMROOT' == k.upper():
self._env_vars.update({k: os.environ[k]})
break # don't risk multiple entries
def __repr__(self):
pid = os.getpid()
return '<%s _executable=%r, _pickle_protocol=%r, is_crashed=%r, pid=%r>' % (
@@ -181,15 +191,6 @@ class CompiledSubprocess(object):
os.path.dirname(os.path.dirname(parso_path)),
'.'.join(str(x) for x in sys.version_info[:3]),
)
# Use explicit envionment to ensure reliable results (#1540)
env = {}
if os.name == 'nt':
# if SYSTEMROOT (or case variant) exists in environment,
# ensure it goes to subprocess
for k, v in os.environ.items():
if 'SYSTEMROOT' == k.upper():
env.update({k: os.environ[k]})
break # don't risk multiple entries
process = GeneralizedPopen(
args,
stdin=subprocess.PIPE,
@@ -198,7 +199,7 @@ class CompiledSubprocess(object):
# Use system default buffering on Python 2 to improve performance
# (this is already the case on Python 3).
bufsize=-1,
env=env
env=self._env_vars
)
self._stderr_queue = Queue()
self._stderr_thread = t = Thread(