You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
datetime has a C API that we expose through a global variable. This does not work well under multiple threads. For example, if PyDateTime_IMPORT fails in one thread while another thread is invoking the datetime C API, then that will set PyDateTimeAPI to NULL, leading to NULL pointer dereferences in the original thread.
This global variable also forces us to do some nasty hacks with subinterpreters because it exposes static types. I helped fix this a few months ago by making _datetime static (see this thread), but there are probably other issues that I haven't found yet.
I think a bandaid fix would be to make PyDateTimeAPI a thread-local variable, but I suspect that would break a lot of things, nor would that fix this for subinterpreters. More generally, we could deprecate PyDateTime_IMPORT and PyDateTimeAPI in favor of letting users manage the pointer on their own, but I don't know if we can do this without too many code changes (see #141543 and #83785 for the docs side).
Has anyone found any examples of this on GitHub? I imagine that this might happen because it seems like it would be common to use dateline to measure the time of something executing but at the same time why use datetime itself? There has to be better libs or inbuilt functions for most use cases
I think that will probably break code that relies on PyDateTimeAPI for checking if PyDateTime_IMPORT failed, so that's probably too much for a patch release. Maybe it'll work for 3.15 though?
Activity
sharktide commentedon Nov 15, 2025
Has anyone found any examples of this on GitHub? I imagine that this might happen because it seems like it would be common to use dateline to measure the time of something executing but at the same time why use datetime itself? There has to be better libs or inbuilt functions for most use cases
ZeroIntensity commentedon Nov 15, 2025
Yes.
kumaraditya303 commentedon Jan 21, 2026
I think we can make it thread safe with atomics, basically create a exchange loop and only initialize it if it is not initialized already.
Something like this should work:
ZeroIntensity commentedon Jan 21, 2026
I think that will probably break code that relies on
PyDateTimeAPIfor checking ifPyDateTime_IMPORTfailed, so that's probably too much for a patch release. Maybe it'll work for 3.15 though?26 remaining items
pythongh-141563: Fix test_cext on Windows (python#144677)
pythongh-141563: Fix test_cppext on macOS (python#144685)
pythongh-141563: Enable test_cppext internal C API tests on macOS (py…
pythongh-141563: Add missing cast to _PyDateTime_IMPORT() (python#144667
pythongh-141563: Don't test datetime.h with the limited C API (python…
pythongh-141563: Fix test_cext on Windows (python#144677)
pythongh-141563: Fix test_cppext on macOS (python#144685)
pythongh-141563: Enable test_cppext internal C API tests on macOS (py…
pythongh-141563: make `PyDateTime_IMPORT` thread-safe (python#144210)
pythongh-141563: Add missing cast to _PyDateTime_IMPORT() (python#144667
pythongh-141563: Don't test datetime.h with the limited C API (python…
pythongh-141563: Fix test_cext on Windows (python#144677)
pythongh-141563: Fix test_cppext on macOS (python#144685)
pythongh-141563: Enable test_cppext internal C API tests on macOS (py…