diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-06-13 18:30:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-13 18:30:42 (GMT) |
commit | 15c3d0013d5e653c63171dc5daa533ed45ba811f (patch) | |
tree | 47bb90dbedd928d818d771f4890c5144db9f5bcb /Modules | |
parent | e0f4dd8ec5d26c2c59893ebe476090892cbfe233 (diff) | |
download | cpython-15c3d0013d5e653c63171dc5daa533ed45ba811f.zip cpython-15c3d0013d5e653c63171dc5daa533ed45ba811f.tar.gz cpython-15c3d0013d5e653c63171dc5daa533ed45ba811f.tar.bz2 |
[3.13] gh-117398: Add datetime C-API type check test for subinterpreters (gh-120463)
Check if the DateTime C-API type matches the datetime.date type on main and shared/isolated subinterpreters.
(cherry picked from commit 50a389565aa0b480792ed06a2ab56fb5a72fc2d8, AKA gh-119604)
Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_testcapi/datetime.c | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/Modules/_testcapi/datetime.c b/Modules/_testcapi/datetime.c index b179603..f3d5421 100644 --- a/Modules/_testcapi/datetime.c +++ b/Modules/_testcapi/datetime.c @@ -22,10 +22,17 @@ test_datetime_capi(PyObject *self, PyObject *args) test_run_counter++; PyDateTime_IMPORT; - if (PyDateTimeAPI) { - Py_RETURN_NONE; + if (PyDateTimeAPI == NULL) { + return NULL; } - return NULL; + // The following C API types need to outlive interpreters, since the + // borrowed references to them can be held by users without being updated. + assert(!PyType_HasFeature(PyDateTimeAPI->DateType, Py_TPFLAGS_HEAPTYPE)); + assert(!PyType_HasFeature(PyDateTimeAPI->TimeType, Py_TPFLAGS_HEAPTYPE)); + assert(!PyType_HasFeature(PyDateTimeAPI->DateTimeType, Py_TPFLAGS_HEAPTYPE)); + assert(!PyType_HasFeature(PyDateTimeAPI->DeltaType, Py_TPFLAGS_HEAPTYPE)); + assert(!PyType_HasFeature(PyDateTimeAPI->TZInfoType, Py_TPFLAGS_HEAPTYPE)); + Py_RETURN_NONE; } /* Functions exposing the C API type checking for testing */ @@ -479,3 +486,38 @@ _PyTestCapi_Init_DateTime(PyObject *mod) } return 0; } + + +/* --------------------------------------------------------------------------- + * Test module for subinterpreters. + */ + +static int +_testcapi_datetime_exec(PyObject *mod) +{ + if (test_datetime_capi(NULL, NULL) == NULL) { + return -1; + } + return 0; +} + +static PyModuleDef_Slot _testcapi_datetime_slots[] = { + {Py_mod_exec, _testcapi_datetime_exec}, + {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, + {0, NULL}, +}; + +static struct PyModuleDef _testcapi_datetime_module = { + PyModuleDef_HEAD_INIT, + .m_name = "_testcapi_datetime", + .m_size = 0, + .m_methods = test_methods, + .m_slots = _testcapi_datetime_slots, +}; + +PyMODINIT_FUNC +PyInit__testcapi_datetime(void) +{ + return PyModuleDef_Init(&_testcapi_datetime_module); +} |