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

Function load_tests

Lib/test/test_datetime.py:10–66  ·  view source on GitHub ↗
(loader, tests, pattern)

Source from the content-addressed store, hash-verified

8TESTS = 'test.datetimetester'
9
10def load_tests(loader, tests, pattern):
11 try:
12 pure_tests = import_fresh_module(TESTS,
13 fresh=['datetime', '_pydatetime', '_strptime'],
14 blocked=['_datetime'])
15 fast_tests = import_fresh_module(TESTS,
16 fresh=['datetime', '_strptime'],
17 blocked=['_pydatetime'])
18 finally:
19 # XXX: import_fresh_module() is supposed to leave sys.module cache untouched,
20 # XXX: but it does not, so we have to cleanup ourselves.
21 for modname in ['datetime', '_datetime', '_pydatetime', '_strptime']:
22 sys.modules.pop(modname, None)
23
24 test_modules = [pure_tests, fast_tests]
25 test_suffixes = ["_Pure", "_Fast"]
26
27 # XXX(gb) First run all the _Pure tests, then all the _Fast tests. You might
28 # not believe this, but in spite of all the sys.modules trickery running a _Pure
29 # test last will leave a mix of pure and native datetime stuff lying around.
30 for module, suffix in zip(test_modules, test_suffixes):
31 test_classes = []
32 if module is None:
33 continue
34 for name, cls in module.__dict__.items():
35 if not isinstance(cls, type):
36 continue
37 if issubclass(cls, unittest.TestCase):
38 test_classes.append(cls)
39 elif issubclass(cls, unittest.TestSuite):
40 suit = cls()
41 test_classes.extend(type(test) for test in suit)
42 test_classes = sorted(set(test_classes), key=lambda cls: cls.__qualname__)
43 for cls in test_classes:
44 cls.__name__ += suffix
45 cls.__qualname__ += suffix
46
47 @functools.wraps(cls, updated=())
48 class Wrapper(cls):
49 @classmethod
50 def setUpClass(cls_, module=module):
51 cls_._save_sys_modules = sys.modules.copy()
52 sys.modules[TESTS] = module
53 sys.modules['datetime'] = module.datetime_module
54 sys.modules['_pydatetime'] = module._pydatetime
55 sys.modules['_datetime'] = module._datetime
56 sys.modules['_strptime'] = module._strptime
57 super().setUpClass()
58
59 @classmethod
60 def tearDownClass(cls_):
61 super().tearDownClass()
62 sys.modules.clear()
63 sys.modules.update(cls_._save_sys_modules)
64
65 tests.addTests(loader.loadTestsFromTestCase(Wrapper))
66 return tests
67

Callers

nothing calls this directly

Calls 9

import_fresh_moduleFunction · 0.90
setFunction · 0.85
addTestsMethod · 0.80
loadTestsFromTestCaseMethod · 0.80
clsClass · 0.50
popMethod · 0.45
itemsMethod · 0.45
appendMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…