Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
9abcd0d
Retain on ObjCInstance creation, autorelease on __del__
samschott Nov 19, 2024
6605842
update tests
samschott Nov 19, 2024
931c352
add change note
samschott Nov 19, 2024
a618f2a
use autorelease instead of release in __del__
samschott Nov 20, 2024
b1bf61c
code formatting
samschott Nov 20, 2024
21f2e0b
update docs
samschott Nov 20, 2024
20ab8f9
add comment about autorelease vs release
samschott Nov 23, 2024
160c819
remove now unneeded cache staleness check
samschott Nov 23, 2024
ce9d78c
remove stale instance cache tests
samschott Nov 23, 2024
6d89330
update test_objcinstance_dealloc
samschott Nov 24, 2024
3bb7ccc
correct inline comment
samschott Nov 24, 2024
c0b091c
make returned_from_method private
samschott Nov 24, 2024
ab1f762
update ObjCInstance doc string
samschott Nov 24, 2024
544d694
updated docs
samschott Nov 24, 2024
b4a1624
update spellchecker
samschott Nov 24, 2024
f0edb5b
update change notes with migration instructions
samschott Nov 24, 2024
22396dc
Rephrase removal note
samschott Nov 25, 2024
7d51fde
remove unneeded space in doc string
samschott Nov 25, 2024
acfa546
change bugfix to feature note
samschott Nov 25, 2024
18e08cc
Fix incorrect inline comment
samschott Nov 25, 2024
532fbe0
trim trailing whitespace
samschott Nov 25, 2024
52e92c0
update test comment
samschott Nov 25, 2024
efed734
check that objects are not deallocated before end of autorelease pool
samschott Nov 25, 2024
ab8a895
merge object lifecycle tests
samschott Nov 25, 2024
30e4277
add a test case for copyWithZone returning the existing instance with…
samschott Nov 25, 2024
c3a4fe1
release additional refcounts by copy calls on the same ObjCInstance
samschott Nov 25, 2024
7bdc31f
rewrite the copy lifecycle test to use NSDictionary instead of a cust…
samschott Nov 26, 2024
460728b
prevent errors on ObjCInstance garbage collection when `send_message`…
samschott Nov 26, 2024
d9c0f62
switch copy lifecycle test to use NSString
samschott Nov 26, 2024
49d9381
remove unused import
samschott Nov 26, 2024
e0d7792
fix spelling mistake
samschott Nov 26, 2024
715912f
spelling updates
samschott Nov 26, 2024
20e45b6
spelling updates
samschott Nov 26, 2024
86b29a4
spelling updates
samschott Nov 26, 2024
944328d
black code formatting
samschott Nov 26, 2024
3b88aaa
rename test case to "immutable copy lifecycle"
samschott Nov 26, 2024
58d0276
improve inline docs
samschott Nov 27, 2024
84e3a9f
special handling for init
samschott Nov 27, 2024
ab46b9d
add tests for init object change
samschott Nov 28, 2024
2305122
implement proper method family detection
samschott Nov 28, 2024
2e4eccb
ensure partial methods are loaded from all superclasses
samschott Nov 28, 2024
2989540
remove unneeded whitespace
samschott Nov 29, 2024
0bc749c
improved release-on-cache-hit documentation
samschott Nov 29, 2024
04981e3
updated change notes
samschott Nov 29, 2024
54ed55c
add test for get_method_family
samschott Nov 29, 2024
c6096c2
remove loop that breaks early on method loading
samschott Nov 29, 2024
a28c901
make method loading slightly clearer
samschott Nov 29, 2024
1b306e2
extract and document method name to tuple logic
samschott Nov 29, 2024
849749e
fall back to full method usage if partial method lookup fails
samschott Nov 29, 2024
15593c1
update partial method cache after successful lookup
samschott Nov 29, 2024
39c548e
Revert "remove loop that breaks early on method loading"
samschott Nov 30, 2024
b78c41d
Revert "ensure partial methods are loaded from all superclasses"
samschott Nov 30, 2024
cb996ad
Reapply "ensure partial methods are loaded from all superclasses"
samschott Nov 30, 2024
6cf88a5
centralize logic for method family
samschott Dec 2, 2024
aa48b5d
update test description
samschott Dec 4, 2024
7fa9e71
update inline comments
samschott Dec 4, 2024
3be2190
fix method family detection
samschott Dec 4, 2024
dded73e
add test case for alloc without init
samschott Dec 4, 2024
199f807
race free interpreter shutdown handling
samschott Dec 4, 2024
d0961b4
black formatting
samschott Dec 4, 2024
a8e9193
more precise family determination to follow the exact rules laid out …
samschott Dec 4, 2024
296cf83
update tests for method family determination to check for non-lowerca…
samschott Dec 4, 2024
93300f3
fix typo in method_name_to_tuple doc string
samschott Dec 4, 2024
2be945b
more exhaustive refcount tests
samschott Dec 5, 2024
2387c02
remove duplicate code from test_objcinstance_returned_lifecycle
samschott Dec 5, 2024
51ba75b
Fix typo
mhsmith Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/rubicon/objc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ def get_method_family(method_name: str) -> str:
"""Returns the method family from the method name. See
https://clang.llvm.org/docs/AutomaticReferenceCounting.html#method-families for
documentation on method families and corresponding selector names."""
method_name = method_name.lstrip("_").split(":")[0]
leading_lowercases = []
for c in method_name:
if c.isupper():
break
leading_lowercases.append(c)
return "".join(leading_lowercases)
first_component = method_name.lstrip("_").split(":")[0]
for family in _RETURNS_RETAINED_FAMILIES:
if first_component.startswith(family):
remainder = first_component.removeprefix(family)
if remainder == "" or remainder[0].isupper():
Comment thread
samschott marked this conversation as resolved.
Outdated
return family

