Skip to content

Commit

Permalink
[python-repl] pass env through to repl (#4808)
Browse files Browse the repository at this point in the history
### Problem

The python repl zeros out the environment variables. See #4795

### Solution

Pass a copy of the current environment to the repl pex when it is run.

### Result

The environment is accessible from the repl
  • Loading branch information
baroquebobcat authored Aug 11, 2017
1 parent 02704ef commit 9f415be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/python/pants/backend/python/tasks2/python_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import os

from pex.pex_info import PexInfo

from pants.backend.python.targets.python_requirement_library import PythonRequirementLibrary
Expand Down Expand Up @@ -49,5 +51,6 @@ def setup_repl_session(self, targets):

# NB: **pex_run_kwargs is used by tests only.
def launch_repl(self, pex, **pex_run_kwargs):
po = pex.run(blocking=False, **pex_run_kwargs)
env = pex_run_kwargs.pop('env', os.environ).copy()
po = pex.run(blocking=False, env=env, **pex_run_kwargs)
po.wait()
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.build_graph.target import Target
from pants.task.repl_task_mixin import ReplTaskMixin
from pants.util.contextutil import temporary_dir
from pants.util.contextutil import environment_as, temporary_dir
from pants_test.backend.python.tasks.python_task_test_base import PythonTaskTestBase


Expand Down Expand Up @@ -177,6 +177,13 @@ def test_non_python_targets(self):
expected=[''],
targets=[self.non_python_target])

def test_access_to_env(self):
with environment_as(SOME_ENV_VAR='twelve'):
self.do_test_repl(code=['import os',
'print(os.environ.get("SOME_ENV_VAR"))'],
expected=['twelve'],
targets=[self.library])

def test_ipython(self):
# IPython supports shelling out with a leading !, so indirectly test its presence by reading
# the head of this very file.
Expand Down

0 comments on commit 9f415be

Please sign in to comment.