mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
gh-137754: Fix import of zoneinfo if _datetime is not available (GH-137845)
Both modules should use the Python implementation in that case.
This commit is contained in:
parent
282e88506b
commit
6620ef0ff6
3 changed files with 27 additions and 1 deletions
|
|
@ -22,6 +22,7 @@
|
|||
from test.test_zoneinfo import _support as test_support
|
||||
from test.test_zoneinfo._support import TZPATH_TEST_LOCK, ZoneInfoTestBase
|
||||
from test.support.import_helper import import_module, CleanImport
|
||||
from test.support.script_helper import assert_python_ok
|
||||
|
||||
lzma = import_module('lzma')
|
||||
py_zoneinfo, c_zoneinfo = test_support.get_modules()
|
||||
|
|
@ -1946,6 +1947,26 @@ class CTestModule(TestModule):
|
|||
module = c_zoneinfo
|
||||
|
||||
|
||||
class MiscTests(unittest.TestCase):
|
||||
def test_pydatetime(self):
|
||||
# Test that zoneinfo works if the C implementation of datetime
|
||||
# is not available and the Python implementation of datetime is used.
|
||||
# The Python implementation of zoneinfo should be used in thet case.
|
||||
#
|
||||
# Run the test in a subprocess, as importing _zoneinfo with
|
||||
# _datettime disabled causes crash in the previously imported
|
||||
# _zoneinfo.
|
||||
assert_python_ok('-c', '''if 1:
|
||||
import sys
|
||||
sys.modules['_datetime'] = None
|
||||
import datetime
|
||||
import zoneinfo
|
||||
tzinfo = zoneinfo.ZoneInfo('Europe/London')
|
||||
datetime.datetime(2025, 10, 26, 2, 0, tzinfo=tzinfo)
|
||||
''',
|
||||
PYTHONTZPATH=str(ZONEINFO_DATA.tzpath))
|
||||
|
||||
|
||||
class ExtensionBuiltTest(unittest.TestCase):
|
||||
"""Smoke test to ensure that the C and Python extensions are both tested.
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,10 @@
|
|||
|
||||
try:
|
||||
from _zoneinfo import ZoneInfo
|
||||
except ImportError: # pragma: nocover
|
||||
except (ImportError, AttributeError): # pragma: nocover
|
||||
# AttributeError: module 'datetime' has no attribute 'datetime_CAPI'.
|
||||
# This happens when the '_datetime' module is not available and the
|
||||
# pure Python implementation is used instead.
|
||||
from ._zoneinfo import ZoneInfo
|
||||
|
||||
reset_tzpath = _tzpath.reset_tzpath
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Fix import of the :mod:`zoneinfo` module if the C implementation of the
|
||||
:mod:`datetime` module is not available.
|
||||
Loading…
Add table
Add a link
Reference in a new issue