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

use get_python_info for consistency and caching #1462

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/changelog/1462.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
improve performance with internal lookup of Python version information - by :user:`blueyed`
5 changes: 3 additions & 2 deletions src/tox/interpreters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import tox
from tox import reporter
from tox.constants import SITE_PACKAGE_QUERY_SCRIPT, VERSION_QUERY_SCRIPT
from tox.constants import SITE_PACKAGE_QUERY_SCRIPT
from tox.interpreters.via_path import get_python_info


class Interpreters:
Expand Down Expand Up @@ -56,7 +57,7 @@ def get_sitepackagesdir(self, info, envdir):
def run_and_get_interpreter_info(name, executable):
assert executable
try:
result = exec_on_interpreter(str(executable), VERSION_QUERY_SCRIPT)
result = get_python_info(str(executable))
result["version_info"] = tuple(result["version_info"]) # fix json dump transformation
del result["name"]
del result["version"]
Expand Down
14 changes: 11 additions & 3 deletions src/tox/interpreters/via_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def exe_spec(python_exe, base):
python_exe = str(python_exe)
with _SPECK_LOCK[python_exe]:
if python_exe not in _SPECS:
info = get_python_info([python_exe])
info = get_python_info(python_exe)
if info is not None:
found = PythonSpec(
info["name"],
Expand All @@ -51,9 +51,16 @@ def exe_spec(python_exe, base):
return _SPECS[python_exe]


_python_info_cache = {}


def get_python_info(cmd):
try:
return _python_info_cache[cmd].copy()
except KeyError:
pass
proc = subprocess.Popen(
cmd + [VERSION_QUERY_SCRIPT],
[cmd] + [VERSION_QUERY_SCRIPT],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
Expand All @@ -65,7 +72,8 @@ def get_python_info(cmd):
except ValueError as exception:
failure = exception
else:
return result
_python_info_cache[cmd] = result
return result.copy()
else:
failure = "exit code {}".format(proc.returncode)
reporter.verbosity1("{!r} cmd {!r} out {!r} err {!r} ".format(failure, cmd, out, err))
9 changes: 2 additions & 7 deletions src/tox/logs/env.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from __future__ import absolute_import, unicode_literals

import json
import subprocess

from tox.constants import VERSION_QUERY_SCRIPT
from tox.interpreters.via_path import get_python_info

from .command import CommandLog

Expand All @@ -17,9 +14,7 @@ def __init__(self, result_log, name, dict):
self.dict = dict

def set_python_info(self, python_executable):
cmd = [str(python_executable), VERSION_QUERY_SCRIPT]
result = subprocess.check_output(cmd, universal_newlines=True)
answer = json.loads(result)
answer = get_python_info(str(python_executable))
answer["executable"] = python_executable
self.dict["python"] = answer

Expand Down