From 7c31d02df3f7abeadadf9307ebc8a959f2be2fb7 Mon Sep 17 00:00:00 2001 From: "Martin K. Scherer" Date: Mon, 18 Nov 2024 20:30:27 +0100 Subject: [PATCH] guard refcnt for nogil python --- native/common/jp_tracer.cpp | 8 +++++++- native/python/jp_pythontypes.cpp | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/native/common/jp_tracer.cpp b/native/common/jp_tracer.cpp index e6601f483..9f98dd15a 100644 --- a/native/common/jp_tracer.cpp +++ b/native/common/jp_tracer.cpp @@ -150,7 +150,13 @@ void JPypeTracer::tracePythonObject(const char* msg, PyObject* ref) if (ref != nullptr) { std::stringstream str; - str << msg << " " << (void*) ref << " " << Py_REFCNT(ref) << " " << Py_TYPE(ref)->tp_name; + str << msg << " " << (void*) ref << " "<< + #ifndef Py_GIL_DISABLED + Py_REFCNT(ref) + #else + -1 + #endif + << " " << Py_TYPE(ref)->tp_name; JPypeTracer::trace1("PY", str.str().c_str()); } else diff --git a/native/python/jp_pythontypes.cpp b/native/python/jp_pythontypes.cpp index dca3d2507..89c828023 100644 --- a/native/python/jp_pythontypes.cpp +++ b/native/python/jp_pythontypes.cpp @@ -22,6 +22,7 @@ static void assertValid(PyObject *obj) { +#ifndef Py_GIL_DISABLED if (Py_REFCNT(obj) >= 1) return; @@ -34,6 +35,9 @@ static void assertValid(PyObject *obj) JP_TRACE_PY("pyref FAULT", obj); JP_RAISE(PyExc_SystemError, "Deleted reference"); // GCOVR_EXCL_STOP +#else + return; // GIL is disabled; we assume obj is valid +#endif } /**