Skip to content
14 changes: 12 additions & 2 deletions Lib/test/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

from test.support.import_helper import import_fresh_module


TESTS = 'test.datetimetester'

def load_tests(loader, tests, pattern):
try:
try:
import _datetime
except ImportError:
has_datetime = False
Comment thread
VanshAgarwal24036 marked this conversation as resolved.
Outdated
else:
has_datetime = True
del _datetime
pure_tests = import_fresh_module(TESTS,
fresh=['datetime', '_pydatetime', '_strptime'],
blocked=['_datetime'])
Expand All @@ -18,11 +24,12 @@ def load_tests(loader, tests, pattern):
finally:
# XXX: import_fresh_module() is supposed to leave sys.module cache untouched,
# XXX: but it does not, so we have to cleanup ourselves.
for modname in ['datetime', '_datetime', '_strptime']:
for modname in ['datetime', '_datetime', '_pydatetime', '_strptime']:
sys.modules.pop(modname, None)

test_modules = [pure_tests, fast_tests]
test_suffixes = ["_Pure", "_Fast"]

# XXX(gb) First run all the _Pure tests, then all the _Fast tests. You might
# not believe this, but in spite of all the sys.modules trickery running a _Pure
# test last will leave a mix of pure and native datetime stuff lying around.
Expand All @@ -45,6 +52,9 @@ def load_tests(loader, tests, pattern):
class Wrapper(cls):
@classmethod
def setUpClass(cls_, module=module):
if module is fast_tests and not has_datetime:
raise unittest.SkipTest("requires _datetime module")

cls_._save_sys_modules = sys.modules.copy()
sys.modules[TESTS] = module
sys.modules['datetime'] = module.datetime_module
Expand Down
Loading