def test_get_referents(): class X(object): __slots__ = ['stuff'] x1 = X() x1.stuff = X() x2 = X() lst = rgc.get_rpy_referents(rgc.cast_instance_to_gcref(x1)) lst2 = [rgc.try_cast_gcref_to_instance(X, x) for x in lst] assert x1.stuff in lst2 assert x2 not in lst2
def try_cast_gcref_to_w_root(gcref): w_obj = rgc.try_cast_gcref_to_instance(W_Root, gcref) # Ignore the instances of W_Root that are not really valid as Python # objects. There is e.g. WeakrefLifeline in module/_weakref that # inherits from W_Root for internal reasons. Such instances don't # have a typedef at all (or have a null typedef after translation). if not we_are_translated(): if not hasattr(w_obj, 'typedef'): return None else: if w_obj is None or not w_obj.typedef: return None return w_obj
def fn(): foo = Foo() gcref1 = rgc.cast_instance_to_gcref(foo) assert rgc.try_cast_gcref_to_instance(Foo, gcref1) is foo assert rgc.try_cast_gcref_to_instance(FooBar, gcref1) is None assert rgc.try_cast_gcref_to_instance(Biz, gcref1) is None foobar = FooBar() gcref2 = rgc.cast_instance_to_gcref(foobar) assert rgc.try_cast_gcref_to_instance(Foo, gcref2) is foobar assert rgc.try_cast_gcref_to_instance(FooBar, gcref2) is foobar assert rgc.try_cast_gcref_to_instance(Biz, gcref2) is None s = lltype.malloc(S) gcref3 = lltype.cast_opaque_ptr(llmemory.GCREF, s) assert rgc.try_cast_gcref_to_instance(Foo, gcref3) is None assert rgc.try_cast_gcref_to_instance(FooBar, gcref3) is None assert rgc.try_cast_gcref_to_instance(Biz, gcref3) is None return 0