Skip to content

Commit ab74c01

Browse files
authored
bpo-31904: Fix site and sysconfig modules for VxWorks RTOS (GH-21821)
1 parent c95f8bc commit ab74c01

File tree

7 files changed

+113
-72
lines changed

7 files changed

+113
-72
lines changed

Lib/distutils/command/install.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
from site import USER_BASE
1919
from site import USER_SITE
20-
HAS_USER_SITE = True
20+
21+
HAS_USER_SITE = (USER_SITE is not None)
2122

2223
WINDOWS_SCHEME = {
2324
'purelib': '$base/Lib/site-packages',
@@ -169,8 +170,9 @@ def initialize_options(self):
169170
self.install_lib = None # set to either purelib or platlib
170171
self.install_scripts = None
171172
self.install_data = None
172-
self.install_userbase = USER_BASE
173-
self.install_usersite = USER_SITE
173+
if HAS_USER_SITE:
174+
self.install_userbase = USER_BASE
175+
self.install_usersite = USER_SITE
174176

175177
self.compile = None
176178
self.optimize = None
@@ -343,8 +345,9 @@ def finalize_options(self):
343345
# Convert directories from Unix /-separated syntax to the local
344346
# convention.
345347
self.convert_paths('lib', 'purelib', 'platlib',
346-
'scripts', 'data', 'headers',
347-
'userbase', 'usersite')
348+
'scripts', 'data', 'headers')
349+
if HAS_USER_SITE:
350+
self.convert_paths('userbase', 'usersite')
348351

349352
# Deprecated
350353
# Well, we're not actually fully completely finalized yet: we still

Lib/distutils/tests/test_install.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from test.support import captured_stdout, run_unittest
99

1010
from distutils import sysconfig
11-
from distutils.command.install import install
11+
from distutils.command.install import install, HAS_USER_SITE
1212
from distutils.command import install as install_module
1313
from distutils.command.build_ext import build_ext
1414
from distutils.command.install import INSTALL_SCHEMES
@@ -66,6 +66,7 @@ def check_path(got, expected):
6666
check_path(cmd.install_scripts, os.path.join(destination, "bin"))
6767
check_path(cmd.install_data, destination)
6868

69+
@unittest.skipUnless(HAS_USER_SITE, 'need user site')
6970
def test_user_site(self):
7071
# test install with --user
7172
# preparing the environment for the test
@@ -93,8 +94,9 @@ def cleanup():
9394

9495
self.addCleanup(cleanup)
9596

96-
for key in ('nt_user', 'unix_user'):
97-
self.assertIn(key, INSTALL_SCHEMES)
97+
if HAS_USER_SITE:
98+
for key in ('nt_user', 'unix_user'):
99+
self.assertIn(key, INSTALL_SCHEMES)
98100

99101
dist = Distribution({'name': 'xx'})
100102
cmd = install(dist)

Lib/site.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ def _getuserbase():
264264
if env_base:
265265
return env_base
266266

267+
# VxWorks has no home directories
268+
if sys.platform == "vxworks":
269+
return None
270+
267271
def joinuser(*args):
268272
return os.path.expanduser(os.path.join(*args))
269273

