Skip to content

Commit af57832

Browse files
[3.13] gh-117398: Add multiphase support to _datetime (gh-119694)
This is an unrevert of d58ebf0 (gh-119636), which was reverted by 9216a53 (gh-119639) due to problems which have been resolved. This is minimal support for multiphase init. Subinterpreters are not supported yet. That will be addressed in a later change. (cherry picked from commit 3e8b609) Co-authored-by: Erlend E. Aasland [email protected]
1 parent a7aa7c4 commit af57832

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

Lib/test/datetimetester.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@
4747
pass
4848
#
4949

50+
# This is copied from test_import/__init__.py.
51+
def no_rerun(reason):
52+
"""Skip rerunning for a particular test.
53+
54+
WARNING: Use this decorator with care; skipping rerunning makes it
55+
impossible to find reference leaks. Provide a clear reason for skipping the
56+
test using the 'reason' parameter.
57+
"""
58+
def deco(func):
59+
_has_run = False
60+
def wrapper(self):
61+
nonlocal _has_run
62+
if _has_run:
63+
self.skipTest(reason)
64+
func(self)
65+
_has_run = True
66+
return wrapper
67+
return deco
68+
5069
pickle_loads = {pickle.loads, pickle._loads}
5170

5271
pickle_choices = [(pickle, pickle, proto)
@@ -6383,6 +6402,7 @@ class IranTest(ZoneInfoTest):
63836402

63846403

63856404
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
6405+
@no_rerun("the encapsulated datetime C API does not support reloading")
63866406
class CapiTest(unittest.TestCase):
63876407
def setUp(self):
63886408
# Since the C API is not present in the _Pure tests, skip all tests

Modules/_datetimemodule.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7048,30 +7048,26 @@ _datetime_exec(PyObject *module)
70487048
}
70497049
#undef DATETIME_ADD_MACRO
70507050

7051-
static struct PyModuleDef datetimemodule = {
7051+
static PyModuleDef_Slot module_slots[] = {
7052+
{Py_mod_exec, _datetime_exec},
7053+
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
7054+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
7055+
{0, NULL},
7056+
};
7057+
7058+
static PyModuleDef datetimemodule = {
70527059
.m_base = PyModuleDef_HEAD_INIT,
70537060
.m_name = "_datetime",
70547061
.m_doc = "Fast implementation of the datetime type.",
7055-
.m_size = -1,
7062+
.m_size = 0,
70567063
.m_methods = module_methods,
7064+
.m_slots = module_slots,
70577065
};
70587066

70597067
PyMODINIT_FUNC
70607068
PyInit__datetime(void)
70617069
{
7062-
PyObject *mod = PyModule_Create(&datetimemodule);
7063-
if (mod == NULL)
7064-
return NULL;
7065-
#ifdef Py_GIL_DISABLED
7066-
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
7067-
#endif
7068-
7069-
if (_datetime_exec(mod) < 0) {
7070-
Py_DECREF(mod);
7071-
return NULL;
7072-
}
7073-
7074-
return mod;
7070+
return PyModuleDef_Init(&datetimemodule);
70757071
}
70767072

70777073
/* ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)