Skip to content

Commit e54aa21

Browse files
frenzymadnessencukouhroncokmcyprian
committed
00251: Change user install location
Change the values of sysconfig's "posix_prefix" install scheme to /usr/local when RPM build or venv/virtualenv is not detected, to make pip, sysconfig and distutils install into an isolated location. The original values are saved as an additional "rpm_prefix" install scheme. The site module adds the /usr/local paths to sys.path when site packages are enabled and RPM build is not detected. Fedora Change: https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe Rewrote in Fedora 36+ to patch sysconfig instead of distutils, see https://discuss.python.org/t/pep-632-deprecate-distutils-module/5134/104 Downstream only for now, waiting for https://bugs.python.org/issue43976 Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Miro Hrončok <[email protected]> Co-authored-by: Michal Cyprian <[email protected]>
1 parent 3cebdae commit e54aa21

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/site.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,15 @@ def getsitepackages(prefixes=None):
380380
return sitepackages
381381

382382
def addsitepackages(known_paths, prefixes=None):
383-
"""Add site-packages to sys.path"""
383+
"""Add site-packages to sys.path
384+
385+
'/usr/local' is included in PREFIXES if RPM build is not detected
386+
to make packages installed into this location visible.
387+
388+
"""
384389
_trace("Processing global site-packages")
390+
if ENABLE_USER_SITE and 'RPM_BUILD_ROOT' not in os.environ:
391+
PREFIXES.insert(0, "/usr/local")
385392
for sitedir in getsitepackages(prefixes):
386393
if os.path.isdir(sitedir):
387394
addsitedir(sitedir, known_paths)

Lib/sysconfig.py

+19
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@
5858
},
5959
}
6060

61+
# backup the original posix_prefix as rpm_prefix
62+
# RPM packages use it and we need to be able to read it even when changed
63+
_INSTALL_SCHEMES['rpm_prefix'] = _INSTALL_SCHEMES['posix_prefix']
64+
65+
if (not (hasattr(sys, 'real_prefix') or
66+
sys.prefix != sys.base_prefix) and
67+
'RPM_BUILD_ROOT' not in os.environ):
68+
_INSTALL_SCHEMES['posix_prefix'] = {
69+
'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
70+
'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
71+
'purelib': '{base}/local/lib/python{py_version_short}/site-packages',
72+
'platlib': '{platbase}/local/{platlibdir}/python{py_version_short}/site-packages',
73+
'include':
74+
'{installed_base}/include/python{py_version_short}{abiflags}',
75+
'platinclude':
76+
'{installed_platbase}/include/python{py_version_short}{abiflags}',
77+
'scripts': '{base}/local/bin',
78+
'data': '{base}/local',
79+
}
6180

6281
# NOTE: site.py has copy of this function.
6382
# Sync it when modify this function.

Lib/test/test_sysconfig.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def test_get_config_h_filename(self):
263263
self.assertTrue(os.path.isfile(config_h), config_h)
264264

265265
def test_get_scheme_names(self):
266-
wanted = ['nt', 'posix_home', 'posix_prefix']
266+
wanted = ['nt', 'posix_home', 'posix_prefix', 'rpm_prefix']
267267
if HAS_USER_BASE:
268268
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
269269
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
@@ -274,6 +274,8 @@ def test_symlink(self): # Issue 7880
274274
cmd = "-c", "import sysconfig; print(sysconfig.get_platform())"
275275
self.assertEqual(py.call_real(*cmd), py.call_link(*cmd))
276276

277+
@unittest.skipIf('RPM_BUILD_ROOT' not in os.environ,
278+
"Test doesn't expect Fedora's paths")
277279
def test_user_similar(self):
278280
# Issue #8759: make sure the posix scheme for the users
279281
# is similar to the global posix_prefix one

0 commit comments

Comments
 (0)