Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In HackedPythonVersion, patch sys.version_info #1076

Merged
merged 2 commits into from
Nov 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions pipenv/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from collections import namedtuple
import os
import hashlib
import tempfile
Expand Down Expand Up @@ -393,17 +394,23 @@ def clean_pkg_version(version):

class HackedPythonVersion(object):
"""A Beautiful hack, which allows us to tell pip which version of Python we're using."""

PatchedSysVersion = namedtuple('PatchedSysVersion', ['major', 'minor', 'micro'])

def __init__(self, python_version, python_path):
self.python_version = python_version
self.python_path = python_path

def __enter__(self):
os.environ['PIP_PYTHON_VERSION'] = str(self.python_version)
os.environ['PIP_PYTHON_PATH'] = str(self.python_path)
self.backup_version_info = sys.version_info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this being used by pip-tools or is it being added as a nice to have for use later?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to reset sys.version_info outside of the context manager (in __exit__). We don't want that patched thing globally.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or was your question "Why are we patching sys.version_info? That's for pip to generate the right list of pep425 tags for wheel selection, i.e. Pick the right wheel for the right python version in the managed venv.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense. Thanks for clarifying!

sys.version_info = self.PatchedSysVersion(*map(int, self.python_version.split('.')))

def __exit__(self, *args):
# Restore original Python version information.
del os.environ['PIP_PYTHON_VERSION']
sys.version_info = self.backup_version_info


def prepare_pip_source_args(sources, pip_args=None):
Expand Down Expand Up @@ -522,17 +529,18 @@ def resolve_deps(deps, which, which_pip, project, sources=None, verbose=False, p
resolved_tree = actually_resolve_reps(deps, index_lookup, markers_lookup, project, sources, verbose, clear, pre)
except RuntimeError:
# Don't exit here, like usual.
pass
resolved_tree = None

# Second (last-resort) attempt:
with HackedPythonVersion(python_version=''.join([str(s) for s in sys.version_info[:3]]), python_path=backup_python_path):
if resolved_tree is None:
with HackedPythonVersion(python_version='.'.join([str(s) for s in sys.version_info[:3]]), python_path=backup_python_path):

try:
# Attempt to resolve again, with different Python version information,
# particularly for particularly particular packages.
resolved_tree = actually_resolve_reps(deps, index_lookup, markers_lookup, project, sources, verbose, clear, pre)
except RuntimeError:
sys.exit(1)
try:
# Attempt to resolve again, with different Python version information,
# particularly for particularly particular packages.
resolved_tree = actually_resolve_reps(deps, index_lookup, markers_lookup, project, sources, verbose, clear, pre)
except RuntimeError:
sys.exit(1)



Expand Down