73
73
import os
74
74
import builtins
75
75
import _sitebuiltins
76
+ import io
76
77
77
78
# Prefixes for site-packages; add additional prefixes like /usr/local here
78
79
PREFIXES = [sys .prefix , sys .exec_prefix ]
87
88
USER_BASE = None
88
89
89
90
91
+ def _trace (message ):
92
+ if sys .flags .verbose :
93
+ print (message , file = sys .stderr )
94
+
95
+
90
96
def makepath (* paths ):
91
97
dir = os .path .join (* paths )
92
98
try :
@@ -99,8 +105,15 @@ def makepath(*paths):
99
105
def abs_paths ():
100
106
"""Set all module __file__ and __cached__ attributes to an absolute path"""
101
107
for m in set (sys .modules .values ()):
102
- if (getattr (getattr (m , '__loader__' , None ), '__module__' , None ) not in
103
- ('_frozen_importlib' , '_frozen_importlib_external' )):
108
+ loader_module = None
109
+ try :
110
+ loader_module = m .__loader__ .__module__
111
+ except AttributeError :
112
+ try :
113
+ loader_module = m .__spec__ .loader .__module__
114
+ except AttributeError :
115
+ pass
116
+ if loader_module not in {'_frozen_importlib' , '_frozen_importlib_external' }:
104
117
continue # don't mess with a PEP 302-supplied __file__
105
118
try :
106
119
m .__file__ = os .path .abspath (m .__file__ )
@@ -155,14 +168,19 @@ def addpackage(sitedir, name, known_paths):
155
168
else :
156
169
reset = False
157
170
fullname = os .path .join (sitedir , name )
171
+ _trace (f"Processing .pth file: { fullname !r} " )
158
172
try :
159
- f = open (fullname , "r" )
173
+ # locale encoding is not ideal especially on Windows. But we have used
174
+ # it for a long time. setuptools uses the locale encoding too.
175
+ f = io .TextIOWrapper (io .open_code (fullname ), encoding = "locale" )
160
176
except OSError :
161
177
return
162
178
with f :
163
179
for n , line in enumerate (f ):
164
180
if line .startswith ("#" ):
165
181
continue
182
+ if line .strip () == "" :
183
+ continue
166
184
try :
167
185
if line .startswith (("import " , "import\t " )):
168
186
exec (line )
@@ -189,6 +207,7 @@ def addpackage(sitedir, name, known_paths):
189
207
def addsitedir (sitedir , known_paths = None ):
190
208
"""Add 'sitedir' argument to sys.path if missing and handle .pth files in
191
209
'sitedir'"""
210
+ _trace (f"Adding directory: { sitedir !r} " )
192
211
if known_paths is None :
193
212
known_paths = _init_pathinfo ()
194
213
reset = True
@@ -247,6 +266,10 @@ def _getuserbase():
247
266
if env_base :
248
267
return env_base
249
268
269
+ # VxWorks has no home directories
270
+ if sys .platform == "vxworks" :
271
+ return None
272
+
250
273
def joinuser (* args ):
251
274
return os .path .expanduser (os .path .join (* args ))
252
275
@@ -265,9 +288,9 @@ def joinuser(*args):
265
288
def _get_path (userbase ):
266
289
version = sys .version_info
267
290
268
- # XXX RUSTPYTHON: we replace pythonx.y with rustpythonx.y
269
291
if os .name == 'nt' :
270
- return f'{ userbase } \\ RustPython{ version [0 ]} { version [1 ]} \\ site-packages'
292
+ ver_nodot = sys .winver .replace ('.' , '' )
293
+ return f'{ userbase } \\ RustPython{ ver_nodot } \\ site-packages'
271
294
272
295
if sys .platform == 'darwin' and sys ._framework :
273
296
return f'{ userbase } /lib/rustpython/site-packages'
@@ -294,11 +317,14 @@ def getusersitepackages():
294
317
If the global variable ``USER_SITE`` is not initialized yet, this
295
318
function will also set it.
296
319
"""
297
- global USER_SITE
320
+ global USER_SITE , ENABLE_USER_SITE
298
321
userbase = getuserbase () # this will also set USER_BASE
299
322
300
323
if USER_SITE is None :
301
- USER_SITE = _get_path (userbase )
324
+ if userbase is None :
325
+ ENABLE_USER_SITE = False # disable user site and return None
326
+ else :
327
+ USER_SITE = _get_path (userbase )
302
328
303
329
return USER_SITE
304
330
@@ -310,6 +336,7 @@ def addusersitepackages(known_paths):
310
336
"""
311
337
# get the per user site-package path
312
338
# this call will also make sure USER_BASE and USER_SITE are set
339
+ _trace ("Processing user site-packages" )
313
340
user_site = getusersitepackages ()
314
341
315
342
if ENABLE_USER_SITE and os .path .isdir (user_site ):
@@ -334,18 +361,27 @@ def getsitepackages(prefixes=None):
334
361
continue
335
362
seen .add (prefix )
336
363
364
+ libdirs = [sys .platlibdir ]
365
+ if sys .platlibdir != "lib" :
366
+ libdirs .append ("lib" )
367
+
337
368
if os .sep == '/' :
338
- sitepackages .append (os .path .join (prefix , "lib" ,
339
- # XXX changed for RustPython
340
- "rustpython%d.%d" % sys .version_info [:2 ],
341
- "site-packages" ))
369
+ for libdir in libdirs :
370
+ path = os .path .join (prefix , libdir ,
371
+ "python%d.%d" % sys .version_info [:2 ],
372
+ "site-packages" )
373
+ sitepackages .append (path )
342
374
else :
343
375
sitepackages .append (prefix )
344
- sitepackages .append (os .path .join (prefix , "lib" , "site-packages" ))
376
+
377
+ for libdir in libdirs :
378
+ path = os .path .join (prefix , libdir , "site-packages" )
379
+ sitepackages .append (path )
345
380
return sitepackages
346
381
347
382
def addsitepackages (known_paths , prefixes = None ):
348
383
"""Add site-packages to sys.path"""
384
+ _trace ("Processing global site-packages" )
349
385
for sitedir in getsitepackages (prefixes ):
350
386
if os .path .isdir (sitedir ):
351
387
addsitedir (sitedir , known_paths )
@@ -441,7 +477,16 @@ def register_readline():
441
477
readline .read_history_file (history )
442
478
except OSError :
443
479
pass
444
- atexit .register (readline .write_history_file , history )
480
+
481
+ def write_history ():
482
+ try :
483
+ readline .write_history_file (history )
484
+ except OSError :
485
+ # bpo-19891, bpo-41193: Home directory does not exist
486
+ # or is not writable, or the filesystem is read-only.
487
+ pass
488
+
489
+ atexit .register (write_history )
445
490
446
491
sys .__interactivehook__ = register_readline
447
492
@@ -450,7 +495,7 @@ def venv(known_paths):
450
495
451
496
env = os .environ
452
497
if sys .platform == 'darwin' and '__PYVENV_LAUNCHER__' in env :
453
- executable = os .environ ['__PYVENV_LAUNCHER__' ]
498
+ executable = sys . _base_executable = os .environ ['__PYVENV_LAUNCHER__' ]
454
499
else :
455
500
executable = sys .executable
456
501
exe_dir , _ = os .path .split (os .path .abspath (executable ))
@@ -582,7 +627,7 @@ def _script():
582
627
Exit codes with --user-base or --user-site:
583
628
0 - user site directory is enabled
584
629
1 - user site directory is disabled by user
585
- 2 - uses site directory is disabled by super user
630
+ 2 - user site directory is disabled by super user
586
631
or for security reasons
587
632
>2 - unknown error
588
633
"""
@@ -594,11 +639,14 @@ def _script():
594
639
for dir in sys .path :
595
640
print (" %r," % (dir ,))
596
641
print ("]" )
597
- print ("USER_BASE: %r (%s)" % (user_base ,
598
- "exists" if os .path .isdir (user_base ) else "doesn't exist" ))
599
- print ("USER_SITE: %r (%s)" % (user_site ,
600
- "exists" if os .path .isdir (user_site ) else "doesn't exist" ))
601
- print ("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE )
642
+ def exists (path ):
643
+ if path is not None and os .path .isdir (path ):
644
+ return "exists"
645
+ else :
646
+ return "doesn't exist"
647
+ print (f"USER_BASE: { user_base !r} ({ exists (user_base )} )" )
648
+ print (f"USER_SITE: { user_site !r} ({ exists (user_site )} )" )
649
+ print (f"ENABLE_USER_SITE: { ENABLE_USER_SITE !r} " )
602
650
sys .exit (0 )
603
651
604
652
buffer = []
0 commit comments