Skip to content

Commit

Permalink
Another file moved to PyDict_GetItemRef. Issue #608
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldoussoren committed Jul 26, 2024
1 parent 9edc4b2 commit 8d6796a
Showing 1 changed file with 58 additions and 20 deletions.
78 changes: 58 additions & 20 deletions pyobjc-core/Modules/objc/super-call.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
PyObject* pyclass;
PyObject* entry;
PyObject* lst;
PyObject* py_selname;
int r;

PyObjC_Assert(special_registry != NULL, -1);

Expand All @@ -82,19 +84,27 @@
call_to_objc = PyObjCFFI_Caller;
}

py_selname = PyUnicode_FromString(sel_getName(sel));
if (py_selname == NULL) {
return -1;
}

if (class == nil) {
pyclass = Py_None;
Py_INCREF(Py_None);
} else {
pyclass = PyObjCClass_New(class);
if (pyclass == NULL) { // LCOV_BR_EXCL_LINE
Py_DECREF(py_selname);
return -1; // LCOV_EXCL_LINE
}
}

v = PyMem_Malloc(sizeof(*v));
if (v == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(py_selname);
Py_DECREF(pyclass);
PyErr_NoMemory();
return -1;
// LCOV_EXCL_STOP
Expand All @@ -105,6 +115,8 @@
entry = PyTuple_New(2);
if (entry == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(py_selname);
Py_DECREF(pyclass);
PyMem_Free(v);
return -1;
// LCOV_EXCL_STOP
Expand All @@ -116,30 +128,39 @@

if (PyTuple_GET_ITEM(entry, 1) == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(py_selname);
Py_DECREF(entry);
return -1;
// LCOV_EXCL_STOP
}

lst = PyDict_GetItemString(special_registry, sel_getName(sel));
if (lst == NULL && PyErr_Occurred()) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
r = PyDict_GetItemRef(special_registry, py_selname, &lst);

switch (r) {
case -1:
Py_DECREF(py_selname);
Py_DECREF(entry);
return -1;
// LCOV_EXCL_STOP
} else if (lst == NULL) {
case 0:
lst = PyList_New(0);
if (PyDict_SetItemString( // LCOV_BR_EXCL_LINE
special_registry, sel_getName(sel), lst)
if (lst == NULL) {
Py_DECREF(py_selname);
Py_DECREF(entry);
return -1;
}
if (PyDict_SetItem( // LCOV_BR_EXCL_LINE
special_registry, py_selname, lst)
== -1) {
// LCOV_EXCL_START
Py_DECREF(py_selname);
Py_DECREF(lst);
Py_DECREF(entry);
return -1;
// LCOV_EXCL_STOP
}
} else {
Py_INCREF(lst);
Py_DECREF(py_selname);

/* case 1: fallthrough */
}

if (PyList_Append(lst, entry) < 0) { // LCOV_BR_EXCL_LINE
Expand Down Expand Up @@ -230,25 +251,34 @@
PyObject* result = NULL;
PyObject* special_class = NULL;
PyObject* search_class = NULL;
PyObject* py_selname = NULL;
PyObject* lst;
Py_ssize_t i;
int r;

if (special_registry == NULL)
goto error;
if (!class)
goto error;

py_selname = PyUnicode_FromString(sel_getName(sel));
if (py_selname == NULL) {
goto error;
}

search_class = PyObjCClass_New(class);
if (search_class == NULL) // LCOV_BR_EXCL_LINE
goto error; // LCOV_EXCL_LINE

lst = PyDict_GetItemString(special_registry, sel_getName(sel));
if (lst == NULL) {
r = PyDict_GetItemRef(special_registry, py_selname, &lst);
switch (r) {
case -1:
goto error;
case 0:
goto error;
/* case 1: fallthrough */
}

Py_INCREF(lst);

/*
* Look for the most specific match:
* - class in the list item is either the class we're looking
Expand Down Expand Up @@ -282,6 +312,7 @@
Py_CLEAR(result);
special_class = pyclass;
result = PyTuple_GET_ITEM(entry, 1);
Py_INCREF(special_class);
Py_INCREF(result);
Py_DECREF(entry);

Expand All @@ -306,21 +337,27 @@
Py_DECREF(entry);
}
}
Py_XDECREF(search_class);
if (!result)
goto error;

Py_CLEAR(special_class);
Py_CLEAR(search_class);
Py_CLEAR(py_selname);

struct registry* rv = PyCapsule_GetPointer(result, "objc.__memblock__");
Py_DECREF(result);
return rv;

error:
return NULL;
Py_CLEAR(special_class);
Py_CLEAR(py_selname);
Py_CLEAR(search_class);
return NULL;
}

static struct registry* _Nullable find_signature(const char* signature)
{
PyObject* o;
PyObject* o = NULL;
int res;

if (signature_registry == NULL) {
Expand All @@ -344,12 +381,13 @@
_PyBytes_Resize(&key, strlen(PyBytes_AS_STRING(key)) + 1) == -1) {
return NULL; // LCOV_EXCL_LINE
}
o = PyDict_GetItemWithError(signature_registry, key);
Py_DECREF(key);
if (o == NULL)
if (PyDict_GetItemRef(signature_registry, key, &o) != 1) {
return NULL;
}

return PyCapsule_GetPointer(o, "objc.__memblock__");
struct registry* result = PyCapsule_GetPointer(o, "objc.__memblock__");
Py_DECREF(o);
return result;
}

PyObjC_CallFunc _Nullable PyObjC_FindCallFunc(Class class, SEL sel, const char* signature)
Expand Down

0 comments on commit 8d6796a

Please sign in to comment.