Skip to content

Commit

Permalink
Issue #608: Next batch of free threading updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldoussoren committed Nov 29, 2024
1 parent d41c684 commit 59078c6
Show file tree
Hide file tree
Showing 16 changed files with 496 additions and 152 deletions.
23 changes: 11 additions & 12 deletions pyobjc-core/Modules/objc/bundle-variables.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,19 @@ static CFBundleRef _Nullable CreateCFBundleFromNSBundle(NSBundle* bundle)
return NULL;
}

const char* c_name = [name UTF8String];
if (c_name == NULL) {
/* This should never happen, but the return value of -UTF8String
* is _Nullable
*/
PyErr_SetString(PyExc_ValueError, "Cannot convert name to string");
PyObject* py_name = id_to_python(name);
if (py_name == NULL) {
Py_DECREF(py_val);
return NULL;
}

if (PyDict_SetItemString(module_globals, c_name, py_val) == -1) {
if (PyDict_SetItem(module_globals, py_name, py_val) == -1) {
Py_DECREF(py_val);
Py_DECREF(py_name);
return NULL;
}
Py_DECREF(py_val);
Py_DECREF(py_name);
}

Py_INCREF(Py_None);
Expand Down Expand Up @@ -202,10 +200,9 @@ static CFBundleRef _Nullable CreateCFBundleFromNSBundle(NSBundle* bundle)
return NULL;
}

const char* c_name = [name UTF8String];
if (c_name == NULL) { // LCOV_BR_EXCL_LINE
PyObject* py_name = id_to_python(name);
if (py_name == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
PyErr_SetString(PyObjCExc_Error, "Cannot convert name to a C string");
if (cfBundle != NULL) CFRelease(cfBundle);
Py_DECREF(seq);
Py_DECREF(py_val);
Expand All @@ -214,16 +211,18 @@ static CFBundleRef _Nullable CreateCFBundleFromNSBundle(NSBundle* bundle)
}


if (PyDict_SetItemString( // LCOV_BR_EXCL_LINE
module_globals, c_name, py_val)
if (PyDict_SetItem( // LCOV_BR_EXCL_LINE
module_globals, py_name, py_val)
== -1) {
// LCOV_EXCL_START
if (cfBundle != NULL) CFRelease(cfBundle);
Py_DECREF(seq);
Py_DECREF(py_val);
Py_DECREF(py_name);
return NULL;
// LCOV_EXCL_STOP
}
Py_DECREF(py_name);
Py_DECREF(py_val);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pyobjc-core/Modules/objc/class-builder.m
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ Class _Nullable PyObjCClass_BuildClass(Class super_class, PyObject* protocols, c
Py_CLEAR(py_superclass);

/* XXX: Can __dict__ ever be in the class_dict? */
if (PyDict_DelItemString(class_dict, "__dict__") == -1) {
if (PyDict_DelItem(class_dict, PyObjCNM___dict__) == -1) {
PyErr_Clear();
}
Py_CLEAR(instance_variables);
Expand Down
13 changes: 11 additions & 2 deletions pyobjc-core/Modules/objc/ctests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1086,16 +1086,25 @@ - (void)methodWithMyStruct:(struct Struct2)val1 andShort:(short)val2
PyMethodDef* cur;
for (cur = mod_methods; cur->ml_name != NULL; cur++) {
PyObject* meth = PyCFunction_NewEx(cur, NULL, NULL);
if (meth == NULL) {
if (meth == NULL) { // LCOV_BR_EXCL_LINE
Py_DECREF(d); // LCOV_EXCL_LINE
return -1; // LCOV_EXCL_LINE
}
if (PyDict_SetItemString(d, cur->ml_name, meth) < 0) {

PyObject* key = PyUnicode_FromString(cur->ml_name);
if (key == NULL) { // LCOV_BR_EXCL_LINE
Py_DECREF(d); // LCOV_EXCL_LINE
Py_DECREF(meth); // LCOV_EXCL_LINE
return -1;
}
if (PyDict_SetItem(d, key, meth) < 0) { // LCOV_BR_EXCL_LINE
Py_DECREF(d); // LCOV_EXCL_LINE
Py_DECREF(meth); // LCOV_EXCL_LINE
Py_DECREF(key); // LCOV_EXCL_LINE
return -1; // LCOV_EXCL_LINE
}
Py_DECREF(meth);
Py_DECREF(key);
}

return PyModule_AddObject(m, "_ctests", d);
Expand Down
25 changes: 22 additions & 3 deletions pyobjc-core/Modules/objc/libffi_support.m
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,23 @@ int PyObjCFFI_Setup(PyObject* m __attribute__((__unused__)))

PyObjC_Assert(!PyErr_Occurred(), NULL);

if (PyDict_SetItemString(array_types, (char*)key, v) == -1) { // LCOV_BR_EXCL_LINE
PyObject* py_key = PyUnicode_FromString(key);
if (py_key == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(v);
return NULL;
// LCOV_EXCL_STOP
}

if (PyDict_SetItem(array_types, py_key, v) == -1) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(v);
Py_DECREF(py_key);
return NULL;
// LCOV_EXCL_STOP
}
Py_DECREF(v);
Py_DECREF(py_key);
return type;
}

Expand Down Expand Up @@ -394,15 +404,24 @@ int PyObjCFFI_Setup(PyObject* m __attribute__((__unused__)))
// LCOV_EXCL_STOP
}

if (PyDict_SetItemString( // LCOV_BR_EXCL_LINE
struct_types, (char*)argtype, v)
PyObject* py_argtype = PyUnicode_FromString(argtype);
if (py_argtype == NULL) { // LCOV_BR_EXCL_LINE
// LCOV_EXCL_START
Py_DECREF(v);
return NULL;
// LCOV_EXCL_STOP
}
if (PyDict_SetItem( // LCOV_BR_EXCL_LINE
struct_types, py_argtype, v)
== -1) {
// LCOV_EXCL_START
Py_DECREF(v);
Py_DECREF(py_argtype);
return NULL;
// LCOV_EXCL_STOP
}
Py_DECREF(v);
Py_DECREF(py_argtype);
return type;
}

Expand Down
13 changes: 12 additions & 1 deletion pyobjc-core/Modules/objc/method-accessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,26 @@
}
}

if (PyDict_SetItemString(res, name, v) == -1) { // LCOV_BR_EXCL_LINE
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);
free(methods);
return NULL;
// LCOV_EXCL_STOP
}

Py_DECREF(py_name);
Py_DECREF(v);
}

