mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
gh-117398: Add datetime C-API type check test for subinterpreters (gh-119604)
Check if the DateTime C-API type matches the datetime.date type on main and shared/isolated subinterpreters.
This commit is contained in:
parent
c2d810b6d4
commit
50a389565a
3 changed files with 87 additions and 4 deletions
|
|
@ -13,6 +13,7 @@ import random
|
|||
import re
|
||||
import struct
|
||||
import sys
|
||||
import textwrap
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
|
|
@ -38,6 +39,10 @@ try:
|
|||
import _testcapi
|
||||
except ImportError:
|
||||
_testcapi = None
|
||||
try:
|
||||
import _interpreters
|
||||
except ModuleNotFoundError:
|
||||
_interpreters = None
|
||||
|
||||
# Needed by test_datetime
|
||||
import _strptime
|
||||
|
|
@ -6780,6 +6785,42 @@ class CapiTest(unittest.TestCase):
|
|||
|
||||
self.assertEqual(dt_orig, dt_rt)
|
||||
|
||||
def test_type_check_in_subinterp(self):
|
||||
script = textwrap.dedent(f"""
|
||||
if {_interpreters is None}:
|
||||
import _testcapi as module
|
||||
module.test_datetime_capi()
|
||||
else:
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
fullname = '_testcapi_datetime'
|
||||
origin = importlib.util.find_spec('_testcapi').origin
|
||||
loader = importlib.machinery.ExtensionFileLoader(fullname, origin)
|
||||
spec = importlib.util.spec_from_loader(fullname, loader)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
def run(type_checker, obj):
|
||||
if not type_checker(obj, True):
|
||||
raise TypeError(f'{{type(obj)}} is not C API type')
|
||||
|
||||
import _datetime
|
||||
run(module.datetime_check_date, _datetime.date.today())
|
||||
run(module.datetime_check_datetime, _datetime.datetime.now())
|
||||
run(module.datetime_check_time, _datetime.time(12, 30))
|
||||
run(module.datetime_check_delta, _datetime.timedelta(1))
|
||||
run(module.datetime_check_tzinfo, _datetime.tzinfo())
|
||||
""")
|
||||
if _interpreters is None:
|
||||
ret = support.run_in_subinterp(script)
|
||||
self.assertEqual(ret, 0)
|
||||
else:
|
||||
for name in ('isolated', 'legacy'):
|
||||
with self.subTest(name):
|
||||
config = _interpreters.new_config(name).__dict__
|
||||
ret = support.run_in_subinterp_with_config(script, **config)
|
||||
self.assertEqual(ret, 0)
|
||||
|
||||
|
||||
def load_tests(loader, standard_tests, pattern):
|
||||
standard_tests.addTest(ZoneInfoCompleteTest())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue