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
14 changes: 1 addition & 13 deletions cuda_core/cuda/core/_cpp/resource_handles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,7 @@ NvJitLinkDestroyFn p_nvJitLinkDestroy = nullptr;

namespace {

#if PY_VERSION_HEX < 0x030D0000
extern "C" int _Py_IsFinalizing(void);
#endif

inline bool py_is_finalizing() noexcept {
#if PY_VERSION_HEX >= 0x030D0000
return Py_IsFinalizing();
#else
// Python < 3.13 does not expose Py_IsFinalizing() publicly. Use the private
// API that exists in those versions.
return _Py_IsFinalizing() != 0;
#endif
}
using cuda_core::detail::py_is_finalizing;

// Helper to release the GIL while calling into the CUDA driver.
// This guard is *conditional*: if the caller already dropped the GIL,
Expand Down
16 changes: 16 additions & 0 deletions cuda_core/cuda/core/_cpp/resource_handles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,24 @@ inline std::intptr_t as_intptr(const CuLinkHandle& h) noexcept {

// as_py() - convert handle to Python wrapper object (returns new reference)
namespace detail {

#if PY_VERSION_HEX < 0x030D0000
extern "C" int _Py_IsFinalizing(void);
#endif

inline bool py_is_finalizing() noexcept {
#if PY_VERSION_HEX >= 0x030D0000
return Py_IsFinalizing();
#else
return _Py_IsFinalizing() != 0;
#endif
}

// n.b. class lookup is not cached to avoid deadlock hazard, see DESIGN.md
inline PyObject* make_py(const char* module_name, const char* class_name, std::intptr_t value) noexcept {
if (py_is_finalizing()) {
Py_RETURN_NONE;
}
PyObject* mod = PyImport_ImportModule(module_name);
if (!mod) return nullptr;
PyObject* cls = PyObject_GetAttrString(mod, class_name);
Expand Down
Loading