gh-141563: Add missing cast to _PyDateTime_IMPORT() (#144667)

Fix compilation on C++.

Add test on PyDateTime_IMPORT in test_cext and test_cppext.
This commit is contained in:
Victor Stinner 2026-02-10 15:47:12 +01:00 committed by GitHub
parent cc81707e40
commit 3dadc22a27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 1 deletions

View file

@ -11,6 +11,7 @@
#endif
#include "Python.h"
#include "datetime.h"
#ifdef TEST_INTERNAL_C_API
// gh-135906: Check for compiler warnings in the internal C API
@ -228,11 +229,23 @@ test_virtual_object(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
Py_RETURN_NONE;
}
static PyObject *
test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyDateTime_IMPORT;
if (PyErr_Occurred()) {
return NULL;
}
Py_RETURN_NONE;
}
static PyMethodDef _testcppext_methods[] = {
{"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc},
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
{"test_virtual_object", test_virtual_object, METH_NOARGS, _Py_NULL},
{"test_datetime", test_datetime, METH_NOARGS, _Py_NULL},
// Note: _testcppext_exec currently runs all test functions directly.
// When adding a new one, add a call there.
@ -261,6 +274,10 @@ _testcppext_exec(PyObject *module)
if (!result) return -1;
Py_DECREF(result);
result = PyObject_CallMethod(module, "test_datetime", "");
if (!result) return -1;
Py_DECREF(result);
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);