-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-120161: Fix a Crash in the _datetime Module #120182
Changes from all commits
1cb0da2
0054962
7d8a19f
34d17e1
a7d2f06
99db91f
3c0084d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import re | ||
import struct | ||
import sys | ||
import textwrap | ||
import unittest | ||
import warnings | ||
|
||
|
@@ -22,7 +23,7 @@ | |
|
||
from test import support | ||
from test.support import is_resource_enabled, ALWAYS_EQ, LARGEST, SMALLEST | ||
from test.support import warnings_helper | ||
from test.support import script_helper, warnings_helper | ||
|
||
import datetime as datetime_module | ||
from datetime import MINYEAR, MAXYEAR | ||
|
@@ -6781,6 +6782,48 @@ def test_datetime_from_timestamp(self): | |
self.assertEqual(dt_orig, dt_rt) | ||
|
||
|
||
class ExtensionModuleTests(unittest.TestCase): | ||
|
||
def setUp(self): | ||
if self.__class__.__name__.endswith('Pure'): | ||
self.skipTest('Not relevant in pure Python') | ||
|
||
@support.cpython_only | ||
def test_gh_120161(self): | ||
with self.subTest('simple'): | ||
script = textwrap.dedent(""" | ||
import datetime | ||
from _ast import Tuple | ||
f = lambda: None | ||
Tuple.dims = property(f, f) | ||
|
||
class tzutc(datetime.tzinfo): | ||
pass | ||
""") | ||
script_helper.assert_python_ok('-c', script) | ||
|
||
with self.subTest('complex'): | ||
script = textwrap.dedent(""" | ||
import asyncio | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would separate this test into its own method, it imports There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, why does it need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the original issue (the snippet is taken almost verbatim). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Importing |
||
import datetime | ||
from typing import Type | ||
|
||
class tzutc(datetime.tzinfo): | ||
pass | ||
_EPOCHTZ = datetime.datetime(1970, 1, 1, tzinfo=tzutc()) | ||
|
||
class FakeDateMeta(type): | ||
def __instancecheck__(self, obj): | ||
return True | ||
class FakeDate(datetime.date, metaclass=FakeDateMeta): | ||
pass | ||
def pickle_fake_date(datetime_) -> Type[FakeDate]: | ||
# A pickle function for FakeDate | ||
return FakeDate | ||
""") | ||
script_helper.assert_python_ok('-c', script) | ||
|
||
|
||
def load_tests(loader, standard_tests, pattern): | ||
standard_tests.addTest(ZoneInfoCompleteTest()) | ||
return standard_tests | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:mod:`datetime` no longer crashes in certain complex reference cycle | ||
situations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpython_only
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but currently there's mostly no need. If the extension module ("_datetime") doesn't exist then the "fast" version of the test is never created, and the pure-Python version of the test already always skips. (See Lib/test/test_datetime.py.) Also note that
@cpython_only
would mean the test wouldn't be run for Python implementations that do provide a_datetime
accelerator module.All that said, I don't see any harm with adding
@cpython_only
on the new test method. I would certainly only expect it to fail on CPython. Thanks for bringing this up.