Skip to content

Commit 3dadc22

Browse files
authored
gh-141563: Add missing cast to _PyDateTime_IMPORT() (#144667)
Fix compilation on C++. Add test on PyDateTime_IMPORT in test_cext and test_cppext.
1 parent cc81707 commit 3dadc22

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Include/datetime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
198198

199199
static inline PyDateTime_CAPI *
200200
_PyDateTime_IMPORT(void) {
201-
PyDateTime_CAPI *val = _Py_atomic_load_ptr(&PyDateTimeAPI);
201+
PyDateTime_CAPI *val = (PyDateTime_CAPI *)_Py_atomic_load_ptr(&PyDateTimeAPI);
202202
if (val == NULL) {
203203
PyDateTime_CAPI *capi = (PyDateTime_CAPI *)PyCapsule_Import(
204204
PyDateTime_CAPSULE_NAME, 0);

Lib/test/test_cext/extension.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#endif
1212

1313
#include "Python.h"
14+
#include "datetime.h"
1415

1516
#ifdef TEST_INTERNAL_C_API
1617
// gh-135906: Check for compiler warnings in the internal C API.
@@ -50,8 +51,21 @@ _testcext_add(PyObject *Py_UNUSED(module), PyObject *args)
5051
}
5152

5253

54+
static PyObject *
55+
test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
56+
{
57+
PyDateTime_IMPORT;
58+
if (PyErr_Occurred()) {
59+
return NULL;
60+
}
61+
62+
Py_RETURN_NONE;
63+
}
64+
65+
5366
static PyMethodDef _testcext_methods[] = {
5467
{"add", _testcext_add, METH_VARARGS, _testcext_add_doc},
68+
{"test_datetime", test_datetime, METH_NOARGS, NULL},
5569
{NULL, NULL, 0, NULL} // sentinel
5670
};
5771

@@ -65,12 +79,18 @@ _testcext_exec(
6579
#endif
6680
)
6781
{
82+
PyObject *result;
83+
6884
#ifdef __STDC_VERSION__
6985
if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
7086
return -1;
7187
}
7288
#endif
7389

90+
result = PyObject_CallMethod(module, "test_datetime", "");
91+
if (!result) return -1;
92+
Py_DECREF(result);
93+
7494
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
7595
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
7696
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);

Lib/test/test_cppext/extension.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#endif
1212

1313
#include "Python.h"
14+
#include "datetime.h"
1415

1516
#ifdef TEST_INTERNAL_C_API
1617
// 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))
228229
Py_RETURN_NONE;
229230
}
230231

232+
static PyObject *
233+
test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
234+
{
235+
PyDateTime_IMPORT;
236+
if (PyErr_Occurred()) {
237+
return NULL;
238+
}
239+
240+
Py_RETURN_NONE;
241+
}
242+
231243
static PyMethodDef _testcppext_methods[] = {
232244
{"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc},
233245
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
234246
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
235247
{"test_virtual_object", test_virtual_object, METH_NOARGS, _Py_NULL},
248+
{"test_datetime", test_datetime, METH_NOARGS, _Py_NULL},
236249
// Note: _testcppext_exec currently runs all test functions directly.
237250
// When adding a new one, add a call there.
238251

@@ -261,6 +274,10 @@ _testcppext_exec(PyObject *module)
261274
if (!result) return -1;
262275
Py_DECREF(result);
263276

277+
result = PyObject_CallMethod(module, "test_datetime", "");
278+
if (!result) return -1;
279+
Py_DECREF(result);
280+
264281
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
265282
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
266283
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);

0 commit comments

Comments
 (0)