Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 48d107a

Browse filesBrowse files
authored
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().
1 parent 74da6f7 commit 48d107a
Copy full SHA for 48d107a

File tree

Expand file treeCollapse file tree

1 file changed

+26
-18
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+26
-18
lines changed

‎Modules/_ctypes/_ctypes.c

Copy file name to clipboardExpand all lines: Modules/_ctypes/_ctypes.c
+26-18Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,29 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
238238
return result;
239239
}
240240

241-
PyObject *
242-
PyDict_GetItemProxy(PyObject *dict, PyObject *key)
241+
static int
242+
_PyDict_GetItemProxy(PyObject *dict, PyObject *key, PyObject **presult)
243243
{
244-
PyObject *result;
245244
PyObject *item = PyDict_GetItemWithError(dict, key);
245+
if (item == NULL) {
246+
if (PyErr_Occurred()) {
247+
return -1;
248+
}
249+
*presult = NULL;
250+
return 0;
251+
}
246252

247-
if (item == NULL)
248-
return NULL;
249-
if (!PyWeakref_CheckProxy(item))
250-
return item;
251-
result = PyWeakref_GET_OBJECT(item);
252-
if (result == Py_None)
253-
return NULL;
254-
return result;
253+
if (!PyWeakref_CheckProxy(item)) {
254+
*presult = Py_NewRef(item);
255+
return 0;
256+
}
257+
PyObject *ref;
258+
if (PyWeakref_GetRef(item, &ref) < 0) {
259+
return -1;
260+
}
261+
// ref is NULL if the referenced object was destroyed
262+
*presult = ref;
263+
return 0;
255264
}
256265

257266
/******************************************************************/
@@ -4832,7 +4841,6 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
48324841
{
48334842
static PyObject *cache;
48344843
PyObject *key;
4835-
PyObject *result;
48364844
char name[256];
48374845
PyObject *len;
48384846

@@ -4848,15 +4856,15 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
48484856
Py_DECREF(len);
48494857
if (!key)
48504858
return NULL;
4851-
result = PyDict_GetItemProxy(cache, key);
4852-
if (result) {
4853-
Py_INCREF(result);
4859+
4860+
PyObject *result;
4861+
if (_PyDict_GetItemProxy(cache, key, &result) < 0) {
48544862
Py_DECREF(key);
4855-
return result;
4863+
return NULL;
48564864
}
4857-
else if (PyErr_Occurred()) {
4865+
if (result) {
48584866
Py_DECREF(key);
4859-
return NULL;
4867+
return result;
48604868
}
48614869

48624870
if (!PyType_Check(itemtype)) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.