@@ -441,7 +441,7 @@ def test_datetime_reset_strptime(self):
441441 self .assertEqual (out , '20000101\n ' * INIT_LOOPS )
442442
443443 def test_datetime_capi_at_shutdown (self ):
444- # gh-120782: Test the case where PyDateTime_IMPORT is called only once
444+ # gh-120782: Current datetime test calls PyDateTime_IMPORT only once
445445 code = textwrap .dedent ("""
446446 import sys
447447 import _testcapi
@@ -471,6 +471,34 @@ def gen():
471471 out , err = self .run_embedded_interpreter ("test_repeated_init_exec" , code )
472472 self .assertEqual (out , '1\n ' + '2\n ' * (INIT_LOOPS - 1 ))
473473
474+ def test_datetime_capi_at_shutdown2 (self ):
475+ # gh-120782: This PR allows PyDateTime_IMPORT to be called on restart
476+ code = textwrap .dedent ("""
477+ import sys
478+ import _testcapi
479+ _testcapi.test_datetime_capi()
480+ assert '_datetime' in sys.modules
481+ timedelta = _testcapi.get_capi_types()['timedelta']
482+
483+ def gen():
484+ try:
485+ yield
486+ finally:
487+ assert not sys.modules
488+ res = 1
489+ try:
490+ timedelta(days=1)
491+ except ImportError as e:
492+ res = 2 if 'sys.meta_path is None' in e.msg else 3
493+ assert not sys.modules
494+ print(res)
495+
496+ it = gen()
497+ next(it)
498+ """ )
499+ out , err = self .run_embedded_interpreter ("test_repeated_init_exec" , code )
500+ self .assertEqual (out , '1\n ' * INIT_LOOPS )
501+
474502 def test_static_types_inherited_slots (self ):
475503 script = textwrap .dedent ("""
476504 import test.support
0 commit comments