Skip to content

Commit 9cae616

Browse files
committed
Apply suggestions
1 parent c3b3a35 commit 9cae616

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

Lib/test/datetimetester.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7297,7 +7297,9 @@ def test_update_type_cache(self):
72977297

72987298
@unittest.skipIf(_interpreters is None, "missing _interpreters module")
72997299
def test_static_type_concurrent_init_fini(self):
7300-
# gh-136421
7300+
# gh-136421: When a managed static extension type is concurrently
7301+
# used by multiple interpreters, there was a crash due to misjudging
7302+
# its first initialization stage or last finalization one.
73017303
script = textwrap.dedent("""
73027304
import threading
73037305
import _interpreters

Modules/_datetimemodule.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7335,18 +7335,18 @@ init_static_types(PyInterpreterState *interp, int reloading)
73357335
if (reloading) {
73367336
return 0;
73377337
}
7338-
if (_Py_IsMainInterpreter(interp)
7339-
&& PyType_HasFeature(&PyDateTime_DateType, Py_TPFLAGS_READY)) {
7340-
// This function was already called from PyInit__datetime()
7341-
return 0;
7338+
if (_Py_IsMainInterpreter(interp)) {
7339+
if (PyType_HasFeature(&PyDateTime_DateType, Py_TPFLAGS_READY)) {
7340+
// This function was already called from PyInit__datetime()
7341+
return 0;
7342+
}
7343+
// `&...` is not a constant expression according to a strict reading
7344+
// of C standards. Fill tp_base at run-time rather than statically.
7345+
// See https://bugs.python.org/issue40777
7346+
PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType;
7347+
PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;
73427348
}
73437349

7344-
// `&...` is not a constant expression according to a strict reading
7345-
// of C standards. Fill tp_base at run-time rather than statically.
7346-
// See https://bugs.python.org/issue40777
7347-
PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType;
7348-
PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;
7349-
73507350
/* Bases classes must be initialized before subclasses,
73517351
* so capi_types must have the types in the appropriate order. */
73527352
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
@@ -7569,11 +7569,13 @@ static PyModuleDef datetimemodule = {
75697569
PyMODINIT_FUNC
75707570
PyInit__datetime(void)
75717571
{
7572-
PyInterpreterState *interp = PyInterpreterState_Get();
7572+
PyInterpreterState *interp = _PyInterpreterState_GET();
75737573
// gh-136421: Ensure static types are fully finalized at the shutdown of
75747574
// the main interpreter rather than subinterpreters for concurrency.
7575-
assert(_Py_IsMainInterpreter(interp));
7576-
init_static_types(interp, 0);
7575+
assert(interp != NULL && _Py_IsMainInterpreter(interp));
7576+
if (init_static_types(interp, 0) < 0) {
7577+
return NULL;
7578+
}
75777579
return PyModuleDef_Init(&datetimemodule);
75787580
}
75797581

0 commit comments

Comments
 (0)