Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ronaldoussoren/pyobjc
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldoussoren committed Jan 1, 2025
2 parents 0ba7ff5 + 5c95ae0 commit 3498633
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 153 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ borrowed references).
getting channel data (``floatChannelData``, ``int16ChannelData`` and
``int32ChannelData``)

* :issue:`632`: fix broken bindings for ``CGWindowListCreateImageFromArray``.

Version 10.3.2
--------------

Expand Down
8 changes: 4 additions & 4 deletions pyobjc-core/Modules/objc/OC_PythonObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ - (void)doesNotRecognizeSelector:(SEL)aSelector
static PyObject* _Nullable get_method_for_selector(PyObject* obj, SEL aSelector)
{
const char* meth_name;
char pymeth_name[256];
Py_ssize_t argcount;
PyObject* pymethod;
const char* p;
Expand All @@ -204,12 +203,13 @@ - (void)doesNotRecognizeSelector:(SEL)aSelector
}
}

const char* py_meth_name =
PyObjC_SELToPythonName(aSelector, pymeth_name, sizeof(pymeth_name) - 1);
PyObject* py_meth_name =
PyObjC_SELToPythonName(aSelector);
if (py_meth_name == NULL) {
return NULL;
}
pymethod = PyObject_GetAttrString(obj, py_meth_name);
pymethod = PyObject_GetAttr(obj, py_meth_name);
Py_CLEAR(py_meth_name);
if (pymethod == NULL) {
return NULL;
}
Expand Down
26 changes: 9 additions & 17 deletions pyobjc-core/Modules/objc/method-accessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@
PyObject* res;
Method* methods;
unsigned int i, method_count;
char buf[256];
Class objc_class;

if (PyObjCClass_Check(self)) {
Expand Down Expand Up @@ -239,10 +238,10 @@

for (i = 0; i < method_count; i++) {
PyObject* v;
char* name;
PyObject* py_name;

name = PyObjC_SELToPythonName(method_getName(methods[i]), buf, sizeof(buf));
if (name == NULL) { // LCOV_BR_EXCL_LINE
py_name = PyObjC_SELToPythonName(method_getName(methods[i]));
if (py_name == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
free(methods);
Py_DECREF(res);
Expand All @@ -251,10 +250,10 @@
}

/* XXX: This needs some documentation. Basically resolve the method
* through normal lookup first, that avoid replicating objc_object.tp_getattro
* through normal lookup first, that avoids replicating objc_object.tp_getattro
* here.
*/
v = PyObject_GetAttrString(self, name);
v = PyObject_GetAttr(self, py_name);
if (v == NULL) {
PyErr_Clear();

Expand All @@ -278,6 +277,7 @@
PyErr_SetString(PyObjCExc_Error,
"Native selector with Nil type encoding");
free(methods);
Py_CLEAR(py_name);
Py_DECREF(res);
return NULL;
// LCOV_EXCL_STOP
Expand All @@ -292,32 +292,24 @@
* Ignoring the error is more useful than raising.
*/
// LCOV_EXCL_START
Py_CLEAR(py_name);
PyErr_Clear();
continue;
// LCOV_EXCL_STOP
}
}

PyObject* py_name = PyUnicode_FromString(name);
if (py_name == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(v);
Py_DECREF(res);
free(methods);
return NULL;
// LCOV_EXCL_STOP
}
if (PyDict_SetItem(res, py_name, v) == -1) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(v);
Py_DECREF(res);
Py_DECREF(py_name);
Py_CLEAR(py_name);
free(methods);
return NULL;
// LCOV_EXCL_STOP
}

Py_DECREF(py_name);
Py_CLEAR(py_name);
Py_DECREF(v);
}

Expand Down
73 changes: 29 additions & 44 deletions pyobjc-core/Modules/objc/objc-class.m
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,6 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
Class cls;
Method* methods;
unsigned int method_count, i;
char selbuf[2048];

