Skip to content

Commit 6a74205

Browse files
blueyedgaborbernat
authored andcommittedDec 2, 2019
use get_python_info for consistency and caching (#1462)
* use get_python_info for consistency and caching * changelog
1 parent 057894f commit 6a74205

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed
 

Diff for: ‎docs/changelog/1462.misc.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
improve performance with internal lookup of Python version information - by :user:`blueyed`

Diff for: ‎src/tox/interpreters/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import tox
77
from tox import reporter
8-
from tox.constants import SITE_PACKAGE_QUERY_SCRIPT, VERSION_QUERY_SCRIPT
8+
from tox.constants import SITE_PACKAGE_QUERY_SCRIPT
9+
from tox.interpreters.via_path import get_python_info
910

1011

1112
class Interpreters:
@@ -56,7 +57,7 @@ def get_sitepackagesdir(self, info, envdir):
5657
def run_and_get_interpreter_info(name, executable):
5758
assert executable
5859
try:
59-
result = exec_on_interpreter(str(executable), VERSION_QUERY_SCRIPT)
60+
result = get_python_info(str(executable))
6061
result["version_info"] = tuple(result["version_info"]) # fix json dump transformation
6162
del result["name"]
6263
del result["version"]

Diff for: ‎src/tox/interpreters/via_path.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def exe_spec(python_exe, base):
3535
python_exe = str(python_exe)
3636
with _SPECK_LOCK[python_exe]:
3737
if python_exe not in _SPECS:
38-
info = get_python_info([python_exe])
38+
info = get_python_info(python_exe)
3939
if info is not None:
4040
found = PythonSpec(
4141
info["name"],
@@ -51,9 +51,16 @@ def exe_spec(python_exe, base):
5151
return _SPECS[python_exe]
5252

5353

54+
_python_info_cache = {}
55+
56+
5457
def get_python_info(cmd):
58+
try:
59+
return _python_info_cache[cmd].copy()
60+
except KeyError:
61+
pass
5562
proc = subprocess.Popen(
56-
cmd + [VERSION_QUERY_SCRIPT],
63+
[cmd] + [VERSION_QUERY_SCRIPT],
5764
stdout=subprocess.PIPE,
5865
stderr=subprocess.PIPE,
5966
universal_newlines=True,
@@ -65,7 +72,8 @@ def get_python_info(cmd):
6572
except ValueError as exception:
6673
failure = exception
6774
else:
68-
return result
75+
_python_info_cache[cmd] = result
76+
return result.copy()
6977
else:
7078
failure = "exit code {}".format(proc.returncode)
7179
reporter.verbosity1("{!r} cmd {!r} out {!r} err {!r} ".format(failure, cmd, out, err))

Diff for: ‎src/tox/logs/env.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from __future__ import absolute_import, unicode_literals
22

3-
import json
4-
import subprocess
5-
6-
from tox.constants import VERSION_QUERY_SCRIPT
3+
from tox.interpreters.via_path import get_python_info
74

85
from .command import CommandLog
96

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

1916
def set_python_info(self, python_executable):
20-
cmd = [str(python_executable), VERSION_QUERY_SCRIPT]
21-
result = subprocess.check_output(cmd, universal_newlines=True)
22-
answer = json.loads(result)
17+
answer = get_python_info(str(python_executable))
2318
answer["executable"] = python_executable
2419
self.dict["python"] = answer
2520

0 commit comments

Comments
 (0)
Please sign in to comment.