return ""


def method_name_to_tuple(name: str) -> (str, tuple[str, ...]):
Expand Down Expand Up @@ -254,9 +255,9 @@ def __call__(self, receiver, *args, convert_args=True, convert_result=True):

# Init methods consume their `self` argument (the receiver), see
# https://clang.llvm.org/docs/AutomaticReferenceCounting.html#semantics-of-init.
# To avoid segfaults on garbage collection if `init` does not return `self` but
# a different object or None, we issue an additional retain. This needs to be
# done before calling the method.
# To ensure the receiver pointer remains valid if `init` does not return `self`
# but a different object or None, we issue an additional retain. This needs to
# be done before calling the method.
# Note that if `init` does return the same object, it will already be in our
# cache and balanced with a `release` on cache retrieval.
method_family = get_method_family(self.name.decode())
Expand Down Expand Up @@ -967,8 +968,12 @@ def __del__(self):
# Autorelease our reference on garbage collection of the Python wrapper. We use
# autorelease instead of release to allow ObjC to take ownership of an object when
# it is returned from a factory method.
if send_message and objc_id:
try:
send_message(self, "autorelease", restype=objc_id, argtypes=[])
except (NameError, TypeError):
# Handle interpreter shutdown gracefully where send_message might be deleted
# (NameError) or set to None (TypeError).
pass

def __str__(self):
"""Get a human-readable representation of ``self``.
Expand Down
43 changes: 34 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,7 @@ def test_objcinstance_returned_lifecycle(self):
# Check that the object is retained when we create the ObjCInstance.
self.assertEqual(obj.retainCount(), 1, "object was not retained")
Comment thread
freakboy3742 marked this conversation as resolved.
Outdated

# Assign the object to an Obj-C weakref and delete it check that it is dealloced.
# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj

Expand All @@ -1907,7 +1907,7 @@ def test_objcinstance_owned_lifecycle(self):

self.assertEqual(obj.retainCount(), 1, "object should be retained only once")

# Assign the object to an Obj-C weakref and delete it check that it is dealloced.
# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj

Expand All @@ -1933,7 +1933,7 @@ def test_objcinstance_immutable_copy_lifecycle(self):
self.assertIs(obj0, obj1)
self.assertIs(obj0, obj2)

# Assign the object to an Obj-C weakref and delete it check that it is dealloced.
# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj0

Expand All @@ -1957,7 +1957,7 @@ def test_objcinstance_init_change_lifecycle(self):

self.assertNotEqual(obj_allocated.ptr.value, obj_initialized.ptr.value)

# Assign the object to an Obj-C weakref and delete it check that it is dealloced.
# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj_initialized

Expand All @@ -1972,8 +1972,31 @@ def test_objcinstance_init_change_lifecycle(self):

self.assertIsNone(wr.weak_property, "object was not deallocated")

def test_objcinstance_alloc_lifecycle(self):
"""We properly retain and release objects that are allocated but never
initialized."""
with autoreleasepool():
obj_allocated = NSObject.alloc()

self.assertEqual(obj_allocated.retainCount(), 1)

# Assign the object to an Obj-C weakref and delete it to check that it is dealloced.
wr = ObjcWeakref.alloc().init()
wr.weak_property = obj_allocated

with autoreleasepool():
del obj_allocated
gc.collect()

self.assertIsNotNone(
wr.weak_property,
"object was deallocated before end of autorelease pool",
)

self.assertIsNone(wr.weak_property, "object was not deallocated")

def test_objcinstance_init_none(self):
"""We do segfault if init returns a different object than it received in alloc."""
"""We do not segfault if init returns nil."""
with autoreleasepool():
image = NSImage.alloc().initWithContentsOfFile("/no/file/here")

Expand Down Expand Up @@ -2186,7 +2209,9 @@ def work():
thread.join()

def test_get_method_family(self):
self.assertEqual(get_method_family("perform"), "perform")
self.assertEqual(get_method_family("performWith:"), "perform")
self.assertEqual(get_method_family("_performWith:"), "perform")
self.assertEqual(get_method_family("_perform:with:"), "perform")
self.assertEqual(get_method_family("mutableCopy"), "mutableCopy")
self.assertEqual(get_method_family("mutableCopy:"), "mutableCopy")
self.assertEqual(get_method_family("_mutableCopy:"), "mutableCopy")
self.assertEqual(get_method_family("_mutableCopy:with:"), "mutableCopy")
self.assertEqual(get_method_family("_mutableCopyWith:"), "mutableCopy")
Comment thread
samschott marked this conversation as resolved.
self.assertEqual(get_method_family("_mutableCopying:"), "")