Skip to content

Commit 74a4b48

Browse files
Simplify _pythoninfo.py (#153)
This rolls back some of the excess in my last PR.
1 parent a526d69 commit 74a4b48

File tree

8 files changed

+433
-537
lines changed

8 files changed

+433
-537
lines changed

pyperformance/_python.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generic helpers for working with a Python executable.
2+
3+
import hashlib
4+
5+
from pyperformance import _pythoninfo
6+
7+
8+
def get_id(python=None, prefix=None, *, short=True):
9+
"""Return a string that uniquely identifies the given Python executable."""
10+
if isinstance(python, str):
11+
python = _pythoninfo.get_info(python)
12+
13+
data = [
14+
# "executable" represents the install location
15+
# (and build, to an extent).
16+
python.sys.executable,
17+
# sys.version encodes version, git info, build_date, and build_tool.
18+
python.sys.version,
19+
python.sys.implementation.name.lower(),
20+
'.'.join(str(v) for v in python.sys.implementation.version),
21+
str(python.sys.api_version),
22+
python.pyc_magic_number.hex(),
23+
]
24+
# XXX Add git info if a dev build.
25+
26+
h = hashlib.sha256()
27+
for value in data:
28+
h.update(value.encode('utf-8'))
29+
# XXX Also include the sorted output of "python -m pip freeze"?
30+
py_id = h.hexdigest()
31+
if short:
32+
py_id = py_id[:12]
33+
34+
if prefix:
35+
if prefix is True:
36+
major, minor = python.sys.version_info[:2]
37+
py_id = f'{python.sys.implementation.name}{major}.{minor}-{py_id}'
38+
else:
39+
py_id = prefix + py_id
40+
41+
return py_id

0 commit comments

Comments
 (0)