Skip to content

Commit

Permalink
Issue #479: Allow replacing a selector with some other value
Browse files Browse the repository at this point in the history
This was possible in older versions of PyObjC and
changed in some cleanup work in 8.5. Revert to the
old behaviour because this breaks applications.
  • Loading branch information
ronaldoussoren committed Aug 12, 2022
1 parent 90508fc commit d4ad7bb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Version 8.5.1

* #489: Fix incompatibility with Python 3.11

* #479: Revert change that made it impossible to replace a method
with a property.

Version 8.5
-----------

Expand Down
2 changes: 2 additions & 0 deletions pyobjc-core/Modules/objc/objc-class.m
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,7 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)
return 0;
}

#if 0 /* Disabled check due to #479 */
/* Check if there is a current attribute with the same name that
* is an unbound selector.
*/
Expand All @@ -2033,6 +2034,7 @@ static Class _Nullable objc_metaclass_locate(PyObject* meta_class)

return -1;
}
#endif

res = PyType_Type.tp_setattro(self, name, value);
return res;
Expand Down
22 changes: 22 additions & 0 deletions pyobjc-core/PyObjCTest/test_regr.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,25 @@ class Foo:

f = Foo()
f.alloc().init()


class TestReplaceMethod(TestCase):
def test_replace_method(self):
# GH #479
NSAppleScript = objc.lookUpClass("NSAppleScript")
o = NSAppleScript.alloc().initWithSource_("tell application Safari to quit")
self.assertIsNot(o, None)

v = o.source()
self.assertIsInstance(v, str)

# In some applications it is useful to replace an Objective-C method
# by a property definition, like so:
NSAppleScript.source = property(
lambda self: self.pyobjc_instanceMethods.source()
)

v2 = o.source
self.assertIsInstance(v, str)

self.assertEqual(v, v2)

1 comment on commit d4ad7bb

@schriftgestalt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Do you know when this will be available on pip?

Please sign in to comment.