Use pythoncapi-compat to simplify Python C API backwards compatibility

This commit is contained in:
kb1000
2024-02-18 14:08:04 +01:00
committed by Baldur Karlsson
parent d0a24b31cd
commit bb7ec408cb
12 changed files with 1163 additions and 80 deletions
@@ -26,6 +26,8 @@
#include <atomic>
#include "3rdparty/pythoncapi_compat.h"
// this is defined elsewhere for managing the opaque global_handle object
extern "C" PyThreadState *GetExecutingThreadState(PyObject *global_handle);
extern "C" PyObject *GetCurrentGlobalHandle();
@@ -178,11 +180,7 @@ inline void get_return(const char *funcname, PyObject *result, PyObject *global_
struct PyObjectRefCounter
{
PyObjectRefCounter(PyObject *o) : obj(o) { Py_INCREF(obj); }
PyObjectRefCounter(const PyObjectRefCounter &o)
{
obj = o.obj;
Py_INCREF(obj);
}
PyObjectRefCounter(const PyObjectRefCounter &o) { obj = Py_NewRef(o.obj); }
~PyObjectRefCounter()
{
// it may not be safe at the point this is destroyed to decref the object. For example if a
@@ -269,8 +267,7 @@ struct ScopedFuncCall
{
ScopedFuncCall(PyObject *h)
{
handle = h;
Py_XINCREF(handle);
handle = Py_XNewRef(h);
gil = PyGILState_Ensure();
}
@@ -288,7 +285,7 @@ template <typename funcType>
funcType ConvertFunc(const char *funcname, PyObject *func, ExceptionHandler exHandle)
{
// allow None to indicate no callback
if(func == Py_None)
if(Py_IsNone(func))
return funcType();
// add a reference to the global object so it stays alive while we execute, in case this is an