Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-120242: Fix handling of [setUp,tearDown]Class in test_datetime #120243

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions Lib/test/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import sys
import functools

from test.support.import_helper import import_fresh_module

Expand Down Expand Up @@ -39,21 +40,26 @@ def load_tests(loader, tests, pattern):
for cls in test_classes:
cls.__name__ += suffix
cls.__qualname__ += suffix
@classmethod
def setUpClass(cls_, module=module):
cls_._save_sys_modules = sys.modules.copy()
sys.modules[TESTS] = module
sys.modules['datetime'] = module.datetime_module
if hasattr(module, '_pydatetime'):
sys.modules['_pydatetime'] = module._pydatetime
sys.modules['_strptime'] = module._strptime
@classmethod
def tearDownClass(cls_):
sys.modules.clear()
sys.modules.update(cls_._save_sys_modules)
cls.setUpClass = setUpClass
cls.tearDownClass = tearDownClass
tests.addTests(loader.loadTestsFromTestCase(cls))

@functools.wraps(cls, updated=())
class Wrapper(cls):
@classmethod
def setUpClass(cls_, module=module):
cls_._save_sys_modules = sys.modules.copy()
sys.modules[TESTS] = module
sys.modules['datetime'] = module.datetime_module
if hasattr(module, '_pydatetime'):
sys.modules['_pydatetime'] = module._pydatetime
sys.modules['_strptime'] = module._strptime
super().setUpClass()

@classmethod
def tearDownClass(cls_):
super().tearDownClass()
sys.modules.clear()
sys.modules.update(cls_._save_sys_modules)

tests.addTests(loader.loadTestsFromTestCase(Wrapper))
return tests


Expand Down
Loading