Replace deprecated PyWeakref_GetObject with PyWeakref_GetRef (closes #191)
PyWeakref_GetObject was deprecated in Python 3.13 and will be removed in 3.15. The new PyWeakref_GetRef API returns a strong reference directly and uses integer return codes for error handling. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a81430991c
commit
c2877c8053
1 changed files with 14 additions and 8 deletions
|
|
@ -37,14 +37,16 @@ void PythonObjectCache::registerObject(uint64_t serial, PyObject* weakref) {
|
|||
PyObject* PythonObjectCache::lookup(uint64_t serial) {
|
||||
if (serial == 0) return nullptr;
|
||||
|
||||
// No mutex needed for read - GIL protects PyWeakref_GetObject
|
||||
// No mutex needed for read - GIL protects PyWeakref_GetRef
|
||||
auto it = cache.find(serial);
|
||||
if (it != cache.end()) {
|
||||
PyObject* obj = PyWeakref_GetObject(it->second);
|
||||
if (obj && obj != Py_None) {
|
||||
Py_INCREF(obj);
|
||||
PyObject* obj = nullptr;
|
||||
int result = PyWeakref_GetRef(it->second, &obj);
|
||||
if (result == 1 && obj) {
|
||||
// obj is already a strong reference from PyWeakref_GetRef
|
||||
return obj;
|
||||
}
|
||||
// result == 0: dead reference, result == -1: error
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
|
@ -65,11 +67,15 @@ void PythonObjectCache::cleanup() {
|
|||
|
||||
auto it = cache.begin();
|
||||
while (it != cache.end()) {
|
||||
PyObject* obj = PyWeakref_GetObject(it->second);
|
||||
if (!obj || obj == Py_None) {
|
||||
PyObject* obj = nullptr;
|
||||
int result = PyWeakref_GetRef(it->second, &obj);
|
||||
if (result <= 0) {
|
||||
// Dead reference or error - remove from cache
|
||||
Py_DECREF(it->second);
|
||||
it = cache.erase(it);
|
||||
} else {
|
||||
// Still alive - release the strong reference we obtained
|
||||
Py_DECREF(obj);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue