Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Bug 782847 - Pymake native commands don't pass the correct environmen…
Browse files Browse the repository at this point in the history
…t to subprocesses. r=gps

This change is technically backwards incompatible, but it brings native command
behavior in line with subprocess behavior.
  • Loading branch information
sunshowers committed Aug 21, 2012
1 parent 1c62ab1 commit d343259
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 7 additions & 3 deletions pymake/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,13 @@ def __init__(self, module, method, argv, env, cwd, pycommandpath=None):
self.pycommandpath = pycommandpath or []

def run(self):
oldenv = os.environ
# os.environ is a magic dictionary. Setting it to something else
# doesn't affect the environment of subprocesses, so use clear/update
oldenv = dict(os.environ)
try:
os.chdir(self.cwd)
os.environ = self.env
os.environ.clear()
os.environ.update(self.env)
if self.module not in sys.modules:
load_module_recursive(self.module,
sys.path + self.pycommandpath)
Expand Down Expand Up @@ -240,7 +243,8 @@ def run(self):
print >>sys.stderr, traceback.print_exc()
return (e.code if isinstance(e.code, int) else 1)
finally:
os.environ = oldenv
os.environ.clear()
os.environ.update(oldenv)
return 0

def job_runner(job):
Expand Down
5 changes: 3 additions & 2 deletions tests/native-environment.mk
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#T gmake skip
export EXPECTED := some data

CMD = %pycmd writeenvtofile
PYCOMMANDPATH = $(TESTPATH)

all:
$(CMD) results EXPECTED
%pycmd writeenvtofile results EXPECTED
test "$$(cat results)" = "$(EXPECTED)"
%pycmd writesubprocessenvtofile results EXPECTED
test "$$(cat results)" = "$(EXPECTED)"
@echo TEST-PASS
11 changes: 10 additions & 1 deletion tests/pycmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, sys
import os, sys, subprocess

def writetofile(args):
with open(args[0], 'w') as f:
Expand All @@ -8,6 +8,15 @@ def writeenvtofile(args):
with open(args[0], 'w') as f:
f.write(os.environ[args[1]])

def writesubprocessenvtofile(args):
with open(args[0], 'w') as f:
p = subprocess.Popen([sys.executable, "-c",
"import os; print os.environ['%s']" % args[1]],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
assert p.returncode == 0
f.write(stdout)

def convertasplode(arg):
try:
return int(arg)
Expand Down

0 comments on commit d343259

Please sign in to comment.