@@ -311,11 +315,14 @@ def getusersitepackages():
311315
If the global variable ``USER_SITE`` is not initialized yet, this
312316
function will also set it.
313317
"""
314-
global USER_SITE
318+
global USER_SITE, ENABLE_USER_SITE
315319
userbase = getuserbase() # this will also set USER_BASE
316320

317321
if USER_SITE is None:
318-
USER_SITE = _get_path(userbase)
322+
if userbase is None:
323+
ENABLE_USER_SITE = False # disable user site and return None
324+
else:
325+
USER_SITE = _get_path(userbase)
319326

320327
return USER_SITE
321328

@@ -630,11 +637,14 @@ def _script():
630637
for dir in sys.path:
631638
print(" %r," % (dir,))
632639
print("]")
633-
print("USER_BASE: %r (%s)" % (user_base,
634-
"exists" if os.path.isdir(user_base) else "doesn't exist"))
635-
print("USER_SITE: %r (%s)" % (user_site,
636-
"exists" if os.path.isdir(user_site) else "doesn't exist"))
637-
print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE)
640+
def exists(path):
641+
if path is not None and os.path.isdir(path):
642+
return "exists"
643+
else:
644+
return "doesn't exist"
645+
print(f"USER_BASE: {user_base!r} ({exists(user_base)})")
646+
print(f"USER_SITE: {user_site!r} ({exists(user_site)})")
647+
print(f"ENABLE_USER_SITE: {ENABLE_USER_SITE!r}")
638648
sys.exit(0)
639649

640650
buffer = []

Lib/sysconfig.py

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,65 @@
5151
'scripts': '{base}/Scripts',
5252
'data': '{base}',
5353
},
54-
# NOTE: When modifying "purelib" scheme, update site._get_path() too.
55-
'nt_user': {
56-
'stdlib': '{userbase}/Python{py_version_nodot_plat}',
57-
'platstdlib': '{userbase}/Python{py_version_nodot_plat}',
58-
'purelib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
59-
'platlib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
60-
'include': '{userbase}/Python{py_version_nodot_plat}/Include',
61-
'scripts': '{userbase}/Python{py_version_nodot_plat}/Scripts',
62-
'data': '{userbase}',
63-
},
64-
'posix_user': {
65-
'stdlib': '{userbase}/{platlibdir}/python{py_version_short}',
66-
'platstdlib': '{userbase}/{platlibdir}/python{py_version_short}',
67-
'purelib': '{userbase}/lib/python{py_version_short}/site-packages',
68-
'platlib': '{userbase}/{platlibdir}/python{py_version_short}/site-packages',
69-
'include': '{userbase}/include/python{py_version_short}',
70-
'scripts': '{userbase}/bin',
71-
'data': '{userbase}',
72-
},
73-
'osx_framework_user': {
74-
'stdlib': '{userbase}/lib/python',
75-
'platstdlib': '{userbase}/lib/python',
76-
'purelib': '{userbase}/lib/python/site-packages',
77-
'platlib': '{userbase}/lib/python/site-packages',
78-
'include': '{userbase}/include',
79-
'scripts': '{userbase}/bin',
80-
'data': '{userbase}',
81-
},
54+
}
55+
56+
57+
# NOTE: site.py has copy of this function.
58+
# Sync it when modify this function.
59+
def _getuserbase():
60+
env_base = os.environ.get("PYTHONUSERBASE", None)
61+
if env_base:
62+
return env_base
63+
64+
# VxWorks has no home directories
65+
if sys.platform == "vxworks":
66+
return None
67+
68+
def joinuser(*args):
69+
return os.path.expanduser(os.path.join(*args))
70+
71+
if os.name == "nt":
72+
base = os.environ.get("APPDATA") or "~"
73+
return joinuser(base, "Python")
74+
75+
if sys.platform == "darwin" and sys._framework:
76+
return joinuser("~", "Library", sys._framework,
77+
"%d.%d" % sys.version_info[:2])
78+
79+
return joinuser("~", ".local")
80+
81+
_HAS_USER_BASE = (_getuserbase() is not None)
82+
83+
if _HAS_USER_BASE:
84+
_INSTALL_SCHEMES |= {
85+
# NOTE: When modifying "purelib" scheme, update site._get_path() too.
86+
'nt_user': {
87+
'stdlib': '{userbase}/Python{py_version_nodot_plat}',
88+
'platstdlib': '{userbase}/Python{py_version_nodot_plat}',
89+
'purelib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
90+
'platlib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
91+
'include': '{userbase}/Python{py_version_nodot_plat}/Include',
92+
'scripts': '{userbase}/Python{py_version_nodot_plat}/Scripts',
93+
'data': '{userbase}',
94+
},
95+
'posix_user': {
96+
'stdlib': '{userbase}/{platlibdir}/python{py_version_short}',
97+
'platstdlib': '{userbase}/{platlibdir}/python{py_version_short}',
98+
'purelib': '{userbase}/lib/python{py_version_short}/site-packages',
99+
'platlib': '{userbase}/{platlibdir}/python{py_version_short}/site-packages',
100+
'include': '{userbase}/include/python{py_version_short}',
101+
'scripts': '{userbase}/bin',
102+
'data': '{userbase}',
103+
},
104+
'osx_framework_user': {
105+
'stdlib': '{userbase}/lib/python',
106+
'platstdlib': '{userbase}/lib/python',
107+
'purelib': '{userbase}/lib/python/site-packages',
108+
'platlib': '{userbase}/lib/python/site-packages',
109+
'include': '{userbase}/include',
110+
'scripts': '{userbase}/bin',
111+
'data': '{userbase}',
112+
},
82113
}
83114

84115
_SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include',
@@ -183,25 +214,6 @@ def _get_default_scheme():
183214
return os.name
184215

185216

186-
# NOTE: site.py has copy of this function.
187-
# Sync it when modify this function.
188-
def _getuserbase():
189-
env_base = os.environ.get("PYTHONUSERBASE", None)
190-
if env_base:
191-
return env_base
192-
193-
def joinuser(*args):
194-
return os.path.expanduser(os.path.join(*args))
195-
196-
if os.name == "nt":
197-
base = os.environ.get("APPDATA") or "~"
198-
return joinuser(base, "Python")
199-
200-
if sys.platform == "darwin" and sys._framework:
201-
return joinuser("~", "Library", sys._framework,
202-
"%d.%d" % sys.version_info[:2])
203-
204-
return joinuser("~", ".local")
205217

206218

207219
def _parse_makefile(filename, vars=None):
@@ -558,10 +570,11 @@ def get_config_vars(*args):
558570
SO = _CONFIG_VARS.get('EXT_SUFFIX')
559571
if SO is not None:
560572
_CONFIG_VARS['SO'] = SO
561-
# Setting 'userbase' is done below the call to the
562-
# init function to enable using 'get_config_var' in
563-
# the init-function.
564-
_CONFIG_VARS['userbase'] = _getuserbase()
573+
if _HAS_USER_BASE:
574+
# Setting 'userbase' is done below the call to the
575+
# init function to enable using 'get_config_var' in
576+
# the init-function.
577+
_CONFIG_VARS['userbase'] = _getuserbase()
565578

566579
# Always convert srcdir to an absolute path
567580
srcdir = _CONFIG_VARS.get('srcdir', _PROJECT_BASE)

Lib/test/test_site.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import site
3737

3838

39+
HAS_USER_SITE = (site.USER_SITE is not None)
3940
OLD_SYS_PATH = None
4041

4142

@@ -195,6 +196,7 @@ def test_addsitedir(self):
195196
def test__getuserbase(self):
196197
self.assertEqual(site._getuserbase(), sysconfig._getuserbase())
197198

199+
@unittest.skipUnless(HAS_USER_SITE, 'need user site')
198200
def test_get_path(self):
199201
if sys.platform == 'darwin' and sys._framework:
200202
scheme = 'osx_framework_user'
@@ -244,6 +246,7 @@ def test_s_option(self):
244246
self.assertEqual(rc, 1,
245247
"User base not set by PYTHONUSERBASE")
246248

249+
@unittest.skipUnless(HAS_USER_SITE, 'need user site')
247250
def test_getuserbase(self):
248251
site.USER_BASE = None
249252
user_base = site.getuserbase()
@@ -261,6 +264,7 @@ def test_getuserbase(self):
261264
self.assertTrue(site.getuserbase().startswith('xoxo'),
262265
site.getuserbase())
263266

267+
@unittest.skipUnless(HAS_USER_SITE, 'need user site')
264268
def test_getusersitepackages(self):
265269
site.USER_SITE = None
266270
site.USER_BASE = None
@@ -295,6 +299,7 @@ def test_getsitepackages(self):
295299
wanted = os.path.join('xoxo', 'lib', 'site-packages')
296300
self.assertEqual(dirs[1], wanted)
297301

302+
@unittest.skipUnless(HAS_USER_SITE, 'need user site')
298303
def test_no_home_directory(self):
299304
# bpo-10496: getuserbase() and getusersitepackages() must not fail if
300305
# the current user has no home directory (if expanduser() returns the

Lib/test/test_sysconfig.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
get_scheme_names, get_config_var, _main)
1919
import _osx_support
2020

21+
22+
HAS_USER_BASE = sysconfig._HAS_USER_BASE
23+
24+
2125
class TestSysConfig(unittest.TestCase):
2226

2327
def setUp(self):
@@ -230,9 +234,10 @@ def test_get_config_h_filename(self):
230234
self.assertTrue(os.path.isfile(config_h), config_h)
231235

232236
def test_get_scheme_names(self):
233-
wanted = ('nt', 'nt_user', 'osx_framework_user',
234-
'posix_home', 'posix_prefix', 'posix_user')
235-
self.assertEqual(get_scheme_names(), wanted)
237+
wanted = ['nt', 'posix_home', 'posix_prefix']
238+
if HAS_USER_BASE:
239+
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
240+
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
236241

237242
@skip_unless_symlink
238243
def test_symlink(self): # Issue 7880
@@ -244,7 +249,8 @@ def test_user_similar(self):
244249
# Issue #8759: make sure the posix scheme for the users
245250
# is similar to the global posix_prefix one
246251
base = get_config_var('base')
247-
user = get_config_var('userbase')
252+
if HAS_USER_BASE:
253+
user = get_config_var('userbase')
248254
# the global scheme mirrors the distinction between prefix and
249255
# exec-prefix but not the user scheme, so we have to adapt the paths
250256
# before comparing (issue #9100)
@@ -259,8 +265,9 @@ def test_user_similar(self):
259265
# before comparing
260266
global_path = global_path.replace(sys.base_prefix, sys.prefix)
261267
base = base.replace(sys.base_prefix, sys.prefix)
262-
user_path = get_path(name, 'posix_user')
263-
self.assertEqual(user_path, global_path.replace(base, user, 1))
268+
if HAS_USER_BASE:
269+
user_path = get_path(name, 'posix_user')
270+
self.assertEqual(user_path, global_path.replace(base, user, 1))
264271

265272
def test_main(self):
266273
# just making sure _main() runs and returns things in the stdout
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix site and sysconfig modules for VxWorks RTOS which has no home directories.

0 commit comments

Comments
 (0)