From 4a15f3b1c1332f6f6b4150feb7502ab94e48a6d7 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Fri, 13 Apr 2018 00:04:03 +0800 Subject: [PATCH 1/4] Make sure --version succeeds And make sure what it returns makes sense. In case it isn't, return None. --- pipenv/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pipenv/utils.py b/pipenv/utils.py index 832105fa..e3eba96e 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -220,7 +220,11 @@ def python_version(path_to_python): c = delegator.run([path_to_python, '--version'], block=False) except Exception: return None + if c.return_code != 0: + return None version = parse_python_version(c.out.strip() or c.err.strip()) + if not version: + return None return u'{major}.{minor}.{micro}'.format(**version) From 5b2db86fa9390251c3b5a5b4a8d3bae3e613a19b Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Fri, 13 Apr 2018 00:10:45 +0800 Subject: [PATCH 2/4] Add a trivial test for help --- tests/unit/test_help.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/unit/test_help.py diff --git a/tests/unit/test_help.py b/tests/unit/test_help.py new file mode 100644 index 00000000..c48e76d3 --- /dev/null +++ b/tests/unit/test_help.py @@ -0,0 +1,10 @@ +import subprocess +import sys + + +def test_help(): + output = subprocess.check_output( + [sys.executable, '-m', 'pipenv.help'], + stderr=subprocess.STDOUT, + ) + assert output From 3fa64125c270b5abddad337657397d89a8e2c757 Mon Sep 17 00:00:00 2001 From: Dan Ryan Date: Thu, 12 Apr 2018 18:43:05 -0400 Subject: [PATCH 3/4] Fix `create_pipfile` version parsing Signed-off-by: Dan Ryan --- pipenv/project.py | 18 ++++++++++++------ pipenv/utils.py | 9 +++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pipenv/project.py b/pipenv/project.py index 46bf8103..642dc519 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -44,6 +44,7 @@ from .environments import ( PIPENV_VIRTUALENV, PIPENV_TEST_INDEX, PIPENV_PYTHON, + PIPENV_DEFAULT_PYTHON_VERSION, ) @@ -578,12 +579,17 @@ class Project(object): u'dev-packages': {}, } # Default requires. - required_python = python or self.which( - 'python', self.virtualenv_location - ) - data[u'requires'] = { - 'python_version': python_version(required_python)[: len('2.7')] - } + required_python = python + if not python: + if self.virtualenv_location: + required_python = self.which('python', self.virtualenv_location) + else: + required_python = self.which('python') + version = python_version(required_python) or PIPENV_DEFAULT_PYTHON_VERSION + if version and len(version) >= 3: + data[u'requires'] = { + 'python_version': version[: len('2.7')] + } self.write_toml(data, 'Pipfile') def write_toml(self, data, path=None): diff --git a/pipenv/utils.py b/pipenv/utils.py index e3eba96e..4a4ce008 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -220,12 +220,13 @@ def python_version(path_to_python): c = delegator.run([path_to_python, '--version'], block=False) except Exception: return None - if c.return_code != 0: - return None + c.block() version = parse_python_version(c.out.strip() or c.err.strip()) - if not version: + try: + version = u'{major}.{minor}.{micro}'.format(**version) + except TypeError: return None - return u'{major}.{minor}.{micro}'.format(**version) + return version def escape_grouped_arguments(s): From cf3c5611005e9b9a5ffda54de959aed588a19af3 Mon Sep 17 00:00:00 2001 From: Dan Ryan Date: Thu, 12 Apr 2018 19:41:01 -0400 Subject: [PATCH 4/4] Pass environment to subprocess - `check_output` needs to pass `PYTHONIOENCODING` to `Popen` on python2 Signed-off-by: Dan Ryan --- tests/unit/test_help.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_help.py b/tests/unit/test_help.py index c48e76d3..2432e969 100644 --- a/tests/unit/test_help.py +++ b/tests/unit/test_help.py @@ -1,3 +1,4 @@ +import os import subprocess import sys @@ -5,6 +6,6 @@ import sys def test_help(): output = subprocess.check_output( [sys.executable, '-m', 'pipenv.help'], - stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT, env=os.environ.copy(), ) assert output