From aab569cd4a4d1ae67ec9bcf921748615ea48ec59 Mon Sep 17 00:00:00 2001 From: John Sirois Date: Wed, 22 Aug 2018 14:48:15 -0600 Subject: [PATCH 1/2] Support `-` when running as an interpreter. It's surprising when using a pex in python interpreter mode that `-` is not recognized as a program coming from stdin as regular python interpreter would. Add support for this with a test. The surprise of this missing feature was rightly identified in the context of https://github.com/pantsbuild/pants/pull/6275. --- pex/pex.py | 12 ++++++++---- tests/test_pex.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pex/pex.py b/pex/pex.py index 651bfd55c..df0065a17 100644 --- a/pex/pex.py +++ b/pex/pex.py @@ -417,13 +417,17 @@ def _execute(self): def execute_interpreter(self): if sys.argv[1:]: + program = sys.argv[1] try: - with open(sys.argv[1]) as fp: - name, content = sys.argv[1], fp.read() + if program == '-': + content = sys.stdin.read() + else: + with open(program) as fp: + content = fp.read() except IOError as e: - die("Could not open %s in the environment [%s]: %s" % (sys.argv[1], sys.argv[0], e)) + die("Could not open %s in the environment [%s]: %s" % (program, sys.argv[0], e)) sys.argv = sys.argv[1:] - self.execute_content(name, content) + self.execute_content(program, content) else: import code code.interact() diff --git a/tests/test_pex.py b/tests/test_pex.py index f27ea77ed..a494e3fb1 100644 --- a/tests/test_pex.py +++ b/tests/test_pex.py @@ -2,6 +2,7 @@ # Licensed under the Apache License, Version 2.0 (see LICENSE). import os +import subprocess import sys import textwrap from contextlib import contextmanager @@ -342,3 +343,19 @@ def test_activate_interpreter_different_from_current(): pex._activate() except SystemExit as e: pytest.fail('PEX activation of %s failed with %s' % (pex, e)) + + +def test_execute_interpreter_stdin_program(): + with temporary_dir() as pex_chroot: + pex_builder = PEXBuilder(path=pex_chroot) + pex_builder.freeze() + process = PEX(pex_chroot).run(args=['-', 'one', 'two'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=subprocess.PIPE, + blocking=False) + stdout, stderr = process.communicate(input='import sys; print(" ".join(sys.argv[1:]))') + + assert 0 == process.returncode + assert 'one two' == stdout.strip() + assert '' == stderr.strip() From d26ef855b8938bd527d6b639096c79446f1bd726 Mon Sep 17 00:00:00 2001 From: John Sirois Date: Wed, 22 Aug 2018 15:00:28 -0600 Subject: [PATCH 2/2] Fixup test to work under python3. --- tests/test_pex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pex.py b/tests/test_pex.py index a494e3fb1..0e30597cb 100644 --- a/tests/test_pex.py +++ b/tests/test_pex.py @@ -354,8 +354,8 @@ def test_execute_interpreter_stdin_program(): stderr=subprocess.PIPE, stdin=subprocess.PIPE, blocking=False) - stdout, stderr = process.communicate(input='import sys; print(" ".join(sys.argv[1:]))') + stdout, stderr = process.communicate(input=b'import sys; print(" ".join(sys.argv[1:]))') assert 0 == process.returncode - assert 'one two' == stdout.strip() - assert '' == stderr.strip() + assert b'one two\n' == stdout + assert b'' == stderr