Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 17 additions & 2 deletions Include/datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,23 @@ typedef struct {
/* Define global variable for the C API and a macro for setting it. */
static PyDateTime_CAPI *PyDateTimeAPI = NULL;

#define PyDateTime_IMPORT \
PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
static inline PyDateTime_CAPI *

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sup non invert,'s ok!

_PyDateTime_IMPORT(void) {
PyDateTime_CAPI *val = _Py_atomic_load_ptr(&PyDateTimeAPI);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now seeing this error in external code that includes datetime.h:

…/cpython_install/include/python3.15/datetime.h:201:22: error: cannot initialize a variable of type 'PyDateTime_CAPI *' with an rvalue of type 'void *'
  201 |     PyDateTime_CAPI *val = _Py_atomic_load_ptr(&PyDateTimeAPI);
      |                      ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Probably just missing a cast.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you get the error with C or C++?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, there is a compiler error in C++. I wrote #144667 to add the missing cast, but also to add checks for compiler warnings (to test_cext and test_cppext).

if (val == NULL) {
PyDateTime_CAPI *capi = (PyDateTime_CAPI *)PyCapsule_Import(
PyDateTime_CAPSULE_NAME, 0);
if (capi != NULL) {
/* if the compare exchange fails then in that case
another thread would have initialized it */
_Py_atomic_compare_exchange_ptr(&PyDateTimeAPI, &val, (void *)capi);
return capi;
}
}
return val;
}

#define PyDateTime_IMPORT _PyDateTime_IMPORT()

/* Macro for access to the UTC singleton */
#define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix thread safety of :c:macro:`! PyDateTime_IMPORT`.
Loading