Expand Down
10 changes: 9 additions & 1 deletion pyobjc-core/Modules/objc/method-imp.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
{
PyObjC_Assert(PyObjCIMP_Check(self), NULL);

return ((PyObjCIMPObject*)self)->cif;
ffi_cif* result;
Py_BEGIN_CRITICAL_SECTION(self);
result = ((PyObjCIMPObject*)self)->cif;
Py_END_CRITICAL_SECTION();

return result;
}

int
PyObjCIMP_SetCIF(PyObject* self, ffi_cif* _Nullable cif)
{
PyObjC_Assert(PyObjCIMP_Check(self), -1);

Py_BEGIN_CRITICAL_SECTION(self);
((PyObjCIMPObject*)self)->cif = cif;
Py_END_CRITICAL_SECTION();

return 0;
}

Expand Down
28 changes: 22 additions & 6 deletions pyobjc-core/Modules/objc/module.m
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,22 @@ + (void)targetForBecomingMultiThreaded:(id)sender
} else if (strcmp(nm, "Object") == 0 || strcmp(nm, "List") == 0
|| strcmp(nm, "Protocol") == 0) {
/* skip, these have been deprecated since OpenStep! */
} else if (PyDict_SetItemString(module_globals, ((PyTypeObject*)item)->tp_name,
} else {
PyObject* py_nm = PyUnicode_FromString(nm);
if (py_nm == NULL) {
Py_DECREF(class_list);
class_list = NULL;
return NULL;
}
if (PyDict_SetItem(module_globals, py_nm,
item)
== -1) {
Py_DECREF(class_list);
class_list = NULL;
return NULL;
== -1) {
Py_DECREF(class_list);
Py_DECREF(py_nm);
class_list = NULL;
return NULL;
}
Py_DECREF(py_nm);
}
}
Py_XDECREF(class_list);
Expand Down Expand Up @@ -1176,8 +1186,14 @@ + (void)targetForBecomingMultiThreaded:(id)sender
return NULL; // LCOV_EXCL_LINE
}

int r = PyDict_SetItemString(PyObjC_TypeStr2CFTypeID, encoding, v);
PyObject* py_encoding = PyUnicode_FromString(encoding);
if (py_encoding == NULL) { // LCOV_BR_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}

int r = PyDict_SetItem(PyObjC_TypeStr2CFTypeID, py_encoding, v);
Py_DECREF(v);
Py_DECREF(py_encoding);
if (r == -1) { // LCOV_BR_EXCL_LINE
return NULL; // LCOV_EXCL_LINE
}
Expand Down
Loading

0 comments on commit 59078c6

Please sign in to comment.