Skip to content

Commit c43132a

Browse files
committed
Fix RefCounted.__del__
1 parent f7d1d6d commit c43132a

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/godot/builtins_classes.pxi

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,6 @@ cdef inline object _build_class_from_spec(str name, StringName gd_name):
154154

155155
attrs = {"_gdpy_godot_class_name": gd_name}
156156

157-
if not is_refcounted and name == "RefCounted":
158-
159-
def _gen():
160-
cdef StringName gdstr_unreference = StringName("unreference")
161-
162-
def _del(self):
163-
print(f'[DEBUG] {type(self).__name__}.__del__()', flush=True)
164-
cdef BaseGDObject obj = <BaseGDObject>self
165-
if _object_call(obj._gd_ptr, gdstr_unreference, []):
166-
gdptrs.gdptr_object_destroy(obj._gd_ptr)
167-
obj._gd_ptr = NULL
168-
169-
def _free(self):
170-
print(f'[DEBUG] {type(self).__name__}.free()', flush=True)
171-
raise RuntimeError("RefCounted Godot object, cannot be freed")
172-
173-
return _del, _free
174-
175-
attrs["__del__"], attrs["free"] = _gen()
176-
177157
while True:
178158
try:
179159
tag = next(items_spec)
@@ -286,8 +266,29 @@ cdef inline object _build_class_from_spec(str name, StringName gd_name):
286266
else:
287267
assert False, tag
288268

289-
# `Object` defines a `free`, but it doesn't work properly (instead we rely on `BaseGDObject.free`)
290-
attrs.pop("free", None)
269+
if name == "Object":
270+
# `Object` defines a `free`, but it doesn't work properly (instead we rely on `BaseGDObject.free`)
271+
attrs.pop("free", None)
272+
273+
elif name == "RefCounted":
274+
275+
def _gen():
276+
cdef StringName gdstr_unreference = StringName("unreference")
277+
278+
def _del(self):
279+
print(f'[DEBUG] {type(self).__name__}.__del__()', flush=True)
280+
cdef BaseGDObject obj = <BaseGDObject>self
281+
if _object_call(obj._gd_ptr, gdstr_unreference, []):
282+
gdptrs.gdptr_object_destroy(obj._gd_ptr)
283+
obj._gd_ptr = NULL
284+
285+
def _free(self):
286+
print(f'[DEBUG] {type(self).__name__}.free()', flush=True)
287+
raise RuntimeError("RefCounted Godot object, cannot be freed")
288+
289+
return _del, _free
290+
291+
attrs["__del__"], attrs["free"] = _gen()
291292

292293
return type(name, bases, attrs)
293294

tests/4-use-godot-from-python/tests/test_utility_function.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ def test_call(kind: str):
2929
assert_eq(utils.cos(0), 1)
3030
case "none_in_object_out":
3131
obj = utils.weakref(None)
32-
try:
33-
assert_isinstance(obj, WeakRef)
34-
assert_eq(obj.get_ref(), None)
35-
finally:
36-
obj.free()
32+
assert_isinstance(obj, WeakRef)
33+
assert_eq(obj.get_ref(), None)
3734
case "none_out":
3835
assert_eq(utils.seed(42), None)
3936
case "variant_in_builtin_out":

0 commit comments

Comments
 (0)