/* Start of with keys in __dict__ */
result = PyDict_Keys(((PyTypeObject*)self)->tp_dict);
Expand Down Expand Up @@ -1452,7 +1451,6 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
methods =
(Method* _Nonnull)class_copyMethodList(object_getClass(cls), &method_count);
for (i = 0; i < method_count; i++) {
const char* name;
PyObject* item;
SEL meth_name = method_getName(methods[i]);
if (meth_name == NULL) { // LCOV_BR_EXCL_LINE
Expand All @@ -1474,16 +1472,7 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
continue;
}

name = PyObjC_SELToPythonName(meth_name, selbuf, sizeof(selbuf));
if (name == NULL) {
/* Ignore selectors that cannot be converted to a python name.
* This should never happen with the size of selbuf.
*/
PyErr_Clear();
continue;
}

item = PyUnicode_FromString(name);
item = PyObjC_SELToPythonName(meth_name);
if (item == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
free(methods);
Expand Down Expand Up @@ -1608,8 +1597,6 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
Class cls;
Method* methods;
unsigned int method_count, j;
char selbuf[2048];
const char* sel_name;

base = PyTuple_GET_ITEM(mro, i);
PyObjC_Assert(base != NULL, NULL);
Expand Down Expand Up @@ -1655,13 +1642,21 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
continue;
}

sel_name = PyObjC_SELToPythonName(method_getName(m), selbuf, sizeof(selbuf));
PyObject* sel_name = PyObjC_SELToPythonName(method_getName(m));
if (sel_name == NULL) {
/* Ignore selectors whose name cannot be converted to a python name */
PyErr_Clear();
continue;
}
if (strcmp(sel_name, name_bytes) == 0) {
int same = PyObject_RichCompareBool(sel_name, name, Py_EQ);
Py_CLEAR(sel_name);
if (same == -1) { // LCOV_BR_EXCL_START
// LCOV_EXCL_START
PyErr_Clear();
continue;
// LCOV_EXCL_STOP
}
if (same) {
/* Create (unbound) selector */
const char* encoding = method_getTypeEncoding(m);
if (encoding == NULL) { // LCOV_BR_EXCL_LINE
Expand Down Expand Up @@ -1919,8 +1914,6 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
Class cls;
Method* methods;
unsigned int method_count, j;
char selbuf[2048];
char* sel_name;

base = PyTuple_GET_ITEM(mro, i);
if (!PyObjCClass_Check(base)) {
Expand All @@ -1937,15 +1930,24 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
for (j = 0; j < method_count; j++) {
Method m = methods[j];

sel_name =
(char*)PyObjC_SELToPythonName(method_getName(m), selbuf, sizeof(selbuf));
PyObject* sel_name = PyObjC_SELToPythonName(method_getName(m));
if (sel_name == NULL) {
/* Ignore methods whose selector cannot be converted */
PyErr_Clear();
continue;
}

if (strcmp(sel_name, PyObjC_Unicode_Fast_Bytes(name)) == 0) {
int same = PyObject_RichCompareBool(sel_name, name, Py_EQ);
Py_CLEAR(sel_name);
if (same == -1) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
/* See above */
PyErr_Clear();
continue;
// LCOV_EXCL_STOP
}

if (same) {
/* Create (unbound) selector */
const char* encoding = method_getTypeEncoding(m);
if (encoding == NULL) { // LCOV_BR_EXCL_LINE
Expand All @@ -1960,8 +1962,10 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
PyObject* result = PyObjCSelector_NewNative(cls, sel, encoding, 0);
free(methods);
if (result == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(mro);
return NULL; // LCOV_EXCL_LINE
return NULL;
// LCOV_EXCL_STOP
}

/* add to __dict__ 'cache' */
Expand Down Expand Up @@ -2645,7 +2649,6 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
Class cls;
Method* methods;
unsigned int method_count, i;
char selbuf[2048];

/* Start of with keys in __dict__ */
result = PyDict_Keys(((PyTypeObject*)self)->tp_dict);
Expand All @@ -2663,7 +2666,6 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
*/
methods = (Method* _Nonnull)class_copyMethodList(cls, &method_count);
for (i = 0; i < method_count; i++) {
char* name;
PyObject* item;

/* Check if the selector should be hidden */
Expand All @@ -2679,15 +2681,7 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
continue;
}

name = (char*)PyObjC_SELToPythonName(method_getName(methods[i]), selbuf,
sizeof(selbuf));
if (name == NULL) {
/* Ignore methods whose SEL cannot be converted */
PyErr_Clear();
continue;
}

item = PyUnicode_FromString(name);
item = PyObjC_SELToPythonName(method_getName(methods[i]));
if (item == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
free(methods);
Expand Down Expand Up @@ -3424,16 +3418,7 @@ Class _Nullable PyObjCClass_GetClass(PyObject* cls)
}
}
{
char selbuf[2048];
char* name;
PyObject* py_name;
name = PyObjC_SELToPythonName(selector, selbuf, sizeof(selbuf));
if (!name) {
PyErr_Clear();
continue;
}

py_name = PyUnicode_FromString(name);
PyObject* py_name = PyObjC_SELToPythonName(selector);
if (!py_name) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
PyErr_Clear();
Expand All @@ -3447,7 +3432,7 @@ Class _Nullable PyObjCClass_GetClass(PyObject* cls)
} else {
value = PyObjCClass_TryResolveSelector(c, py_name, selector);
}
Py_DECREF(py_name);
Py_CLEAR(py_name);
if (value != NULL) {
return value;
} else if (PyErr_Occurred()) {
Expand Down
34 changes: 13 additions & 21 deletions pyobjc-core/Modules/objc/objc-object.m
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,11 @@

/* XXX: Consider passing in name_bytes as well */
/* returns a new reference */
static inline PyObject* _Nullable _type_lookup_harder(PyTypeObject* tp, PyObject* name,
const char* name_bytes)
static inline PyObject* _Nullable _type_lookup_harder(PyTypeObject* tp, PyObject* name)
{
Py_ssize_t i, n;
PyObject * mro, *base;
PyObject* descr = NULL;
char selbuf[1024];
char* sel_name;

/* Look in tp_dict of types in MRO */
Py_BEGIN_CRITICAL_SECTION(tp);
Expand Down Expand Up @@ -518,8 +515,7 @@
continue;
}

sel_name =
(char*)PyObjC_SELToPythonName(method_getName(m), selbuf, sizeof(selbuf));
PyObject* sel_name = PyObjC_SELToPythonName(method_getName(m));
if (sel_name == NULL) { // LCOV_BR_EXCL_LINE
/* This can only be hit if the method selector is nil or
* if the selector name is longer than 1K
Expand All @@ -530,7 +526,15 @@
// LCOV_EXCL_STOP
}

if (strcmp(sel_name, name_bytes) == 0) {
int same = PyObject_RichCompareBool(sel_name, name, Py_EQ);
Py_CLEAR(sel_name);
if (same == -1) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
PyErr_Clear();
continue;
// LCOV_EXCL_STOP
}
if (same) {
const char* encoding = method_getTypeEncoding(m);
SEL meth_name = method_getName(m);

Expand Down Expand Up @@ -691,7 +695,7 @@
* for a method where the selector does not conform to the
* naming convention that _type_lookup expects.
*/
descr = _type_lookup_harder(tp, name, namestr);
descr = _type_lookup_harder(tp, name);

if (descr != NULL) {
f = Py_TYPE(descr)->tp_descr_get;
Expand Down Expand Up @@ -1088,7 +1092,6 @@
Class cls;
Method* methods;
unsigned int method_count, i;
char selbuf[2048];

/* Start of with keys in __dict__ */
PyObject* class_dict = Py_TYPE(self)->tp_dict;
Expand Down Expand Up @@ -1122,7 +1125,6 @@
*/
methods = (Method* _Nonnull)class_copyMethodList(cls, &method_count);
for (i = 0; i < method_count; i++) {
char* name;
PyObject* item;
SEL sel;

Expand All @@ -1143,17 +1145,7 @@
continue;
}

name = (char*)PyObjC_SELToPythonName(method_getName(methods[i]), selbuf,
sizeof(selbuf));
if (name == NULL) { // LCOV_BR_EXCL_LINE
/* Can only fail if the selector name is longer than the buffer */
// LCOV_EXCL_START
PyErr_Clear();
continue;
// LCOV_EXCL_STOP
}

item = PyUnicode_FromString(name);
item = PyObjC_SELToPythonName(method_getName(methods[i]));
if (item == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
free(methods);
Expand Down
2 changes: 1 addition & 1 deletion pyobjc-core/Modules/objc/objc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extern PyObject* _Nullable PyObjC_AdjustSelf(PyObject* object);

extern int PyObjCRT_SignaturesEqual(const char*, const char*) __attribute__((__pure__));

extern char* _Nullable PyObjC_SELToPythonName(SEL, char*, size_t);
extern PyObject* _Nullable PyObjC_SELToPythonName(SEL);

extern bool version_is_deprecated(int version);

Expand Down
Loading

0 comments on commit 3498633

Please sign in to comment.