Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
apply suggested changes
  • Loading branch information
neonene committed Apr 30, 2024
commit fb0f5efdf95cf56b288ce70bfddac18addf22fda
2 changes: 1 addition & 1 deletion Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,7 @@ def test_datetime_capi_client(self):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
""")
exec(script)
exec(script) # run main interp first
ret = support.run_in_subinterp(script)
self.assertEqual(ret, 0)

Expand Down
2 changes: 1 addition & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ _PyDateTimeAPI_Import(void)
if (capi) {
// PyInit__datetime() is not called when the module is already loaded
// with single-phase init.
set_datetime_capi_by_interp((PyDateTime_CAPI *)capi);
set_datetime_capi_by_interp(capi);
}
}

Expand Down
49 changes: 46 additions & 3 deletions Modules/_testmultiphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -956,16 +956,59 @@ PyInit__test_shared_gil_only(void)

#include "datetime.h"

static int
datetime_capi_import_with_error(void)
{
static int multiphase = -1;
int ismain = PyInterpreterState_Get() == PyInterpreterState_Main();
if (ismain) {
PyObject *module = PyImport_ImportModule("_datetime");
if (module == NULL) {
return -1;
}
PyModuleDef *def = PyModule_GetDef(module);
Py_DECREF(module);
if (def && def->m_size >= 0) {
multiphase = 1;
}
else {
multiphase = 0;
}
}
if (multiphase < 0) {
PyErr_SetString(PyExc_AssertionError,
"Main interpreter must be loaded first.");
return -1;
}

_PyDateTimeAPI_Import();
if (!PyErr_Occurred()) {
return 0;
}
#ifdef Py_GIL_DISABLED
if (!ismain && !multiphase) {
PyErr_WriteUnraisable(NULL);
return 0;
}
#endif
return -1;
}

static int
datetime_capi_client_exec(PyObject *m)
{
_PyDateTimeAPI_Clear();
if (PyDateTimeAPI != NULL) {
if (_PyDateTimeAPI_Get() != NULL) {
PyErr_SetString(PyExc_AssertionError,
"DateTime API is expected to remain NULL.");
return -1;
}
if (datetime_capi_import_with_error() < 0) {
return -1;
}
PyDateTime_IMPORT;
PyErr_Clear();
if (PyDateTimeAPI != PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)) {
PyErr_SetString(PyExc_AssertionError,
"DateTime API does not match Capsule CAPI.");
return -1;
}
PyErr_Clear();
Expand Down