From c6020d2f7921fb2d52d44853a31dbb537a34ac84 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 11 Mar 2025 17:30:25 +0000 Subject: [PATCH 1/7] Add test skip --- Lib/test/test_zoneinfo/test_zoneinfo.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index 8414721555731e..e656cd191d0879 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -1915,7 +1915,8 @@ def test_exclude_posixrules(self): class CTestModule(TestModule): module = c_zoneinfo - +@unittest.skipIf(__import__("sys").modules.get("_zoneinfo") is None, + "_zoneinfo C module not available") class ExtensionBuiltTest(unittest.TestCase): """Smoke test to ensure that the C and Python extensions are both tested. From 9779d3e29e5077eed529a3f11f684ab1afa47058 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 11 Mar 2025 17:56:32 +0000 Subject: [PATCH 2/7] Make it nicer --- Lib/test/test_zoneinfo/_support.py | 6 +++++- Lib/test/test_zoneinfo/test_zoneinfo.py | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_zoneinfo/_support.py b/Lib/test/test_zoneinfo/_support.py index 5a76c163fb75bd..9aede6297630f3 100644 --- a/Lib/test/test_zoneinfo/_support.py +++ b/Lib/test/test_zoneinfo/_support.py @@ -33,10 +33,14 @@ def get_modules(): one time — in other words, when using this function you will only ever get one copy of each module rather than a fresh import each time. """ - import zoneinfo as c_module py_module = import_fresh_module("zoneinfo", blocked=["_zoneinfo"]) + try: + import _zoneinfo as c_module + except ImportError: + c_module = py_module + return py_module, c_module diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index e656cd191d0879..59277f448588c0 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -1915,8 +1915,7 @@ def test_exclude_posixrules(self): class CTestModule(TestModule): module = c_zoneinfo -@unittest.skipIf(__import__("sys").modules.get("_zoneinfo") is None, - "_zoneinfo C module not available") +@unittest.skipIf(c_zoneinfo == py_zoneinfo,"zoneinfo C extension not available") class ExtensionBuiltTest(unittest.TestCase): """Smoke test to ensure that the C and Python extensions are both tested. From 1fd497aee030e0af64c546c2577b905f1f344420 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 11 Mar 2025 17:59:47 +0000 Subject: [PATCH 3/7] Add accidentally removed newline --- Lib/test/test_zoneinfo/test_zoneinfo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index 59277f448588c0..19c1eb7dfe0fa4 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -1915,6 +1915,7 @@ def test_exclude_posixrules(self): class CTestModule(TestModule): module = c_zoneinfo + @unittest.skipIf(c_zoneinfo == py_zoneinfo,"zoneinfo C extension not available") class ExtensionBuiltTest(unittest.TestCase): """Smoke test to ensure that the C and Python extensions are both tested. From 885505b9b43135cb7ab0865c8cd7fea20eb56231 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 11 Mar 2025 18:34:36 +0000 Subject: [PATCH 4/7] Fix --- Lib/test/test_zoneinfo/_support.py | 1 + Lib/test/test_zoneinfo/test_zoneinfo.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_zoneinfo/_support.py b/Lib/test/test_zoneinfo/_support.py index 9aede6297630f3..ee6b2f0693d0fb 100644 --- a/Lib/test/test_zoneinfo/_support.py +++ b/Lib/test/test_zoneinfo/_support.py @@ -33,6 +33,7 @@ def get_modules(): one time — in other words, when using this function you will only ever get one copy of each module rather than a fresh import each time. """ + import zoneinfo as c_module py_module = import_fresh_module("zoneinfo", blocked=["_zoneinfo"]) diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index 19c1eb7dfe0fa4..e656cd191d0879 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -1915,8 +1915,8 @@ def test_exclude_posixrules(self): class CTestModule(TestModule): module = c_zoneinfo - -@unittest.skipIf(c_zoneinfo == py_zoneinfo,"zoneinfo C extension not available") +@unittest.skipIf(__import__("sys").modules.get("_zoneinfo") is None, + "_zoneinfo C module not available") class ExtensionBuiltTest(unittest.TestCase): """Smoke test to ensure that the C and Python extensions are both tested. From b78cf5b304014775c64c165b05c6539989795d3d Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 11 Mar 2025 18:35:29 +0000 Subject: [PATCH 5/7] Clean up again --- Lib/test/test_zoneinfo/_support.py | 5 ----- Lib/test/test_zoneinfo/test_zoneinfo.py | 1 + 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Lib/test/test_zoneinfo/_support.py b/Lib/test/test_zoneinfo/_support.py index ee6b2f0693d0fb..5a76c163fb75bd 100644 --- a/Lib/test/test_zoneinfo/_support.py +++ b/Lib/test/test_zoneinfo/_support.py @@ -37,11 +37,6 @@ def get_modules(): py_module = import_fresh_module("zoneinfo", blocked=["_zoneinfo"]) - try: - import _zoneinfo as c_module - except ImportError: - c_module = py_module - return py_module, c_module diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index e656cd191d0879..5c42c92063c1c9 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -1915,6 +1915,7 @@ def test_exclude_posixrules(self): class CTestModule(TestModule): module = c_zoneinfo + @unittest.skipIf(__import__("sys").modules.get("_zoneinfo") is None, "_zoneinfo C module not available") class ExtensionBuiltTest(unittest.TestCase): From abb9d1674fe770212926c40a92ee62345bd5ca34 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Tue, 16 Sep 2025 16:27:59 +0100 Subject: [PATCH 6/7] Lint --- Lib/test/test_zoneinfo/test_zoneinfo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index 96dfe7bac04299..3d109bc743ce1a 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -22,7 +22,6 @@ 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() From 0a04fdfb347f606772150af420d3b3b84d8c0b12 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sat, 3 Oct 2026 16:28:49 +0100 Subject: [PATCH 7/7] Fixup old merge --- Lib/test/test_zoneinfo/test_zoneinfo.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index 3d109bc743ce1a..59f82931e9872f 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -12,6 +12,7 @@ import re import shutil import struct +import sys import tempfile import unittest from datetime import date, datetime, time, timedelta, timezone @@ -22,6 +23,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() @@ -1955,7 +1957,27 @@ class CTestModule(TestModule): module = c_zoneinfo -@unittest.skipIf(__import__("sys").modules.get("_zoneinfo") is None, +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)) + + +@unittest.skipIf(sys.modules.get("_zoneinfo") is None, "_zoneinfo C module not available") class ExtensionBuiltTest(unittest.TestCase): """Smoke test to ensure that the C and Python extensions are both tested.