return result;
}
-PyObject *
-PyDict_GetItemProxy(PyObject *dict, PyObject *key)
+static int
+_PyDict_GetItemProxy(PyObject *dict, PyObject *key, PyObject **presult)
{
- PyObject *result;
PyObject *item = PyDict_GetItemWithError(dict, key);
+ if (item == NULL) {
+ if (PyErr_Occurred()) {
+ return -1;
+ }
+ *presult = NULL;
+ return 0;
+ }
- if (item == NULL)
- return NULL;
- if (!PyWeakref_CheckProxy(item))
- return item;
- result = PyWeakref_GET_OBJECT(item);
- if (result == Py_None)
- return NULL;
- return result;
+ if (!PyWeakref_CheckProxy(item)) {
+ *presult = Py_NewRef(item);
+ return 0;
+ }
+ PyObject *ref;
+ if (PyWeakref_GetRef(item, &ref) < 0) {
+ return -1;
+ }
+ // ref is NULL if the referenced object was destroyed
+ *presult = ref;
+ return 0;
}
/******************************************************************/
{
static PyObject *cache;
PyObject *key;
- PyObject *result;
char name[256];
PyObject *len;
Py_DECREF(len);
if (!key)
return NULL;
- result = PyDict_GetItemProxy(cache, key);
- if (result) {
- Py_INCREF(result);
+
+ PyObject *result;
+ if (_PyDict_GetItemProxy(cache, key, &result) < 0) {
Py_DECREF(key);
- return result;
+ return NULL;
}
- else if (PyErr_Occurred()) {
+ if (result) {
Py_DECREF(key);
- return NULL;
+ return result;
}
if (!PyType_Check(itemtype)) {