Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-132413: Extend datetime C-API tests for subinterpreters#133111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes from1 commit
Commits
Show all changes
15 commits Select commitHold shift + click to select a range
30c30cd
Update datetime.c
neonene9c11843
Update datetimetester.py
neoneneda91cdb
Add a note
neonene7d79dd9
Reword
neonenece0a687
Merge branch 'main' into crashtester
neonenea862456
Rename a keyword to be replaced
neonenebe5791c
Reconsider arguments
neonenecccdd2c
Add a testcase
neonene4298825
Add a test for using subinterp twice
neoneneb3f3f21
Do not dedent subinterp's code
neonene0ee6e3d
Introduce test_datetime_capi_newinterp()
neoneneca98adb
Cleanup
neonene8bcad71
Add a test to test_embed
neonenea2919f7
Smaller patch for tests
neonene01ac886
Merge branch 'main' into crashtester
neoneneFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Update datetimetester.py
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit9c118439d2269d5024ed4188fbdee6cb02c52a55
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -7155,48 +7155,76 @@ def test_datetime_from_timestamp(self): | ||
self.assertEqual(dt_orig, dt_rt) | ||
def assert_python_in_subinterp(self, check_if_ok: bool, script, | ||
neonene marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
setup='_testcapi.test_datetime_capi()', | ||
config='isolated'): | ||
# iOS requires the use of the custom framework loader, | ||
# not the ExtensionFileLoader. | ||
if sys.platform == "ios": | ||
extension_loader = "AppleFrameworkLoader" | ||
else: | ||
extension_loader = "ExtensionFileLoader" | ||
code = textwrap.dedent(f''' | ||
import textwrap | ||
from test import support | ||
subcode = textwrap.dedent(""" | ||
if {_interpreters is None}: | ||
import _testcapi | ||
else: | ||
import importlib.machinery | ||
import importlib.util | ||
fullname = '_testcapi_datetime' | ||
origin = importlib.util.find_spec('_testcapi').origin | ||
loader = importlib.machinery.{extension_loader}(fullname, origin) | ||
spec = importlib.util.spec_from_loader(fullname, loader) | ||
_testcapi = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(_testcapi) | ||
$SCRIPT$ | ||
""") | ||
import _testcapi | ||
$SETUP$ | ||
if {_interpreters is None}: | ||
ret = support.run_in_subinterp(subcode) | ||
else: | ||
import _interpreters | ||
config = _interpreters.new_config('{config}').__dict__ | ||
ret = support.run_in_subinterp_with_config(subcode, **config) | ||
assert ret == 0 | ||
''').rstrip() | ||
code = code.replace('$SETUP$', setup) | ||
code = code.replace('$SCRIPT$', textwrap.indent(script, '\x20'*4)) | ||
if check_if_ok: | ||
res = script_helper.assert_python_ok('-c', code) | ||
else: | ||
res = script_helper.assert_python_failure('-c', code) | ||
return res | ||
def test_type_check_in_subinterp(self): | ||
script = textwrap.dedent(f""" | ||
def run(type_checker, obj): | ||
if not type_checker(obj, True): | ||
raise TypeError(f'{{type(obj)}} is not C API type') | ||
_testcapi.test_datetime_capi() | ||
import _datetime | ||
run(_testcapi.datetime_check_date, _datetime.date.today()) | ||
run(_testcapi.datetime_check_datetime, _datetime.datetime.now()) | ||
run(_testcapi.datetime_check_time, _datetime.time(12, 30)) | ||
run(_testcapi.datetime_check_delta, _datetime.timedelta(1)) | ||
run(_testcapi.datetime_check_tzinfo, _datetime.tzinfo()) | ||
""") | ||
self.assert_python_in_subinterp(True, script, '') | ||
if _interpreters is not None: | ||
with self.subTest(name := 'legacy'): | ||
self.assert_python_in_subinterp(True, script, '', name) | ||
class ExtensionModuleTests(unittest.TestCase): | ||
@@ -7205,6 +7233,9 @@ def setUp(self): | ||
if self.__class__.__name__.endswith('Pure'): | ||
self.skipTest('Not relevant in pure Python') | ||
def assert_python_in_subinterp(self, *args, **kwargs): | ||
return CapiTest.assert_python_in_subinterp(self, *args, **kwargs) | ||
@support.cpython_only | ||
def test_gh_120161(self): | ||
with self.subTest('simple'): | ||
@@ -7270,8 +7301,64 @@ def test_update_type_cache(self): | ||
assert isinstance(_datetime.timezone.utc, _datetime.tzinfo) | ||
del sys.modules['_datetime'] | ||
""") | ||
res = script_helper.assert_python_ok('-c', script) | ||
self.assertFalse(res.err) | ||
def test_module_free(self): | ||
script = textwrap.dedent(""" | ||
import sys | ||
import gc | ||
import weakref | ||
ws = weakref.WeakSet() | ||
for _ in range(3): | ||
import _datetime | ||
timedelta = _datetime.timedelta # static type | ||
ws.add(_datetime) | ||
del sys.modules['_datetime'] | ||
del _datetime | ||
gc.collect() | ||
assert len(ws) == 0 | ||
""") | ||
script_helper.assert_python_ok('-c', script) | ||
@unittest.skipIf(not support.Py_DEBUG, "Debug builds only") | ||
def test_no_leak(self): | ||
script = textwrap.dedent(""" | ||
import datetime | ||
datetime.datetime.strptime('20000101', '%Y%m%d').strftime('%Y%m%d') | ||
""") | ||
res = script_helper.assert_python_ok('-X', 'showrefcount', '-c', script) | ||
self.assertIn(b'[0 refs, 0 blocks]', res.err) | ||
def test_static_type_on_subinterp(self): | ||
script = textwrap.dedent(""" | ||
date = _testcapi.get_capi_types()['date'] | ||
date.today | ||
""") | ||
# Test the script fails | ||
with self.subTest('[PyDateTime_IMPORT] main: yes, sub: no'): | ||
self.assert_python_in_subinterp(False, script) | ||
# Test the script succeeds | ||
script2 = '_testcapi.test_datetime_capi()' + script | ||
with self.subTest('[PyDateTime_IMPORT] main: no, sub: yes'): | ||
self.assert_python_in_subinterp(True, script2, setup='') | ||
with self.subTest('[PyDateTime_IMPORT] main: yes, sub: yes'): | ||
# Check if PyDateTime_IMPORT is invoked not only once | ||
self.assert_python_in_subinterp(True, script2) | ||
script3 = 'import _datetime' + script | ||
with self.subTest('Explicit import'): | ||
self.assert_python_in_subinterp(True, script3) | ||
script4 = textwrap.dedent(""" | ||
timedelta = _testcapi.get_capi_types()['timedelta'] | ||
timedelta(days=1) | ||
""") + script | ||
with self.subTest('Implicit import'): | ||
self.assert_python_in_subinterp(True, script4) | ||
def load_tests(loader, standard_tests, pattern): | ||
standard_tests.addTest(ZoneInfoCompleteTest()) | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.