@@ -7155,73 +7155,48 @@ def test_datetime_from_timestamp(self):
71557155
71567156 self .assertEqual (dt_orig , dt_rt )
71577157
7158- def assert_python_ok_in_subinterp (self , script , init = '' , fini = '' ,
7159- repeat = 1 , config = 'isolated' ):
7158+ def test_type_check_in_subinterp (self ):
71607159 # iOS requires the use of the custom framework loader,
71617160 # not the ExtensionFileLoader.
71627161 if sys .platform == "ios" :
71637162 extension_loader = "AppleFrameworkLoader"
71647163 else :
71657164 extension_loader = "ExtensionFileLoader"
71667165
7167- code = textwrap .dedent (f'''
7168- subinterp_code = """
7166+ script = textwrap .dedent (f"""
71697167 if { _interpreters is None } :
7170- import _testcapi
7168+ import _testcapi as module
7169+ module.test_datetime_capi()
71717170 else:
71727171 import importlib.machinery
71737172 import importlib.util
71747173 fullname = '_testcapi_datetime'
71757174 origin = importlib.util.find_spec('_testcapi').origin
71767175 loader = importlib.machinery.{ extension_loader } (fullname, origin)
71777176 spec = importlib.util.spec_from_loader(fullname, loader)
7178- _testcapi = importlib.util.module_from_spec(spec)
7179- spec.loader.exec_module(_testcapi)
7180- interp_index = $INTERP_INDEX$
7181- setup = _testcapi.test_datetime_capi_newinterp # call it if needed
7182- $SCRIPT$
7183- """
7177+ module = importlib.util.module_from_spec(spec)
7178+ spec.loader.exec_module(module)
71847179
7185- import _testcapi
7186- from test import support
7187- setup = _testcapi.test_datetime_capi_newinterp
7188- $INIT$
7189-
7190- for idx in range({ repeat } ):
7191- subcode = subinterp_code.replace('$INTERP_INDEX$', str(idx))
7192- if { _interpreters is None } :
7193- ret = support.run_in_subinterp(subcode)
7194- else:
7195- import _interpreters
7196- config = _interpreters.new_config('{ config } ').__dict__
7197- ret = support.run_in_subinterp_with_config(subcode, **config)
7198- assert ret == 0
7199- $FINI$
7200- ''' )
7201- code = code .replace ('$INIT$' , init ).replace ('$FINI$' , fini )
7202- code = code .replace ('$SCRIPT$' , script )
7203-
7204- res = script_helper .assert_python_ok ('-c' , code )
7205- return res
7206-
7207- def test_type_check_in_subinterp (self ):
7208- script = textwrap .dedent (f"""
72097180 def run(type_checker, obj):
72107181 if not type_checker(obj, True):
72117182 raise TypeError(f'{{type(obj)}} is not C API type')
72127183
7213- setup()
72147184 import _datetime
7215- run(_testcapi.datetime_check_date, _datetime.date.today())
7216- run(_testcapi.datetime_check_datetime, _datetime.datetime.now())
7217- run(_testcapi.datetime_check_time, _datetime.time(12, 30))
7218- run(_testcapi.datetime_check_delta, _datetime.timedelta(1))
7219- run(_testcapi.datetime_check_tzinfo, _datetime.tzinfo())
7220- """ )
7221- self .assert_python_ok_in_subinterp (script )
7222- if _interpreters is not None :
7223- with self .subTest (name := 'legacy' ):
7224- self .assert_python_ok_in_subinterp (script , config = name )
7185+ run(module.datetime_check_date, _datetime.date.today())
7186+ run(module.datetime_check_datetime, _datetime.datetime.now())
7187+ run(module.datetime_check_time, _datetime.time(12, 30))
7188+ run(module.datetime_check_delta, _datetime.timedelta(1))
7189+ run(module.datetime_check_tzinfo, _datetime.tzinfo())
7190+ """ )
7191+ if _interpreters is None :
7192+ ret = support .run_in_subinterp (script )
7193+ self .assertEqual (ret , 0 )
7194+ else :
7195+ for name in ('isolated' , 'legacy' ):
7196+ with self .subTest (name ):
7197+ config = _interpreters .new_config (name ).__dict__
7198+ ret = support .run_in_subinterp_with_config (script , ** config )
7199+ self .assertEqual (ret , 0 )
72257200
72267201
72277202class ExtensionModuleTests (unittest .TestCase ):
@@ -7230,9 +7205,6 @@ def setUp(self):
72307205 if self .__class__ .__name__ .endswith ('Pure' ):
72317206 self .skipTest ('Not relevant in pure Python' )
72327207
7233- def assert_python_ok_in_subinterp (self , * args , ** kwargs ):
7234- return CapiTest .assert_python_ok_in_subinterp (self , * args , ** kwargs )
7235-
72367208 @support .cpython_only
72377209 def test_gh_120161 (self ):
72387210 with self .subTest ('simple' ):
@@ -7298,117 +7270,8 @@ def test_update_type_cache(self):
72987270 assert isinstance(_datetime.timezone.utc, _datetime.tzinfo)
72997271 del sys.modules['_datetime']
73007272 """ )
7301- res = script_helper .assert_python_ok ('-c' , script )
7302- self .assertFalse (res .err )
7303-
7304- def test_module_free (self ):
7305- script = textwrap .dedent ("""
7306- import sys
7307- import gc
7308- import weakref
7309- ws = weakref.WeakSet()
7310- for _ in range(3):
7311- import _datetime
7312- timedelta = _datetime.timedelta # static type
7313- ws.add(_datetime)
7314- del sys.modules['_datetime']
7315- del _datetime
7316- gc.collect()
7317- assert len(ws) == 1 # only one remains
7318- """ )
73197273 script_helper .assert_python_ok ('-c' , script )
73207274
7321- @unittest .skipIf (not support .Py_DEBUG , "Debug builds only" )
7322- def test_no_leak (self ):
7323- script = textwrap .dedent ("""
7324- import datetime
7325- datetime.datetime.strptime('20000101', '%Y%m%d').strftime('%Y%m%d')
7326- """ )
7327- res = script_helper .assert_python_ok ('-X' , 'showrefcount' , '-c' , script )
7328- self .assertIn (b'[0 refs, 0 blocks]' , res .err )
7329-
7330- def test_static_type_on_subinterp (self ):
7331- script = textwrap .dedent ("""
7332- date = _testcapi.get_capi_types()['date']
7333- date.today
7334- """ )
7335- with_setup = 'setup()' + script
7336- with self .subTest ('[PyDateTime_IMPORT] main: no, sub: yes' ):
7337- self .assert_python_ok_in_subinterp (with_setup )
7338-
7339- with self .subTest ('[PyDateTime_IMPORT] main: yes, sub: yes' ):
7340- # Fails if the setup() means test_datetime_capi() rather than
7341- # test_datetime_capi_newinterp()
7342- self .assert_python_ok_in_subinterp (with_setup , 'setup()' )
7343- self .assert_python_ok_in_subinterp ('setup()' , fini = with_setup )
7344- self .assert_python_ok_in_subinterp (with_setup , repeat = 2 )
7345-
7346- with_import = 'import _datetime' + script
7347- with self .subTest ('Explicit import' ):
7348- self .assert_python_ok_in_subinterp (with_import , 'setup()' )
7349-
7350- with_import = textwrap .dedent ("""
7351- timedelta = _testcapi.get_capi_types()['timedelta']
7352- timedelta(days=1)
7353- """ ) + script
7354- with self .subTest ('Implicit import' ):
7355- self .assert_python_ok_in_subinterp (with_import , 'setup()' )
7356-
7357- def test_static_type_at_shutdown (self ):
7358- # gh-132413
7359- script = textwrap .dedent ("""
7360- import sys
7361- import _datetime
7362- timedelta = _datetime.timedelta
7363-
7364- def gen():
7365- try:
7366- yield
7367- finally:
7368- # Exceptions are ignored here
7369- assert not sys.modules
7370- td = _datetime.timedelta(days=1)
7371- assert td.days == 1
7372- td = timedelta(days=1)
7373- assert td.days == 1
7374- assert not sys.modules
7375-
7376- it = gen()
7377- next(it)
7378- """ )
7379- with self .subTest ('MainInterpreter' ):
7380- res = script_helper .assert_python_ok ('-c' , script )
7381- self .assertFalse (res .err )
7382- with self .subTest ('Subinterpreter' ):
7383- res = self .assert_python_ok_in_subinterp (script )
7384- self .assertFalse (res .err )
7385-
7386- script = textwrap .dedent ("""
7387- import sys
7388- timedelta = _testcapi.get_capi_types()['timedelta']
7389-
7390- def gen():
7391- try:
7392- yield
7393- finally:
7394- # Exceptions are ignored here
7395- assert not sys.modules
7396- td = timedelta(days=1)
7397- assert td.days == 1
7398- assert not sys.modules
7399-
7400- it = gen()
7401- next(it)
7402- """ )
7403- with self .subTest ('[PyDateTime_IMPORT] main: yes, sub: no' ):
7404- res = self .assert_python_ok_in_subinterp (script , 'setup()' )
7405- self .assertIn (b'ImportError: sys.meta_path is None' , res .err )
7406-
7407- with_import = 'setup()' + script
7408- with self .subTest ('[PyDateTime_IMPORT] main: no, sub: yes' ):
7409- res = self .assert_python_ok_in_subinterp (with_import )
7410- self .assertFalse (res .err )
7411-
74127275
74137276def load_tests (loader , standard_tests , pattern ):
74147277 standard_tests .addTest (ZoneInfoCompleteTest ())
0 commit comments