MCPcopy Index your code
hub / github.com/python/cpython / test_startup_imports

Method test_startup_imports

Lib/test/test_site.py:582–630  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

580
581 @support.requires_subprocess()
582 def test_startup_imports(self):
583 # Get sys.path in isolated mode (python3 -I)
584 popen = subprocess.Popen([sys.executable, '-X', 'utf8', '-I',
585 '-c', 'import sys; print(repr(sys.path))'],
586 stdout=subprocess.PIPE,
587 encoding='utf-8',
588 errors='surrogateescape')
589 stdout = popen.communicate()[0]
590 self.assertEqual(popen.returncode, 0, repr(stdout))
591 isolated_paths = ast.literal_eval(stdout)
592
593 # bpo-27807: Even with -I, the site module executes all .pth files
594 # found in sys.path (see site.addpackage()). Skip the test if at least
595 # one .pth file is found.
596 for path in isolated_paths:
597 pth_files = glob.glob(os.path.join(glob.escape(path), "*.pth"))
598 if pth_files:
599 self.skipTest(f"found {len(pth_files)} .pth files in: {path}")
600
601 # This tests checks which modules are loaded by Python when it
602 # initially starts upon startup.
603 popen = subprocess.Popen([sys.executable, '-X', 'utf8', '-I', '-v',
604 '-c', 'import sys; print(set(sys.modules))'],
605 stdout=subprocess.PIPE,
606 stderr=subprocess.PIPE,
607 encoding='utf-8',
608 errors='surrogateescape')
609 stdout, stderr = popen.communicate()
610 self.assertEqual(popen.returncode, 0, (stdout, stderr))
611 modules = ast.literal_eval(stdout)
612
613 self.assertIn('site', modules)
614
615 # http://bugs.python.org/issue19205
616 re_mods = {'re', '_sre', 're._compiler', 're._constants', 're._parser'}
617 self.assertFalse(modules.intersection(re_mods), stderr)
618
619 # http://bugs.python.org/issue9548
620 self.assertNotIn('locale', modules, stderr)
621
622 # http://bugs.python.org/issue19209
623 self.assertNotIn('copyreg', modules, stderr)
624
625 # http://bugs.python.org/issue19218
626 collection_mods = {'_collections', 'collections', 'functools',
627 'heapq', 'itertools', 'keyword', 'operator',
628 'reprlib', 'types', 'weakref'
629 }.difference(sys.builtin_module_names)
630 self.assertFalse(modules.intersection(collection_mods), stderr)
631
632 @support.requires_subprocess()
633 def test_startup_interactivehook(self):

Callers

nothing calls this directly

Calls 11

communicateMethod · 0.95
escapeMethod · 0.80
skipTestMethod · 0.80
assertInMethod · 0.80
assertFalseMethod · 0.80
assertNotInMethod · 0.80
differenceMethod · 0.80
assertEqualMethod · 0.45
globMethod · 0.45
joinMethod · 0.45
intersectionMethod · 0.45

Tested by

no test coverage detected