https://github.com/python/cpython/commit/3dadc22a2796af7718f1aec02e30f100ac6553bd
commit: 3dadc22a2796af7718f1aec02e30f100ac6553bd
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-02-10T14:47:12Z
summary:

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

Fix compilation on C++.

Add test on PyDateTime_IMPORT in test_cext and test_cppext.

files:
M Include/datetime.h
M Lib/test/test_cext/extension.c
M Lib/test/test_cppext/extension.cpp

diff --git a/Include/datetime.h b/Include/datetime.h
index ed7e55009d2208..66e6c6e3ac3575 100644
--- a/Include/datetime.h
+++ b/Include/datetime.h
@@ -198,7 +198,7 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
 
 static inline PyDateTime_CAPI *
 _PyDateTime_IMPORT(void) {
-    PyDateTime_CAPI *val = _Py_atomic_load_ptr(&PyDateTimeAPI);
+    PyDateTime_CAPI *val = (PyDateTime_CAPI 
*)_Py_atomic_load_ptr(&PyDateTimeAPI);
     if (val == NULL) {
         PyDateTime_CAPI *capi = (PyDateTime_CAPI *)PyCapsule_Import(
             PyDateTime_CAPSULE_NAME, 0);
diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c
index 0f668c1da32d6e..20c2b6e89d8e17 100644
--- a/Lib/test/test_cext/extension.c
+++ b/Lib/test/test_cext/extension.c
@@ -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.
@@ -50,8 +51,21 @@ _testcext_add(PyObject *Py_UNUSED(module), PyObject *args)
 }
 
 
+static PyObject *
+test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
+{
+    PyDateTime_IMPORT;
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
+
+    Py_RETURN_NONE;
+}
+
+
 static PyMethodDef _testcext_methods[] = {
     {"add", _testcext_add, METH_VARARGS, _testcext_add_doc},
+    {"test_datetime", test_datetime, METH_NOARGS, NULL},
     {NULL, NULL, 0, NULL}  // sentinel
 };
 
@@ -65,12 +79,18 @@ _testcext_exec(
 #endif
     )
 {
+    PyObject *result;
+
 #ifdef __STDC_VERSION__
     if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
         return -1;
     }
 #endif
 
+    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);
diff --git a/Lib/test/test_cppext/extension.cpp 
b/Lib/test/test_cppext/extension.cpp
index b631ddad7201f0..51271250366429 100644
--- a/Lib/test/test_cppext/extension.cpp
+++ b/Lib/test/test_cppext/extension.cpp
@@ -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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to