Debian Codegraph @

Lib/test/test_site.py

raw ◀ ▶
1 Debian patch touch this file
1 """Tests for 'site'.
2
3 Tests assume the initial paths in sys.path once the interpreter has begun
4 executing have not been removed.
5
6 """
7 import unittest
8 import test.support
9 from test import support
10 from test.support.script_helper import assert_python_ok
11 from test.support import import_helper
12 from test.support import os_helper
13 from test.support import socket_helper
14 from test.support import captured_stderr
15 from test.support.os_helper import TESTFN, EnvironmentVarGuard
16 from test.support.script_helper import spawn_python, kill_python
17 import ast
18 import builtins
19 import glob
20 import io
21 import os
22 import re
23 import shutil
24 import stat
25 import subprocess
26 import sys
27 import sysconfig
28 import tempfile
29 from textwrap import dedent
30 import urllib.error
31 import urllib.request
32 from unittest import mock
33 from copy import copy
34
35 # These tests are not particularly useful if Python was invoked with -S.
36 # If you add tests that are useful under -S, this skip should be moved
37 # to the class level.
38 if sys.flags.no_site:
39 raise unittest.SkipTest("Python was invoked with -S")
40
41 import site
42
43
44 HAS_USER_SITE = (site.USER_SITE is not None)
45 OLD_SYS_PATH = None
46
47
48 def setUpModule():
49 global OLD_SYS_PATH
50 OLD_SYS_PATH = sys.path[:]
51
52 if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
53 # need to add user site directory for tests
54 try:
55 os.makedirs(site.USER_SITE)
56 # modify sys.path: will be restored by tearDownModule()
57 site.addsitedir(site.USER_SITE)
58 except PermissionError as exc:
59 raise unittest.SkipTest('unable to create user site directory (%r): %s'
60 % (site.USER_SITE, exc))
61
62
63 def tearDownModule():
64 sys.path[:] = OLD_SYS_PATH
65
66
67 class HelperFunctionsTests(unittest.TestCase):
68 """Tests for helper functions.
69 """
70
71 def setUp(self):
72 """Save a copy of sys.path"""
73 self.sys_path = sys.path[:]
74 self.old_base = site.USER_BASE
75 self.old_site = site.USER_SITE
76 self.old_prefixes = site.PREFIXES
77 self.original_vars = sysconfig._CONFIG_VARS
78 self.old_vars = copy(sysconfig._CONFIG_VARS)
79
80 def tearDown(self):
81 """Restore sys.path"""
82 sys.path[:] = self.sys_path
83 site.USER_BASE = self.old_base
84 site.USER_SITE = self.old_site
85 site.PREFIXES = self.old_prefixes
86 sysconfig._CONFIG_VARS = self.original_vars
87 # _CONFIG_VARS is None before get_config_vars() is called
88 if sysconfig._CONFIG_VARS is not None:
89 sysconfig._CONFIG_VARS.clear()
90 sysconfig._CONFIG_VARS.update(self.old_vars)
91
92 def test_makepath(self):
93 # Test makepath() have an absolute path for its first return value
94 # and a case-normalized version of the absolute path for its
95 # second value.
96 path_parts = ("Beginning", "End")
97 original_dir = os.path.join(*path_parts)
98 abs_dir, norm_dir = site.makepath(*path_parts)
99 self.assertEqual(os.path.abspath(original_dir), abs_dir)
100 if original_dir == os.path.normcase(original_dir):
101 self.assertEqual(abs_dir, norm_dir)
102 else:
103 self.assertEqual(os.path.normcase(abs_dir), norm_dir)
104
105 def test_init_pathinfo(self):
106 dir_set = site._init_pathinfo()
107 for entry in [site.makepath(path)[1] for path in sys.path
108 if path and os.path.exists(path)]:
109 self.assertIn(entry, dir_set,
110 "%s from sys.path not found in set returned "
111 "by _init_pathinfo(): %s" % (entry, dir_set))
112
113 def pth_file_tests(self, pth_file):
114 """Contain common code for testing results of reading a .pth file"""
115 self.assertIn(pth_file.imported, sys.modules,
116 "%s not in sys.modules" % pth_file.imported)
117 self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
118 self.assertFalse(os.path.exists(pth_file.bad_dir_path))
119
120 def test_addpackage(self):
121 # Make sure addpackage() imports if the line starts with 'import',
122 # adds directories to sys.path for any line in the file that is not a
123 # comment or import that is a valid directory name for where the .pth
124 # file resides; invalid directories are not added
125 pth_file = PthFile()
126 pth_file.cleanup(prep=True) # to make sure that nothing is
127 # pre-existing that shouldn't be
128 try:
129 pth_file.create()
130 site.addpackage(pth_file.base_dir, pth_file.filename, set())
131 self.pth_file_tests(pth_file)
132 finally:
133 pth_file.cleanup()
134
135 def make_pth(self, contents, pth_dir='.', pth_name=TESTFN):
136 # Create a .pth file and return its (abspath, basename).
137 pth_dir = os.path.abspath(pth_dir)
138 pth_basename = pth_name + '.pth'
139 pth_fn = os.path.join(pth_dir, pth_basename)
140 with open(pth_fn, 'w', encoding='utf-8') as pth_file:
141 self.addCleanup(lambda: os.remove(pth_fn))
142 pth_file.write(contents)
143 return pth_dir, pth_basename
144
145 def test_addpackage_import_bad_syntax(self):
146 # Issue 10642
147 pth_dir, pth_fn = self.make_pth("import bad-syntax\n")
148 with captured_stderr() as err_out:
149 site.addpackage(pth_dir, pth_fn, set())
150 self.assertRegex(err_out.getvalue(), "line 1")
151 self.assertRegex(err_out.getvalue(),
152 re.escape(os.path.join(pth_dir, pth_fn)))
153 # XXX: the previous two should be independent checks so that the
154 # order doesn't matter. The next three could be a single check
155 # but my regex foo isn't good enough to write it.
156 self.assertRegex(err_out.getvalue(), 'Traceback')
157 self.assertRegex(err_out.getvalue(), r'import bad-syntax')
158 self.assertRegex(err_out.getvalue(), 'SyntaxError')
159
160 def test_addpackage_import_bad_exec(self):
161 # Issue 10642
162 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
163 with captured_stderr() as err_out:
164 site.addpackage(pth_dir, pth_fn, set())
165 self.assertRegex(err_out.getvalue(), "line 2")
166 self.assertRegex(err_out.getvalue(),
167 re.escape(os.path.join(pth_dir, pth_fn)))
168 # XXX: ditto previous XXX comment.
169 self.assertRegex(err_out.getvalue(), 'Traceback')
170 self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
171
172 def test_addpackage_empty_lines(self):
173 # Issue 33689
174 pth_dir, pth_fn = self.make_pth("\n\n \n\n")
175 known_paths = site.addpackage(pth_dir, pth_fn, set())
176 self.assertEqual(known_paths, set())
177
178 def test_addpackage_import_bad_pth_file(self):
179 # Issue 5258
180 pth_dir, pth_fn = self.make_pth("abc\x00def\n")
181 with captured_stderr() as err_out:
182 self.assertFalse(site.addpackage(pth_dir, pth_fn, set()))
183 self.maxDiff = None
184 self.assertEqual(err_out.getvalue(), "")
185 for path in sys.path:
186 if isinstance(path, str):
187 self.assertNotIn("abc\x00def", path)
188
189 def test_addsitedir(self):
190 # Same tests for test_addpackage since addsitedir() essentially just
191 # calls addpackage() for every .pth file in the directory
192 pth_file = PthFile()
193 pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
194 # that is tested for
195 try:
196 pth_file.create()
197 site.addsitedir(pth_file.base_dir, set())
198 self.pth_file_tests(pth_file)
199 finally:
200 pth_file.cleanup()
201
202 def test_addsitedir_dotfile(self):
203 pth_file = PthFile('.dotfile')
204 pth_file.cleanup(prep=True)
205 try:
206 pth_file.create()
207 site.addsitedir(pth_file.base_dir, set())
208 self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
209 self.assertIn(pth_file.base_dir, sys.path)
210 finally:
211 pth_file.cleanup()
212
213 @unittest.skipUnless(hasattr(os, 'chflags'), 'test needs os.chflags()')
214 def test_addsitedir_hidden_flags(self):
215 pth_file = PthFile()
216 pth_file.cleanup(prep=True)
217 try:
218 pth_file.create()
219 st = os.stat(pth_file.file_path)
220 os.chflags(pth_file.file_path, st.st_flags | stat.UF_HIDDEN)
221 site.addsitedir(pth_file.base_dir, set())
222 self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
223 self.assertIn(pth_file.base_dir, sys.path)
224 finally:
225 pth_file.cleanup()
226
227 @unittest.skipUnless(sys.platform == 'win32', 'test needs Windows')
228 @support.requires_subprocess()
229 def test_addsitedir_hidden_file_attribute(self):
230 pth_file = PthFile()
231 pth_file.cleanup(prep=True)
232 try:
233 pth_file.create()
234 subprocess.check_call(['attrib', '+H', pth_file.file_path])
235 site.addsitedir(pth_file.base_dir, set())
236 self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
237 self.assertIn(pth_file.base_dir, sys.path)
238 finally:
239 pth_file.cleanup()
240
241 # This tests _getuserbase, hence the double underline
242 # to distinguish from a test for getuserbase
243 def test__getuserbase(self):
244 self.assertEqual(site._getuserbase(), sysconfig._getuserbase())
245
246 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
247 def test_get_path(self):
248 if sys.platform == 'darwin' and sys._framework:
249 scheme = 'osx_framework_user'
250 else:
251 scheme = os.name + '_user'
252 self.assertEqual(os.path.normpath(site._get_path(site._getuserbase())),
253 sysconfig.get_path('purelib', scheme))
254
255 @unittest.skipUnless(site.ENABLE_USER_SITE, "requires access to PEP 370 "
256 "user-site (site.ENABLE_USER_SITE)")
257 @support.requires_subprocess()
258 def test_s_option(self):
259 # (ncoghlan) Change this to use script_helper...
260 usersite = os.path.normpath(site.USER_SITE)
261 self.assertIn(usersite, sys.path)
262
263 env = os.environ.copy()
264 rc = subprocess.call([sys.executable, '-c',
265 'import sys; sys.exit(%r in sys.path)' % usersite],
266 env=env)
267 self.assertEqual(rc, 1)
268
269 env = os.environ.copy()
270 rc = subprocess.call([sys.executable, '-s', '-c',
271 'import sys; sys.exit(%r in sys.path)' % usersite],
272 env=env)
273 if usersite == site.getsitepackages()[0]:
274 self.assertEqual(rc, 1)
275 else:
276 self.assertEqual(rc, 0, "User site still added to path with -s")
277
278 env = os.environ.copy()
279 env["PYTHONNOUSERSITE"] = "1"
280 rc = subprocess.call([sys.executable, '-c',
281 'import sys; sys.exit(%r in sys.path)' % usersite],
282 env=env)
283 if usersite == site.getsitepackages()[0]:
284 self.assertEqual(rc, 1)
285 else:
286 self.assertEqual(rc, 0,
287 "User site still added to path with PYTHONNOUSERSITE")
288
289 env = os.environ.copy()
290 env["PYTHONUSERBASE"] = "/tmp"
291 rc = subprocess.call([sys.executable, '-c',
292 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
293 env=env)
294 self.assertEqual(rc, 1,
295 "User base not set by PYTHONUSERBASE")
296
297 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
298 def test_getuserbase(self):
299 site.USER_BASE = None
300 user_base = site.getuserbase()
301
302 # the call sets site.USER_BASE
303 self.assertEqual(site.USER_BASE, user_base)
304
305 # let's set PYTHONUSERBASE and see if it uses it
306 site.USER_BASE = None
307 import sysconfig
308 sysconfig._CONFIG_VARS = None
309
310 with EnvironmentVarGuard() as environ:
311 environ['PYTHONUSERBASE'] = 'xoxo'
312 self.assertStartsWith(site.getuserbase(), 'xoxo')
313
314 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
315 def test_getusersitepackages(self):
316 site.USER_SITE = None
317 site.USER_BASE = None
318 user_site = site.getusersitepackages()
319
320 # the call sets USER_BASE *and* USER_SITE
321 self.assertEqual(site.USER_SITE, user_site)
322 self.assertStartsWith(user_site, site.USER_BASE)
323 self.assertEqual(site.USER_BASE, site.getuserbase())
324
325 def test_getsitepackages(self):
326 site.PREFIXES = ['xoxo']
327 dirs = site.getsitepackages()
328 if os.sep == '/':
329 # OS X, Linux, FreeBSD, etc
330 if sys.platlibdir != "lib":
•331 self.assertEqual(len(dirs), 3)
332 wanted = os.path.join('xoxo', 'local', 'lib',
333 f'python{sysconfig._get_python_version_abi()}',
•334 'dist-packages')
335 self.assertEqual(dirs[0], wanted)
336 else:
•337 self.assertEqual(len(dirs), 3)
338 wanted = os.path.join('xoxo', 'lib',
339 f'python{sysconfig._get_python_version_abi()}',
•340 'dist-packages')
341 self.assertEqual(dirs[-1], wanted)
342 else:
343 # other platforms
344 self.assertEqual(len(dirs), 2)
345 self.assertEqual(dirs[0], 'xoxo')
346 wanted = os.path.join('xoxo', 'lib', 'site-packages')
347 self.assertEqual(os.path.normcase(dirs[1]),
348 os.path.normcase(wanted))
349
350 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
351 def test_no_home_directory(self):
352 # bpo-10496: getuserbase() and getusersitepackages() must not fail if
353 # the current user has no home directory (if expanduser() returns the
354 # path unchanged).
355 site.USER_SITE = None
356 site.USER_BASE = None
357
358 with EnvironmentVarGuard() as environ, \
359 mock.patch('os.path.expanduser', lambda path: path):
360 environ.unset('PYTHONUSERBASE', 'APPDATA')
361
362 user_base = site.getuserbase()
363 self.assertStartsWith(user_base, '~' + os.sep)
364
365 user_site = site.getusersitepackages()
366 self.assertStartsWith(user_site, user_base)
367
368 with mock.patch('os.path.isdir', return_value=False) as mock_isdir, \
369 mock.patch.object(site, 'addsitedir') as mock_addsitedir, \
370 support.swap_attr(site, 'ENABLE_USER_SITE', True):
371
372 # addusersitepackages() must not add user_site to sys.path
373 # if it is not an existing directory
374 known_paths = set()
375 site.addusersitepackages(known_paths)
376
377 mock_isdir.assert_called_once_with(user_site)
378 mock_addsitedir.assert_not_called()
379 self.assertFalse(known_paths)
380
381 def test_gethistoryfile(self):
382 filename = 'file'
383 rc, out, err = assert_python_ok('-c',
384 f'import site; assert site.gethistoryfile() == "{filename}"',
385 PYTHON_HISTORY=filename)
386 self.assertEqual(rc, 0)
387
388 # Check that PYTHON_HISTORY is ignored in isolated mode.
389 rc, out, err = assert_python_ok('-I', '-c',
390 f'import site; assert site.gethistoryfile() != "{filename}"',
391 PYTHON_HISTORY=filename)
392 self.assertEqual(rc, 0)
393
394 def test_trace(self):
395 message = "bla-bla-bla"
396 for verbose, out in (True, message + "\n"), (False, ""):
397 with mock.patch('sys.flags', mock.Mock(verbose=verbose)), \
398 mock.patch('sys.stderr', io.StringIO()):
399 site._trace(message)
400 self.assertEqual(sys.stderr.getvalue(), out)
401
402
403 class PthFile(object):
404 """Helper class for handling testing of .pth files"""
405
406 def __init__(self, filename_base=TESTFN, imported="time",
407 good_dirname="__testdir__", bad_dirname="__bad"):
408 """Initialize instance variables"""
409 self.filename = filename_base + ".pth"
410 self.base_dir = os.path.abspath('')
411 self.file_path = os.path.join(self.base_dir, self.filename)
412 self.imported = imported
413 self.good_dirname = good_dirname
414 self.bad_dirname = bad_dirname
415 self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
416 self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
417
418 def create(self):
419 """Create a .pth file with a comment, blank lines, an ``import
420 <self.imported>``, a line with self.good_dirname, and a line with
421 self.bad_dirname.
422
423 Creation of the directory for self.good_dir_path (based off of
424 self.good_dirname) is also performed.
425
426 Make sure to call self.cleanup() to undo anything done by this method.
427
428 """
429 FILE = open(self.file_path, 'w')
430 try:
431 print("#import @bad module name", file=FILE)
432 print("\n", file=FILE)
433 print("import %s" % self.imported, file=FILE)
434 print(self.good_dirname, file=FILE)
435 print(self.bad_dirname, file=FILE)
436 finally:
437 FILE.close()
438 os.mkdir(self.good_dir_path)
439
440 def cleanup(self, prep=False):
441 """Make sure that the .pth file is deleted, self.imported is not in
442 sys.modules, and that both self.good_dirname and self.bad_dirname are
443 not existing directories."""
444 if os.path.exists(self.file_path):
445 os.remove(self.file_path)
446 if prep:
447 self.imported_module = sys.modules.get(self.imported)
448 if self.imported_module:
449 del sys.modules[self.imported]
450 else:
451 if self.imported_module:
452 sys.modules[self.imported] = self.imported_module
453 if os.path.exists(self.good_dir_path):
454 os.rmdir(self.good_dir_path)
455 if os.path.exists(self.bad_dir_path):
456 os.rmdir(self.bad_dir_path)
457
458 class ImportSideEffectTests(unittest.TestCase):
459 """Test side-effects from importing 'site'."""
460
461 def setUp(self):
462 """Make a copy of sys.path"""
463 self.sys_path = sys.path[:]
464
465 def tearDown(self):
466 """Restore sys.path"""
467 sys.path[:] = self.sys_path
468
469 def test_abs_paths_cached_None(self):
470 """Test for __cached__ is None.
471
472 Regarding to PEP 3147, __cached__ can be None.
473
474 See also: https://bugs.python.org/issue30167
475 """
476 sys.modules['test'].__cached__ = None
477 site.abs_paths()
478 self.assertIsNone(sys.modules['test'].__cached__)
479
480 def test_no_duplicate_paths(self):
481 # No duplicate paths should exist in sys.path
482 # Handled by removeduppaths()
483 site.removeduppaths()
484 seen_paths = set()
485 for path in sys.path:
486 self.assertNotIn(path, seen_paths)
487 seen_paths.add(path)
488
489 @unittest.skip('test not implemented')
490 def test_add_build_dir(self):
491 # Test that the build directory's Modules directory is used when it
492 # should be.
493 # XXX: implement
494 pass
495
496 def test_setting_quit(self):
497 # 'quit' and 'exit' should be injected into builtins
498 self.assertHasAttr(builtins, "quit")
499 self.assertHasAttr(builtins, "exit")
500
501 def test_setting_copyright(self):
502 # 'copyright', 'credits', and 'license' should be in builtins
503 self.assertHasAttr(builtins, "copyright")
504 self.assertHasAttr(builtins, "credits")
505 self.assertHasAttr(builtins, "license")
506
507 def test_setting_help(self):
508 # 'help' should be set in builtins
509 self.assertHasAttr(builtins, "help")
510
511 def test_sitecustomize_executed(self):
512 # If sitecustomize is available, it should have been imported.
513 if "sitecustomize" not in sys.modules:
514 try:
515 import sitecustomize # noqa: F401
516 except ImportError:
517 pass
518 else:
519 self.fail("sitecustomize not imported automatically")
520
521 @support.requires_subprocess()
522 def test_customization_modules_on_startup(self):
523 mod_names = [
524 'sitecustomize'
525 ]
526
527 if site.ENABLE_USER_SITE:
528 mod_names.append('usercustomize')
529
530 temp_dir = tempfile.mkdtemp()
531 self.addCleanup(os_helper.rmtree, temp_dir)
532
533 with EnvironmentVarGuard() as environ:
534 environ['PYTHONPATH'] = temp_dir
535
536 for module_name in mod_names:
537 os_helper.rmtree(temp_dir)
538 os.mkdir(temp_dir)
539
540 customize_path = os.path.join(temp_dir, f'{module_name}.py')
541 eyecatcher = f'EXECUTED_{module_name}'
542
543 with open(customize_path, 'w') as f:
544 f.write(f'print("{eyecatcher}")')
545
546 output = subprocess.check_output([sys.executable, '-c', '""'])
547 self.assertIn(eyecatcher, output.decode('utf-8'))
548
549 # -S blocks any site-packages
550 output = subprocess.check_output([sys.executable, '-S', '-c', '""'])
551 self.assertNotIn(eyecatcher, output.decode('utf-8'))
552
553 # -s blocks user site-packages
554 if 'usercustomize' == module_name:
555 output = subprocess.check_output([sys.executable, '-s', '-c', '""'])
556 self.assertNotIn(eyecatcher, output.decode('utf-8'))
557
558
559 @unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"),
560 'need SSL support to download license')
561 @test.support.requires_resource('network')
562 @test.support.system_must_validate_cert
563 def test_license_exists_at_url(self):
564 # This test is a bit fragile since it depends on the format of the
565 # string displayed by license in the absence of a LICENSE file.
566 url = license._Printer__data.split()[1]
567 req = urllib.request.Request(url, method='HEAD')
568 # Reset global urllib.request._opener
569 self.addCleanup(urllib.request.urlcleanup)
570 try:
571 with socket_helper.transient_internet(url):
572 with urllib.request.urlopen(req) as data:
573 code = data.getcode()
574 except urllib.error.HTTPError as e:
575 code = e.code
576 self.assertEqual(code, 200, msg="Can't find " + url)
577
578 @support.cpython_only
579 def test_lazy_imports(self):
580 import_helper.ensure_lazy_imports("site", [
581 "io",
582 "locale",
583 "traceback",
584 "atexit",
585 "warnings",
586 "textwrap",
587 ])
588
589
590 class StartupImportTests(unittest.TestCase):
591
592 @support.requires_subprocess()
593 def test_startup_imports(self):
594 # Get sys.path in isolated mode (python3 -I)
595 popen = subprocess.Popen([sys.executable, '-X', 'utf8', '-I',
596 '-c', 'import sys; print(repr(sys.path))'],
597 stdout=subprocess.PIPE,
598 encoding='utf-8',
599 errors='surrogateescape')
600 stdout = popen.communicate()[0]
601 self.assertEqual(popen.returncode, 0, repr(stdout))
602 isolated_paths = ast.literal_eval(stdout)
603
604 # bpo-27807: Even with -I, the site module executes all .pth files
605 # found in sys.path (see site.addpackage()). Skip the test if at least
606 # one .pth file is found.
607 for path in isolated_paths:
608 pth_files = glob.glob(os.path.join(glob.escape(path), "*.pth"))
609 if pth_files:
610 self.skipTest(f"found {len(pth_files)} .pth files in: {path}")
611
612 # This tests checks which modules are loaded by Python when it
613 # initially starts upon startup.
614 popen = subprocess.Popen([sys.executable, '-X', 'utf8', '-I', '-v',
615 '-c', 'import sys; print(set(sys.modules))'],
616 stdout=subprocess.PIPE,
617 stderr=subprocess.PIPE,
618 encoding='utf-8',
619 errors='surrogateescape')
620 stdout, stderr = popen.communicate()
621 self.assertEqual(popen.returncode, 0, (stdout, stderr))
622 modules = ast.literal_eval(stdout)
623
624 self.assertIn('site', modules)
625
626 # http://bugs.python.org/issue19205
627 re_mods = {'re', '_sre', 're._compiler', 're._constants', 're._parser'}
628 self.assertFalse(modules.intersection(re_mods), stderr)
629
630 # http://bugs.python.org/issue9548
631 self.assertNotIn('locale', modules, stderr)
632
633 # http://bugs.python.org/issue19209
634 self.assertNotIn('copyreg', modules, stderr)
635
636 # http://bugs.python.org/issue19218
637 collection_mods = {'_collections', 'collections', 'functools',
638 'heapq', 'itertools', 'keyword', 'operator',
639 'reprlib', 'types', 'weakref'
640 }.difference(sys.builtin_module_names)
641 self.assertFalse(modules.intersection(collection_mods), stderr)
642
643 @support.requires_subprocess()
644 def test_startup_interactivehook(self):
645 r = subprocess.Popen([sys.executable, '-c',
646 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
647 self.assertTrue(r, "'__interactivehook__' not added by site")
648
649 @support.requires_subprocess()
650 def test_startup_interactivehook_isolated(self):
651 # issue28192 readline is not automatically enabled in isolated mode
652 r = subprocess.Popen([sys.executable, '-I', '-c',
653 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
654 self.assertFalse(r, "'__interactivehook__' added in isolated mode")
655
656 @support.requires_subprocess()
657 def test_startup_interactivehook_isolated_explicit(self):
658 # issue28192 readline can be explicitly enabled in isolated mode
659 r = subprocess.Popen([sys.executable, '-I', '-c',
660 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
661 self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
662
663 class _pthFileTests(unittest.TestCase):
664
665 if sys.platform == 'win32':
666 def _create_underpth_exe(self, lines, exe_pth=True):
667 import _winapi
668 temp_dir = tempfile.mkdtemp()
669 self.addCleanup(os_helper.rmtree, temp_dir)
670 exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
671 dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
672 dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
673 shutil.copy(sys.executable, exe_file)
674 shutil.copy(dll_src_file, dll_file)
675 for fn in glob.glob(os.path.join(os.path.split(dll_src_file)[0], "vcruntime*.dll")):
676 shutil.copy(fn, os.path.join(temp_dir, os.path.split(fn)[1]))
677 if exe_pth:
678 _pth_file = os.path.splitext(exe_file)[0] + '._pth'
679 else:
680 _pth_file = os.path.splitext(dll_file)[0] + '._pth'
681 with open(_pth_file, 'w', encoding='utf8') as f:
682 for line in lines:
683 print(line, file=f)
684 return exe_file
685 else:
686 def _create_underpth_exe(self, lines, exe_pth=True):
687 if not exe_pth:
688 raise unittest.SkipTest("library ._pth file not supported on this platform")
689 temp_dir = tempfile.mkdtemp()
690 self.addCleanup(os_helper.rmtree, temp_dir)
691 exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
692 os.symlink(sys.executable, exe_file)
693 _pth_file = exe_file + '._pth'
694 with open(_pth_file, 'w') as f:
695 for line in lines:
696 print(line, file=f)
697 return exe_file
698
699 def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
700 sys_path = []
701 for line in lines:
702 if not line or line[0] == '#':
703 continue
704 abs_path = os.path.abspath(os.path.join(sys_prefix, line))
705 sys_path.append(abs_path)
706 return sys_path
707
708 def _get_pth_lines(self, libpath: str, *, import_site: bool):
709 pth_lines = ['fake-path-name']
710 # include 200 lines of `libpath` in _pth lines (or fewer
711 # if the `libpath` is long enough to get close to 32KB
712 # see https://github.com/python/cpython/issues/113628)
713 encoded_libpath_length = len(libpath.encode("utf-8"))
714 repetitions = min(200, 30000 // encoded_libpath_length)
715 if repetitions <= 2:
716 self.skipTest(
717 f"Python stdlib path is too long ({encoded_libpath_length:,} bytes)")
718 pth_lines.extend(libpath for _ in range(repetitions))
719 pth_lines.extend(['', '# comment'])
720 if import_site:
721 pth_lines.append('import site')
722 return pth_lines
723
724 @support.requires_subprocess()
725 def test_underpth_basic(self):
726 pth_lines = ['#.', '# ..', *sys.path, '.', '..']
727 exe_file = self._create_underpth_exe(pth_lines)
728 sys_path = self._calc_sys_path_for_underpth_nosite(
729 os.path.dirname(exe_file),
730 pth_lines)
731
732 output = subprocess.check_output([exe_file, '-X', 'utf8', '-c',
733 'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
734 ], encoding='utf-8', errors='surrogateescape')
735 actual_sys_path = output.rstrip().split('\n')
736 self.assertTrue(actual_sys_path, "sys.flags.no_site was False")
737 self.assertEqual(
738 actual_sys_path,
739 sys_path,
740 "sys.path is incorrect"
741 )
742
743 @support.requires_subprocess()
744 def test_underpth_nosite_file(self):
745 libpath = test.support.STDLIB_DIR
746 exe_prefix = os.path.dirname(sys.executable)
747 pth_lines = self._get_pth_lines(libpath, import_site=False)
748 exe_file = self._create_underpth_exe(pth_lines)
749 sys_path = self._calc_sys_path_for_underpth_nosite(
750 os.path.dirname(exe_file),
751 pth_lines)
752
753 env = os.environ.copy()
754 env['PYTHONPATH'] = 'from-env'
755 env['PATH'] = '{}{}{}'.format(exe_prefix, os.pathsep, os.getenv('PATH'))
756 output = subprocess.check_output([exe_file, '-c',
757 'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
758 ], env=env, encoding='utf-8', errors='surrogateescape')
759 actual_sys_path = output.rstrip().split('\n')
760 self.assertTrue(actual_sys_path, "sys.flags.no_site was False")
761 self.assertEqual(
762 actual_sys_path,
763 sys_path,
764 "sys.path is incorrect"
765 )
766
767 @support.requires_subprocess()
768 def test_underpth_file(self):
769 libpath = test.support.STDLIB_DIR
770 exe_prefix = os.path.dirname(sys.executable)
771 exe_file = self._create_underpth_exe(
772 self._get_pth_lines(libpath, import_site=True))
773 sys_prefix = os.path.dirname(exe_file)
774 env = os.environ.copy()
775 env['PYTHONPATH'] = 'from-env'
776 env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
777 rc = subprocess.call([exe_file, '-c',
778 'import sys; sys.exit(not sys.flags.no_site and '
779 '%r in sys.path and %r in sys.path and %r not in sys.path and '
780 'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
781 os.path.join(sys_prefix, 'fake-path-name'),
782 libpath,
783 os.path.join(sys_prefix, 'from-env'),
784 )], env=env)
785 self.assertTrue(rc, "sys.path is incorrect")
786
787 @support.requires_subprocess()
788 def test_underpth_dll_file(self):
789 libpath = test.support.STDLIB_DIR
790 exe_prefix = os.path.dirname(sys.executable)
791 exe_file = self._create_underpth_exe(
792 self._get_pth_lines(libpath, import_site=True), exe_pth=False)
793 sys_prefix = os.path.dirname(exe_file)
794 env = os.environ.copy()
795 env['PYTHONPATH'] = 'from-env'
796 env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
797 rc = subprocess.call([exe_file, '-c',
798 'import sys; sys.exit(not sys.flags.no_site and '
799 '%r in sys.path and %r in sys.path and %r not in sys.path and '
800 'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
801 os.path.join(sys_prefix, 'fake-path-name'),
802 libpath,
803 os.path.join(sys_prefix, 'from-env'),
804 )], env=env)
805 self.assertTrue(rc, "sys.path is incorrect")
806
807
808 class CommandLineTests(unittest.TestCase):
809 def exists(self, path):
810 if path is not None and os.path.isdir(path):
811 return "exists"
812 else:
813 return "doesn't exist"
814
815 def get_excepted_output(self, *args):
816 if len(args) == 0:
817 user_base = site.getuserbase()
818 user_site = site.getusersitepackages()
819 output = io.StringIO()
820 output.write("sys.path = [\n")
821 for dir in sys.path:
822 output.write(" %r,\n" % (dir,))
823 output.write("]\n")
824 output.write(f"USER_BASE: {user_base} ({self.exists(user_base)})\n")
825 output.write(f"USER_SITE: {user_site} ({self.exists(user_site)})\n")
826 output.write(f"ENABLE_USER_SITE: {site.ENABLE_USER_SITE}\n")
827 return 0, dedent(output.getvalue()).strip()
828
829 buffer = []
830 if '--user-base' in args:
831 buffer.append(site.getuserbase())
832 if '--user-site' in args:
833 buffer.append(site.getusersitepackages())
834
835 if buffer:
836 return_code = 3
837 if site.ENABLE_USER_SITE:
838 return_code = 0
839 elif site.ENABLE_USER_SITE is False:
840 return_code = 1
841 elif site.ENABLE_USER_SITE is None:
842 return_code = 2
843 output = os.pathsep.join(buffer)
844 return return_code, os.path.normpath(dedent(output).strip())
845 else:
846 return 10, None
847
848 def invoke_command_line(self, *args):
849 cmd_args = []
850 if sys.flags.no_user_site:
851 cmd_args.append("-s")
852 cmd_args.extend(["-m", "site", *args])
853
854 with EnvironmentVarGuard() as env:
855 env["PYTHONUTF8"] = "1"
856 env["PYTHONIOENCODING"] = "utf-8"
857 proc = spawn_python(*cmd_args, text=True, env=env,
858 encoding='utf-8', errors='replace')
859
860 output = kill_python(proc)
861 return_code = proc.returncode
862 return return_code, os.path.normpath(dedent(output).strip())
863
864 @support.requires_subprocess()
865 def test_no_args(self):
866 return_code, output = self.invoke_command_line()
867 excepted_return_code, _ = self.get_excepted_output()
868 self.assertEqual(return_code, excepted_return_code)
869 lines = output.splitlines()
870 self.assertEqual(lines[0], "sys.path = [")
871 self.assertEqual(lines[-4], "]")
872 excepted_base = f"USER_BASE: '{site.getuserbase()}'" +\
873 f" ({self.exists(site.getuserbase())})"
874 self.assertEqual(lines[-3], excepted_base)
875 excepted_site = f"USER_SITE: '{site.getusersitepackages()}'" +\
876 f" ({self.exists(site.getusersitepackages())})"
877 self.assertEqual(lines[-2], excepted_site)
878 self.assertEqual(lines[-1], f"ENABLE_USER_SITE: {site.ENABLE_USER_SITE}")
879
880 @support.requires_subprocess()
881 def test_unknown_args(self):
882 return_code, output = self.invoke_command_line("--unknown-arg")
883 excepted_return_code, _ = self.get_excepted_output("--unknown-arg")
884 self.assertEqual(return_code, excepted_return_code)
885 self.assertIn('[--user-base] [--user-site]', output)
886
887 @support.requires_subprocess()
888 def test_base_arg(self):
889 return_code, output = self.invoke_command_line("--user-base")
890 excepted = self.get_excepted_output("--user-base")
891 excepted_return_code, excepted_output = excepted
892 self.assertEqual(return_code, excepted_return_code)
893 self.assertEqual(output, excepted_output)
894
895 @support.requires_subprocess()
896 def test_site_arg(self):
897 return_code, output = self.invoke_command_line("--user-site")
898 excepted = self.get_excepted_output("--user-site")
899 excepted_return_code, excepted_output = excepted
900 self.assertEqual(return_code, excepted_return_code)
901 self.assertEqual(output, excepted_output)
902
903 @support.requires_subprocess()
904 def test_both_args(self):
905 return_code, output = self.invoke_command_line("--user-base",
906 "--user-site")
907 excepted = self.get_excepted_output("--user-base", "--user-site")
908 excepted_return_code, excepted_output = excepted
909 self.assertEqual(return_code, excepted_return_code)
910 self.assertEqual(output, excepted_output)
911
912
913 if __name__ == "__main__":
914 unittest.main()