Skip to content

Commit 124d650

Browse files
committed
Fix tests for free-threaded builds
1 parent 9af13a6 commit 124d650

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed

Lib/test/datetimetester.py

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7175,6 +7175,7 @@ def test_type_check_in_subinterp(self):
71757175
spec = importlib.util.spec_from_loader(fullname, loader)
71767176
module = importlib.util.module_from_spec(spec)
71777177
spec.loader.exec_module(module)
7178+
module.test_datetime_capi()
71787179
71797180
def run(type_checker, obj):
71807181
if not type_checker(obj, True):
@@ -7313,58 +7314,77 @@ def gen():
73137314
res = script_helper.assert_python_ok('-c', script)
73147315
self.assertFalse(res.err)
73157316

7316-
def test_static_type_at_shutdown3(self):
7317-
script = textwrap.dedent(f'''
7317+
def run_script_132413(self, script):
7318+
# iOS requires the use of the custom framework loader,
7319+
# not the ExtensionFileLoader.
7320+
if sys.platform == "ios":
7321+
extension_loader = "AppleFrameworkLoader"
7322+
else:
7323+
extension_loader = "ExtensionFileLoader"
7324+
7325+
main_interp_script = textwrap.dedent(f'''
73187326
import textwrap
73197327
from test import support
73207328
7321-
subinterp_script = textwrap.dedent(f"""
7322-
from _testcapi import get_delta_type
7323-
# Test without calling test_datetime_capi() in subinterp
7324-
timedelta = get_delta_type()
7325-
7326-
def gen():
7327-
try:
7328-
yield
7329-
finally:
7330-
timedelta(days=1)
7331-
7332-
it = gen()
7333-
next(it)
7329+
sub_script = textwrap.dedent("""
7330+
if {_interpreters is None}:
7331+
import _testcapi as module
7332+
else:
7333+
import importlib.machinery
7334+
import importlib.util
7335+
fullname = '_testcapi_datetime'
7336+
origin = importlib.util.find_spec('_testcapi').origin
7337+
loader = importlib.machinery.{extension_loader}(fullname, origin)
7338+
spec = importlib.util.spec_from_loader(fullname, loader)
7339+
module = importlib.util.module_from_spec(spec)
7340+
spec.loader.exec_module(module)
7341+
7342+
# Skip calling test_datetime_capi()
7343+
$REPLACE_ME$
73347344
""")
73357345
73367346
import _testcapi
73377347
_testcapi.test_datetime_capi()
7338-
ret = support.run_in_subinterp(subinterp_script)
7348+
7349+
if {_interpreters is None}:
7350+
ret = support.run_in_subinterp(sub_script)
7351+
else:
7352+
import _interpreters
7353+
config = _interpreters.new_config('isolated').__dict__
7354+
ret = support.run_in_subinterp_with_config(sub_script, **config)
7355+
73397356
assert ret == 0
7340-
''')
73417357
7342-
res = script_helper.assert_python_ok('-c', script)
7343-
self.assertIn(b'ImportError: sys.meta_path is None', res.err)
7358+
''').replace('$REPLACE_ME$', textwrap.indent(script, '\x20'*4))
73447359

7345-
def test_static_type_before_shutdown(self):
7346-
script = textwrap.dedent(f'''
7347-
import textwrap
7348-
from test import support
7360+
res = script_helper.assert_python_ok('-c', main_interp_script)
7361+
return res
73497362

7350-
subinterp_script = textwrap.dedent(f"""
7351-
from _testcapi import get_delta_type
7352-
# Test without calling test_datetime_capi() in subinterp
7363+
def test_static_type_at_shutdown3(self):
7364+
script = textwrap.dedent("""
7365+
timedelta = module.get_delta_type()
73537366
7354-
import sys
7355-
assert '_datetime' not in sys.modules
7356-
timedelta = get_delta_type()
7357-
timedelta(days=1)
7358-
assert '_datetime' in sys.modules
7359-
""")
7367+
def gen():
7368+
try:
7369+
yield
7370+
finally:
7371+
timedelta(days=1)
73607372
7361-
import _testcapi
7362-
_testcapi.test_datetime_capi()
7363-
ret = support.run_in_subinterp(subinterp_script)
7364-
assert ret == 0
7365-
''')
7373+
it = gen()
7374+
next(it)
7375+
""")
7376+
res = self.run_script_132413(script)
7377+
self.assertIn(b'ImportError: sys.meta_path is None', res.err)
73667378

7367-
res = script_helper.assert_python_ok('-c', script)
7379+
def test_static_type_before_shutdown(self):
7380+
script = textwrap.dedent(f"""
7381+
import sys
7382+
assert '_datetime' not in sys.modules
7383+
timedelta = module.get_delta_type()
7384+
timedelta(days=1)
7385+
assert '_datetime' in sys.modules
7386+
""")
7387+
res = self.run_script_132413(script)
73687388
self.assertFalse(res.err)
73697389

73707390
def test_remain_only_one_module(self):
@@ -7375,7 +7395,7 @@ def test_remain_only_one_module(self):
73757395
ws = weakref.WeakSet()
73767396
for _ in range(3):
73777397
import _datetime
7378-
td = _datetime.timedelta
7398+
timedelta = _datetime.timedelta
73797399
ws.add(_datetime)
73807400
del sys.modules['_datetime']
73817401
del _datetime

Modules/_testcapi/datetime.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,7 @@ _PyTestCapi_Init_DateTime(PyObject *mod)
502502
static int
503503
_testcapi_datetime_exec(PyObject *mod)
504504
{
505-
if (test_datetime_capi(NULL, NULL) == NULL) {
506-
return -1;
507-
}
505+
// Call test_datetime_capi() in each test.
508506
return 0;
509507
}
510508

0 commit comments

Comments
 (0)