Skip to content

Commit e40ce72

Browse files
committed
Include rustc version in the user agent, if rustc is available
Rust is becoming more popular for writing Python extension modules in, this information would be valuable for package maintainers to assess the ecosystem, in the same way glibc or openssl version is.
1 parent e6414d6 commit e40ce72

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/pip/_internal/network/session.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import mimetypes
2020
import os
2121
import platform
22+
import shutil
23+
import subprocess
2224
import sys
2325
import urllib.parse
2426
import warnings
@@ -163,6 +165,18 @@ def user_agent():
163165
if setuptools_dist is not None:
164166
data["setuptools_version"] = str(setuptools_dist.version)
165167

168+
if shutil.which("rustc") is not None:
169+
# If for any reason `rustc --version` fails, silently ignore it
170+
try:
171+
rustc_output = subprocess.check_output(["rustc", "--version"])
172+
except Exception:
173+
pass
174+
else:
175+
# The format of `rustc --version` is:
176+
# `b'rustc 1.52.1 (9bc8c42bb 2021-05-09)\n'`
177+
# We extract just the middle (1.52.1) part
178+
data["rustc_version"] = rustc_output.split(b" ")[1].decode()
179+
166180
# Use None rather than False so as not to give the impression that
167181
# pip knows it is not being run under CI. Rather, it is a null or
168182
# inconclusive result. Also, we include some value rather than no

tests/unit/test_req_file.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,14 @@ def make_var(name):
598598
with open(tmpdir.joinpath('req1.txt'), 'w') as fp:
599599
fp.write(template.format(*map(make_var, env_vars)))
600600

601+
session = PipSession()
601602
with patch('pip._internal.req.req_file.os.getenv') as getenv:
602603
getenv.side_effect = lambda n: env_vars[n]
603604

604605
reqs = list(parse_reqfile(
605606
tmpdir.joinpath('req1.txt'),
606607
finder=finder,
607-
session=PipSession()
608+
session=session
608609
))
609610

610611
assert len(reqs) == 1, \
@@ -623,13 +624,14 @@ def test_expand_missing_env_variables(self, tmpdir, finder):
623624
with open(tmpdir.joinpath('req1.txt'), 'w') as fp:
624625
fp.write(req_url)
625626

627+
session = PipSession()
626628
with patch('pip._internal.req.req_file.os.getenv') as getenv:
627629
getenv.return_value = ''
628630

629631
reqs = list(parse_reqfile(
630632
tmpdir.joinpath('req1.txt'),
631633
finder=finder,
632-
session=PipSession()
634+
session=session
633635
))
634636

635637
assert len(reqs) == 1, \

0 commit comments

Comments
 (0)