From df22dbcebad5bab5bc0cd26eb1ea2909fcdbbf03 Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Sun, 18 Jan 2026 19:08:55 +0530 Subject: [PATCH 01/10] gh-143959: Split datetime tests requiring _datetime --- Lib/test/test_datetime.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 005187f13e665f..bfdbed63fe886e 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -2,6 +2,7 @@ import sys import functools +from test import support from test.support.import_helper import import_fresh_module @@ -12,17 +13,24 @@ def load_tests(loader, tests, pattern): pure_tests = import_fresh_module(TESTS, fresh=['datetime', '_pydatetime', '_strptime'], blocked=['_datetime']) - fast_tests = import_fresh_module(TESTS, - fresh=['datetime', '_strptime'], - blocked=['_pydatetime']) + fast_tests = None + if support.import_module('_datetime', required=False): + fast_tests = import_fresh_module( + TESTS, + fresh=['datetime', '_strptime'], + blocked=['_pydatetime'], + ) finally: # XXX: import_fresh_module() is supposed to leave sys.module cache untouched, # XXX: but it does not, so we have to cleanup ourselves. - for modname in ['datetime', '_datetime', '_strptime']: + for modname in ['datetime', '_datetime', '_pydatetime', '_strptime']: sys.modules.pop(modname, None) - test_modules = [pure_tests, fast_tests] - test_suffixes = ["_Pure", "_Fast"] + test_modules = [pure_tests] + test_suffixes = ["_Pure"] + if fast_tests is not None: + test_modules.append(fast_tests) + test_suffixes.append("_Fast") # XXX(gb) First run all the _Pure tests, then all the _Fast tests. You might # not believe this, but in spite of all the sys.modules trickery running a _Pure # test last will leave a mix of pure and native datetime stuff lying around. From 3b42bcd402820b8b573ed061bd2f599aa96eec01 Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Sun, 18 Jan 2026 23:12:53 +0530 Subject: [PATCH 02/10] gh-143959: Split datetime tests requiring _datetime --- Lib/test/test_datetime.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index bfdbed63fe886e..e2602f92b662ce 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -2,19 +2,24 @@ import sys import functools -from test import support -from test.support.import_helper import import_fresh_module +from test.support.import_helper import import_fresh_module, import_module TESTS = 'test.datetimetester' def load_tests(loader, tests, pattern): try: - pure_tests = import_fresh_module(TESTS, - fresh=['datetime', '_pydatetime', '_strptime'], - blocked=['_datetime']) + pure_tests = import_fresh_module( + TESTS, + fresh=['datetime', '_pydatetime', '_strptime'], + blocked=['_datetime'], + ) fast_tests = None - if support.import_module('_datetime', required=False): + try: + import_module('_datetime') + except ImportError: + fast_tests = None + else: fast_tests = import_fresh_module( TESTS, fresh=['datetime', '_strptime'], From ecfbbbd453ff3378b634e50b841f3d8876bfc45f Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 16:51:58 +0530 Subject: [PATCH 03/10] gh-143959: Skip datetime Fast tests when _datetime absent --- Lib/test/test_datetime.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index e2602f92b662ce..42e235e27e6672 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -2,8 +2,7 @@ import sys import functools -from test.support.import_helper import import_fresh_module, import_module - +from test.support.import_helper import import_fresh_module TESTS = 'test.datetimetester' @@ -14,28 +13,26 @@ def load_tests(loader, tests, pattern): fresh=['datetime', '_pydatetime', '_strptime'], blocked=['_datetime'], ) - fast_tests = None try: - import_module('_datetime') + import _datetime + has_datetime = True except ImportError: - fast_tests = None - else: - fast_tests = import_fresh_module( - TESTS, - fresh=['datetime', '_strptime'], - blocked=['_pydatetime'], - ) + has_datetime = False + + fast_tests = import_fresh_module( + TESTS, + fresh=['datetime', '_strptime'], + blocked=['_pydatetime'], + ) finally: # XXX: import_fresh_module() is supposed to leave sys.module cache untouched, # XXX: but it does not, so we have to cleanup ourselves. for modname in ['datetime', '_datetime', '_pydatetime', '_strptime']: sys.modules.pop(modname, None) - test_modules = [pure_tests] - test_suffixes = ["_Pure"] - if fast_tests is not None: - test_modules.append(fast_tests) - test_suffixes.append("_Fast") + test_modules = [pure_tests, fast_tests] + test_suffixes = ["_Pure", "_Fast"] + # XXX(gb) First run all the _Pure tests, then all the _Fast tests. You might # not believe this, but in spite of all the sys.modules trickery running a _Pure # test last will leave a mix of pure and native datetime stuff lying around. @@ -58,6 +55,9 @@ def load_tests(loader, tests, pattern): class Wrapper(cls): @classmethod def setUpClass(cls_, module=module): + if suffix == "_Fast" and not has_datetime: + raise unittest.SkipTest("requires _datetime module") + cls_._save_sys_modules = sys.modules.copy() sys.modules[TESTS] = module sys.modules['datetime'] = module.datetime_module From b227ee9afcfa5df7782d5a2b1024bfb92a4baef5 Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 16:56:11 +0530 Subject: [PATCH 04/10] Removed whitespaces --- Lib/test/test_datetime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 42e235e27e6672..05af8499ff93e7 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -57,7 +57,7 @@ class Wrapper(cls): def setUpClass(cls_, module=module): if suffix == "_Fast" and not has_datetime: raise unittest.SkipTest("requires _datetime module") - + cls_._save_sys_modules = sys.modules.copy() sys.modules[TESTS] = module sys.modules['datetime'] = module.datetime_module From 281077dee4e27d5af9cfdb7365d64ef02b3c253b Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 17:02:04 +0530 Subject: [PATCH 05/10] Correct Lint error --- Lib/test/test_datetime.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 05af8499ff93e7..e21ecc8d7037ad 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -1,6 +1,7 @@ import unittest import sys import functools +import importlib.util from test.support.import_helper import import_fresh_module @@ -13,11 +14,9 @@ def load_tests(loader, tests, pattern): fresh=['datetime', '_pydatetime', '_strptime'], blocked=['_datetime'], ) - try: - import _datetime - has_datetime = True - except ImportError: - has_datetime = False + + # Check availability without importing _datetime + has_datetime = importlib.util.find_spec('_datetime') is not None fast_tests = import_fresh_module( TESTS, From 9fafddbacb2e68fdea9b0027a7ff25c6cacadb60 Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 18:05:15 +0530 Subject: [PATCH 06/10] gh-143959: Skip Fast datetime tests if _datetime is unavailable --- Lib/test/test_datetime.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index e21ecc8d7037ad..85214dfefe6962 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -1,7 +1,6 @@ import unittest import sys import functools -import importlib.util from test.support.import_helper import import_fresh_module @@ -15,8 +14,13 @@ def load_tests(loader, tests, pattern): blocked=['_datetime'], ) - # Check availability without importing _datetime - has_datetime = importlib.util.find_spec('_datetime') is not None + try: + import _datetime + except ImportError: + has_datetime = False + else: + has_datetime = True + del _datetime fast_tests = import_fresh_module( TESTS, @@ -54,7 +58,7 @@ def load_tests(loader, tests, pattern): class Wrapper(cls): @classmethod def setUpClass(cls_, module=module): - if suffix == "_Fast" and not has_datetime: + if module is fast_tests and not has_datetime: raise unittest.SkipTest("requires _datetime module") cls_._save_sys_modules = sys.modules.copy() From f21b5debc1825c5cfce2a5e5cd9f4151c383ae04 Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 18:31:10 +0530 Subject: [PATCH 07/10] restore --- Lib/test/test_datetime.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 85214dfefe6962..3206d29131b48f 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -8,12 +8,6 @@ def load_tests(loader, tests, pattern): try: - pure_tests = import_fresh_module( - TESTS, - fresh=['datetime', '_pydatetime', '_strptime'], - blocked=['_datetime'], - ) - try: import _datetime except ImportError: @@ -21,7 +15,11 @@ def load_tests(loader, tests, pattern): else: has_datetime = True del _datetime - + pure_tests = import_fresh_module( + TESTS, + fresh=['datetime', '_pydatetime', '_strptime'], + blocked=['_datetime'], + ) fast_tests = import_fresh_module( TESTS, fresh=['datetime', '_strptime'], From 20b694456482c5dd4c0c3f5415532bf591597438 Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Wed, 21 Jan 2026 09:29:28 +0530 Subject: [PATCH 08/10] Restore Changes --- Lib/test/test_datetime.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 3206d29131b48f..fed89e0b1dbf7c 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -15,16 +15,12 @@ def load_tests(loader, tests, pattern): else: has_datetime = True del _datetime - pure_tests = import_fresh_module( - TESTS, - fresh=['datetime', '_pydatetime', '_strptime'], - blocked=['_datetime'], - ) - fast_tests = import_fresh_module( - TESTS, - fresh=['datetime', '_strptime'], - blocked=['_pydatetime'], - ) + pure_tests = import_fresh_module(TESTS, + fresh=['datetime', '_pydatetime', '_strptime'], + blocked=['_datetime']) + fast_tests = import_fresh_module(TESTS, + fresh=['datetime', '_strptime'], + blocked=['_pydatetime']) finally: # XXX: import_fresh_module() is supposed to leave sys.module cache untouched, # XXX: but it does not, so we have to cleanup ourselves. From b5890680ac2fc25ff59a8812de69cc0547bd92ab Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Wed, 21 Jan 2026 15:33:27 +0530 Subject: [PATCH 09/10] Renamed Variable --- Lib/test/test_datetime.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index fed89e0b1dbf7c..32556bcb1847b1 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -11,9 +11,9 @@ def load_tests(loader, tests, pattern): try: import _datetime except ImportError: - has_datetime = False + has_datetime_ext = False else: - has_datetime = True + has_datetime_ext = True del _datetime pure_tests = import_fresh_module(TESTS, fresh=['datetime', '_pydatetime', '_strptime'], @@ -52,7 +52,7 @@ def load_tests(loader, tests, pattern): class Wrapper(cls): @classmethod def setUpClass(cls_, module=module): - if module is fast_tests and not has_datetime: + if module is fast_tests and not has_datetime_ext: raise unittest.SkipTest("requires _datetime module") cls_._save_sys_modules = sys.modules.copy() From 73a161a0341cb2f8acd4aea73d891eb1bc1b0a8e Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Wed, 21 Jan 2026 16:11:44 +0530 Subject: [PATCH 10/10] Debug --- Lib/test/test_datetime.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 32556bcb1847b1..598ca9a67f3ccb 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -18,17 +18,19 @@ def load_tests(loader, tests, pattern): pure_tests = import_fresh_module(TESTS, fresh=['datetime', '_pydatetime', '_strptime'], blocked=['_datetime']) - fast_tests = import_fresh_module(TESTS, - fresh=['datetime', '_strptime'], - blocked=['_pydatetime']) + fast_tests = None + if has_datetime_ext: + fast_tests = import_fresh_module(TESTS, + fresh=['datetime', '_strptime'], + blocked=['_pydatetime']) finally: # XXX: import_fresh_module() is supposed to leave sys.module cache untouched, # XXX: but it does not, so we have to cleanup ourselves. for modname in ['datetime', '_datetime', '_pydatetime', '_strptime']: sys.modules.pop(modname, None) - test_modules = [pure_tests, fast_tests] - test_suffixes = ["_Pure", "_Fast"] + test_modules = [pure_tests] + test_suffixes = ["_Pure"] # XXX(gb) First run all the _Pure tests, then all the _Fast tests. You might # not believe this, but in spite of all the sys.modules trickery running a _Pure @@ -52,9 +54,6 @@ def load_tests(loader, tests, pattern): class Wrapper(cls): @classmethod def setUpClass(cls_, module=module): - if module is fast_tests and not has_datetime_ext: - raise unittest.SkipTest("requires _datetime module") - cls_._save_sys_modules = sys.modules.copy() sys.modules[TESTS] = module sys.modules['datetime'] = module.datetime_module