mirror of
https://github.com/python/cpython.git
synced 2025-09-14 12:46:49 +00:00
gh-105927: _ctypes use PyWeakref_GetRef() (#105964)
Rename PyDict_GetItemProxy() to _PyDict_GetItemProxy() and mark it as static. _PyDict_GetItemProxy() now returns a strong reference, instead of a borrowed reference: replace PyWeakref_GET_OBJECT() with _PyWeakref_GET_REF().
This commit is contained in:
parent
74da6f7c9f
commit
48d107a87d
1 changed files with 27 additions and 19 deletions
|
@ -238,20 +238,29 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *
|
static int
|
||||||
PyDict_GetItemProxy(PyObject *dict, PyObject *key)
|
_PyDict_GetItemProxy(PyObject *dict, PyObject *key, PyObject **presult)
|
||||||
{
|
{
|
||||||
PyObject *result;
|
|
||||||
PyObject *item = PyDict_GetItemWithError(dict, key);
|
PyObject *item = PyDict_GetItemWithError(dict, key);
|
||||||
|
if (item == NULL) {
|
||||||
|
if (PyErr_Occurred()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*presult = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (item == NULL)
|
if (!PyWeakref_CheckProxy(item)) {
|
||||||
return NULL;
|
*presult = Py_NewRef(item);
|
||||||
if (!PyWeakref_CheckProxy(item))
|
return 0;
|
||||||
return item;
|
}
|
||||||
result = PyWeakref_GET_OBJECT(item);
|
PyObject *ref;
|
||||||
if (result == Py_None)
|
if (PyWeakref_GetRef(item, &ref) < 0) {
|
||||||
return NULL;
|
return -1;
|
||||||
return result;
|
}
|
||||||
|
// ref is NULL if the referenced object was destroyed
|
||||||
|
*presult = ref;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************/
|
/******************************************************************/
|
||||||
|
@ -4832,7 +4841,6 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
|
||||||
{
|
{
|
||||||
static PyObject *cache;
|
static PyObject *cache;
|
||||||
PyObject *key;
|
PyObject *key;
|
||||||
PyObject *result;
|
|
||||||
char name[256];
|
char name[256];
|
||||||
PyObject *len;
|
PyObject *len;
|
||||||
|
|
||||||
|
@ -4848,16 +4856,16 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
|
||||||
Py_DECREF(len);
|
Py_DECREF(len);
|
||||||
if (!key)
|
if (!key)
|
||||||
return NULL;
|
return NULL;
|
||||||
result = PyDict_GetItemProxy(cache, key);
|
|
||||||
if (result) {
|
PyObject *result;
|
||||||
Py_INCREF(result);
|
if (_PyDict_GetItemProxy(cache, key, &result) < 0) {
|
||||||
Py_DECREF(key);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
else if (PyErr_Occurred()) {
|
|
||||||
Py_DECREF(key);
|
Py_DECREF(key);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
if (result) {
|
||||||
|
Py_DECREF(key);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (!PyType_Check(itemtype)) {
|
if (!PyType_Check(itemtype)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue