Skip to content

Commit

Permalink
gh-120242: Fix handling of [setUp,tearDown]Class in test_datetime (
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored and pull[bot] committed Aug 8, 2024
1 parent 5008543 commit 1165549
Showing 1 changed file with 21 additions and 15 deletions.
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

0 comments on commit 1165549

Please sign in to comment.