Per-frame UI callbacks (OnUpdate/OnRender/...) looked up methods with PyObject_GetAttrString, building a new PyString each call. Names are now interned once and cached; Py_AtExit drops the cache so a Py_Finalize/Py_Initialize cycle never reuses stale objects. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
351 lines
7.5 KiB
C++
351 lines
7.5 KiB
C++
#pragma once
|
|
|
|
#include <Python.h>
|
|
#include "PortPythonPointer.h"
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
|
|
inline PyObject * Py_BuildNone()
|
|
{
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
inline void Py_ReleaseNone()
|
|
{
|
|
Py_DECREF(Py_None);
|
|
}
|
|
|
|
inline PyObject * Py_BadArgument()
|
|
{
|
|
PyErr_BadArgument();
|
|
return nullptr;
|
|
}
|
|
|
|
inline PyObject * Py_BuildException(const char * c_pszErr = nullptr, ...)
|
|
{
|
|
if (!c_pszErr)
|
|
PyErr_Clear();
|
|
else
|
|
{
|
|
char szErrBuf[512+1];
|
|
va_list args;
|
|
va_start(args, c_pszErr);
|
|
vsnprintf(szErrBuf, sizeof(szErrBuf), c_pszErr, args);
|
|
va_end(args);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, szErrBuf);
|
|
}
|
|
|
|
return Py_BuildNone();
|
|
}
|
|
|
|
inline bool PyTuple_GetObject(PyObject* poArgs, int pos, PyObject** ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
*ret = poItem;
|
|
return true;
|
|
}
|
|
|
|
inline bool PyTuple_GetLong(PyObject* poArgs, int pos, long* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem || !PyInt_Check(poItem))
|
|
return false;
|
|
|
|
*ret = PyInt_AsLong(poItem);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyTuple_GetDouble(PyObject* poArgs, int pos, double* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
if (PyFloat_Check(poItem)) {
|
|
*ret = PyFloat_AsDouble(poItem);
|
|
return true;
|
|
} else if (PyInt_Check(poItem)) {
|
|
*ret = (double)PyInt_AsLong(poItem);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
inline bool PyTuple_GetFloat(PyObject* poArgs, int pos, float* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
if (PyFloat_Check(poItem)) {
|
|
*ret = (float)PyFloat_AsDouble(poItem);
|
|
return true;
|
|
} else if (PyInt_Check(poItem)) {
|
|
*ret = (float)PyInt_AsLong(poItem);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, int* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
if (PyInt_Check(poItem))
|
|
{
|
|
*ret = (int)PyInt_AsLong(poItem);
|
|
return true;
|
|
}
|
|
if (PyLong_Check(poItem))
|
|
{
|
|
*ret = (int)PyLong_AsUnsignedLongMask(poItem);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
inline bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret)
|
|
{
|
|
int val = 0;
|
|
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
|
*ret = (unsigned char)val;
|
|
return result;
|
|
}
|
|
|
|
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned char* ret)
|
|
{
|
|
int val = 0;
|
|
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
|
*ret = (unsigned char)val;
|
|
return result;
|
|
}
|
|
|
|
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned short* ret)
|
|
{
|
|
int val = 0;
|
|
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
|
*ret = (unsigned short)val;
|
|
return result;
|
|
}
|
|
|
|
inline bool PyTuple_GetLongLong(PyObject* poArgs, int pos, long long* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
*ret = PyLong_AsLongLong(poItem);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyTuple_GetUnsignedLong(PyObject* poArgs, int pos, unsigned long* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
*ret = PyLong_AsUnsignedLong(poItem);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyTuple_GetUnsignedInteger(PyObject* poArgs, int pos, unsigned int* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
*ret = (unsigned int)PyLong_AsUnsignedLong(poItem);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyTuple_GetString(PyObject* poArgs, int pos, char** ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem || !PyString_Check(poItem))
|
|
return false;
|
|
|
|
*ret = PyString_AsString(poItem);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyTuple_GetBoolean(PyObject* poArgs, int pos, bool* ret)
|
|
{
|
|
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
|
return false;
|
|
|
|
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
|
if (!poItem)
|
|
return false;
|
|
|
|
*ret = PyLong_AsLong(poItem) ? true : false;
|
|
return true;
|
|
}
|
|
|
|
// Interned method-name strings: UI windows call OnUpdate/OnRender/... every frame, and
|
|
// PyObject_GetAttrString would build a fresh PyString per call. The cache is dropped through
|
|
// Py_AtExit, so a Py_Finalize/Py_Initialize cycle never sees stale objects.
|
|
struct __PyNameHash
|
|
{
|
|
using is_transparent = void;
|
|
size_t operator()(std::string_view s) const noexcept { return std::hash<std::string_view>{}(s); }
|
|
};
|
|
inline std::unordered_map<std::string, PyObject*, __PyNameHash, std::equal_to<>>& __PyNameCache()
|
|
{
|
|
static std::unordered_map<std::string, PyObject*, __PyNameHash, std::equal_to<>> cache;
|
|
return cache;
|
|
}
|
|
inline bool& __PyNameCacheRegistered()
|
|
{
|
|
static bool registered = false;
|
|
return registered;
|
|
}
|
|
inline void __PyClearNameCache()
|
|
{
|
|
// Runs inside Py_Finalize after the interpreter released its objects; only forget the pointers.
|
|
__PyNameCache().clear();
|
|
__PyNameCacheRegistered() = false;
|
|
}
|
|
inline PyObject* __PyInternedName(const char* c_szName)
|
|
{
|
|
auto& cache = __PyNameCache();
|
|
auto it = cache.find(std::string_view(c_szName));
|
|
if (it != cache.end())
|
|
return it->second;
|
|
if (!__PyNameCacheRegistered())
|
|
{
|
|
if (Py_AtExit(__PyClearNameCache) != 0)
|
|
return nullptr;
|
|
__PyNameCacheRegistered() = true;
|
|
}
|
|
PyObject* poName = PyString_InternFromString(c_szName);
|
|
if (!poName)
|
|
{
|
|
PyErr_Clear();
|
|
return nullptr;
|
|
}
|
|
cache.emplace(c_szName, poName); // the cache owns this reference
|
|
return poName;
|
|
}
|
|
|
|
inline bool __PyCallClassMemberFunc_ByCString(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, PyObject** ppoRet)
|
|
{
|
|
if (!poClass)
|
|
{
|
|
Py_XDECREF(poArgs);
|
|
return false;
|
|
}
|
|
|
|
PyObject * poName = __PyInternedName(c_szFunc);
|
|
PyObject * poFunc = poName ? PyObject_GetAttr(poClass, poName) : PyObject_GetAttrString(poClass, (char *)c_szFunc);
|
|
if (!poFunc)
|
|
{
|
|
PyErr_Clear();
|
|
Py_XDECREF(poArgs);
|
|
return false;
|
|
}
|
|
|
|
if (!PyCallable_Check(poFunc))
|
|
{
|
|
Py_DECREF(poFunc);
|
|
Py_XDECREF(poArgs);
|
|
return false;
|
|
}
|
|
|
|
PyObject * poRet = PyObject_CallObject(poFunc, poArgs);
|
|
if (!poRet)
|
|
{
|
|
PyErr_Print();
|
|
Py_DECREF(poFunc);
|
|
Py_XDECREF(poArgs);
|
|
return false;
|
|
}
|
|
|
|
*ppoRet = poRet;
|
|
Py_DECREF(poFunc);
|
|
Py_XDECREF(poArgs);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs)
|
|
{
|
|
PyObject* poRet = nullptr;
|
|
if (!poClass)
|
|
{
|
|
Py_XDECREF(poArgs);
|
|
return false;
|
|
}
|
|
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
|
return false;
|
|
Py_DECREF(poRet);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, bool* pisRet)
|
|
{
|
|
PyObject* poRet = nullptr;
|
|
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
|
return false;
|
|
|
|
if (PyNumber_Check(poRet))
|
|
*pisRet = (PyLong_AsLong(poRet) != 0);
|
|
else
|
|
*pisRet = true;
|
|
|
|
Py_DECREF(poRet);
|
|
return true;
|
|
}
|
|
|
|
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, long * plRetValue)
|
|
{
|
|
PyObject* poRet = nullptr;
|
|
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
|
return false;
|
|
|
|
if (PyNumber_Check(poRet))
|
|
{
|
|
*plRetValue = PyLong_AsLong(poRet);
|
|
Py_DECREF(poRet);
|
|
return true;
|
|
}
|
|
|
|
Py_DECREF(poRet);
|
|
return false;
|
|
}
|