Skip to content

Commit d7c2cc8

Browse files
fangererqunaibit
authored andcommitted
Fix cpyext/test_gc for CPython
1 parent 4bdc801 commit d7c2cc8

File tree

1 file changed

+46
-36
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests/cpyext

1 file changed

+46
-36
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_gc.py

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,13 @@ def test_native_class(self):
206206

207207
if GRAALPY_NATIVE:
208208
get_handle_table_id = __graalpython__.get_handle_table_id
209-
is_strong_handle_table_ref = __graalpython__.is_strong_handle_table_ref
209+
def assert_is_strong(x): assert __graalpython__.is_strong_handle_table_ref(x)
210+
def assert_is_weak(x): assert not __graalpython__.is_strong_handle_table_ref(x)
210211
else:
211212
# just that the test is compatible with CPython
212-
def get_handle_table_id(object):
213-
return -1
214-
215-
216-
def is_strong_handle_table_ref(id):
217-
return False
213+
def get_handle_table_id(object): return -1
214+
def assert_is_strong(x): pass
215+
def assert_is_weak(x): pass
218216

219217
class TestGCRefCycles:
220218
def _trigger_gc(self):
@@ -479,12 +477,12 @@ def assert_is_freed(id): pass
479477
assert_is_alive(ID_OBJ10)
480478
assert_is_alive(ID_OBJ11)
481479
assert_is_alive(ID_OBJ14)
482-
assert is_strong_handle_table_ref(htid_l)
483-
assert is_strong_handle_table_ref(htid_l1)
484-
assert is_strong_handle_table_ref(htid_l2)
485-
assert is_strong_handle_table_ref(htid_l3)
486-
assert is_strong_handle_table_ref(htid_l4)
487-
assert is_strong_handle_table_ref(htid_d0)
480+
assert_is_strong(htid_l)
481+
assert_is_strong(htid_l1)
482+
assert_is_strong(htid_l2)
483+
assert_is_strong(htid_l3)
484+
assert_is_strong(htid_l4)
485+
assert_is_strong(htid_d0)
488486

489487
del obj2, l, obj3
490488
del obj4, obj5
@@ -510,12 +508,12 @@ def assert_is_freed(id): pass
510508
assert_is_alive(ID_OBJ9)
511509
assert_is_alive(ID_OBJ10)
512510
assert_is_alive(ID_OBJ14)
513-
assert is_strong_handle_table_ref(htid_l2)
514-
assert is_strong_handle_table_ref(htid_l3)
515-
assert not is_strong_handle_table_ref(htid_l)
516-
assert not is_strong_handle_table_ref(htid_l1)
517-
assert not is_strong_handle_table_ref(htid_l4)
518-
assert not is_strong_handle_table_ref(htid_d0)
511+
assert_is_strong(htid_l2)
512+
assert_is_strong(htid_l3)
513+
assert_is_weak(htid_l)
514+
assert_is_weak(htid_l1)
515+
assert_is_weak(htid_l4)
516+
assert_is_weak(htid_d0)
519517

520518
rescued_obj4 = l1[0]
521519
del l1
@@ -531,7 +529,7 @@ def assert_is_freed(id): pass
531529
assert_is_alive(ID_OBJ5)
532530
assert_is_alive(ID_OBJ14)
533531
assert rescued_obj4.get_obj().get_obj()[0] is rescued_obj4
534-
assert is_strong_handle_table_ref(htid_l4)
532+
assert_is_strong(htid_l4)
535533

536534
del rescued_obj4
537535

@@ -551,8 +549,8 @@ def assert_is_freed(id): pass
551549

552550
assert_is_alive(ID_OBJ12)
553551
assert_is_alive(ID_OBJ13)
554-
assert is_strong_handle_table_ref(htid_l2)
555-
assert is_strong_handle_table_ref(htid_l3)
552+
assert_is_strong(htid_l2)
553+
assert_is_strong(htid_l3)
556554

557555
del obj12, obj13, l2, l3
558556

@@ -564,8 +562,8 @@ def assert_is_freed(id): pass
564562
assert_is_freed(ID_OBJ5)
565563
assert_is_freed(ID_OBJ12)
566564
assert_is_freed(ID_OBJ13)
567-
assert not is_strong_handle_table_ref(htid_l2)
568-
assert not is_strong_handle_table_ref(htid_l3)
565+
assert_is_weak(htid_l2)
566+
assert_is_weak(htid_l3)
569567

570568

571569
@skipIf(GRAALPY and not GRAALPY_NATIVE, "Python GC only used in native mode")
@@ -634,24 +632,36 @@ def test_cycle_with_lists(self):
634632
tp_flags='Py_TPFLAGS_DEFAULT',
635633
tp_dealloc='(destructor)tc_dealloc',
636634
)
635+
if RELY_ON_GC:
636+
def assert_is_alive(id):
637+
assert not TestCycle.is_freed(id)
638+
def assert_is_freed(id):
639+
assert TestCycle.is_freed(id)
640+
else:
641+
def assert_is_alive(id): pass
642+
def assert_is_freed(id): pass
643+
637644
l0, l1 = TestCycle.set_list_item(0)
645+
htid_l0 = get_handle_table_id(l0)
646+
htid_l1 = get_handle_table_id(l1)
638647

639648
ml0, ml1 = list(), list()
640649
ml0.append(ml1)
641650
ml0.append(TestCycle(1))
642651
ml1.append(ml0)
643652

644-
assert not TestCycle.is_freed(0)
645-
assert not TestCycle.is_freed(1)
646-
assert l0[0] == l1
647-
assert l1[0] == l0
648-
assert ml0[0] == ml1
649-
assert ml1[0] == ml0
653+
assert_is_alive(0)
654+
assert_is_alive(1)
655+
assert_is_strong(htid_l0)
656+
assert_is_strong(htid_l1)
657+
assert l0[0] is l1
658+
assert l1[0] is l0
659+
assert ml0[0] is ml1
660+
assert ml1[0] is ml0
650661

651-
del l0
652-
del l1
653-
del ml0
654-
del ml1
662+
del l0, l1, ml0, ml1
655663
self._trigger_gc()
656-
assert TestCycle.is_freed(0)
657-
assert TestCycle.is_freed(1)
664+
assert_is_freed(0)
665+
assert_is_freed(1)
666+
assert_is_weak(htid_l0)
667+
assert_is_weak(htid_l1)

0 commit comments

Comments
 (0)