| 1 |
from pypy.conftest import option |
| 2 |
|
| 3 |
|
| 4 |
class AppTestReferents(object): |
| 5 |
|
| 6 |
def setup_class(cls): |
| 7 |
from rpython.rlib import rgc |
| 8 |
cls._backup = [rgc.get_rpy_roots] |
| 9 |
w = cls.space.wrap |
| 10 |
space = cls.space |
| 11 |
if option.runappdirect: |
| 12 |
ro = None |
| 13 |
else: |
| 14 |
class RandomRPythonObject(object): |
| 15 |
pass |
| 16 |
ro = RandomRPythonObject() |
| 17 |
l4 = space.newlist([w(4)]) |
| 18 |
l2 = space.newlist([w(2)]) |
| 19 |
l7 = space.newlist([w(7)]) |
| 20 |
cls.ALL_ROOTS = [l4, space.newlist([l2, l7]), ro, |
| 21 |
space.newtuple([l7])] |
| 22 |
cls.w_ALL_ROOTS = cls.space.newlist(cls.ALL_ROOTS) |
| 23 |
rgc.get_rpy_roots = lambda: ( |
| 24 |
map(rgc._GcRef, cls.ALL_ROOTS) + [rgc.NULL_GCREF]*17) |
| 25 |
cls.w_runappdirect = cls.space.wrap(option.runappdirect) |
| 26 |
|
| 27 |
def teardown_class(cls): |
| 28 |
from rpython.rlib import rgc |
| 29 |
rgc.get_rpy_roots = cls._backup[0] |
| 30 |
|
| 31 |
def test_get_objects(self): |
| 32 |
import gc |
| 33 |
lst = gc.get_objects() |
| 34 |
i4, l27, ro, rt = self.ALL_ROOTS |
| 35 |
i2, i7 = l27 |
| 36 |
found = 0 |
| 37 |
for x in lst: |
| 38 |
if x is i4: found |= 1 |
| 39 |
if x is i2: found |= 2 |
| 40 |
if x is i7: found |= 4 |
| 41 |
if x is l27: found |= 8 |
| 42 |
assert found == 15 |
| 43 |
for x in lst: |
| 44 |
if type(x) is gc.GcRef: |
| 45 |
assert 0, "get_objects() returned a GcRef" |
| 46 |
|
| 47 |
def test_get_rpy_roots(self): |
| 48 |
import gc |
| 49 |
lst = gc.get_rpy_roots() |
| 50 |
if self.runappdirect: |
| 51 |
pass |
| 52 |
else: |
| 53 |
assert lst[0] == [4] |
| 54 |
assert lst[1] == [[2], [7]] |
| 55 |
assert type(lst[2]) is gc.GcRef |
| 56 |
assert lst[3] == ([7],) |
| 57 |
assert len(lst) == 4 |
| 58 |
|
| 59 |
def test_get_rpy_referents(self): |
| 60 |
import gc |
| 61 |
y = [12345] |
| 62 |
x = [y] |
| 63 |
lst = gc.get_rpy_referents(x) |
| 64 |
|
| 65 |
|
| 66 |
print(lst) |
| 67 |
lst2 = [x for x in lst if type(x) is gc.GcRef] |
| 68 |
assert lst2 != [] |
| 69 |
|
| 70 |
|
| 71 |
lst3 = [] |
| 72 |
for x in lst2: lst3 += gc.get_rpy_referents(x) |
| 73 |
if y not in lst3: |
| 74 |
lst4 = [] |
| 75 |
for x in lst3: lst4 += gc.get_rpy_referents(x) |
| 76 |
if y not in lst4: |
| 77 |
assert 0, "does not seem to reach 'y'" |
| 78 |
|
| 79 |
def test_get_rpy_memory_usage(self): |
| 80 |
import gc |
| 81 |
n = gc.get_rpy_memory_usage(12345) |
| 82 |
print(n) |
| 83 |
assert 4 <= n <= 64 |
| 84 |
|
| 85 |
def test_get_rpy_type_index(self): |
| 86 |
import gc |
| 87 |
class Foo(object): |
| 88 |
pass |
| 89 |
n1 = gc.get_rpy_type_index(12345) |
| 90 |
n2 = gc.get_rpy_type_index(23456) |
| 91 |
n3 = gc.get_rpy_type_index(1.2) |
| 92 |
n4 = gc.get_rpy_type_index(Foo()) |
| 93 |
print(n1, n2, n3, n4) |
| 94 |
assert n1 == n2 |
| 95 |
assert n1 != n3 |
| 96 |
assert n1 != n4 |
| 97 |
assert n3 != n4 |
| 98 |
|
| 99 |
def test_get_referents(self): |
| 100 |
import gc |
| 101 |
y = [12345] |
| 102 |
z = [23456] |
| 103 |
x = [y, z] |
| 104 |
lst = gc.get_referents(x) |
| 105 |
assert y in lst and z in lst |
| 106 |
|
| 107 |
def test_get_referrers(self): |
| 108 |
import gc |
| 109 |
l27 = self.ALL_ROOTS[1] |
| 110 |
i2, i7 = l27 |
| 111 |
lst = gc.get_referrers(i7) |
| 112 |
for x in lst: |
| 113 |
if x is l27: |
| 114 |
break |
| 115 |
else: |
| 116 |
assert 0, "the list [2, 7] is not found as gc.get_referrers(7)" |
| 117 |
l7t = self.ALL_ROOTS[3] |
| 118 |
for x in lst: |
| 119 |
if x is l7t: |
| 120 |
break |
| 121 |
else: |
| 122 |
if i7 is self.ALL_ROOTS[3][0]: |
| 123 |
assert 0, "the tuple (7,) is not found as gc.get_referrers(7)" |
| 124 |
|
| 125 |
|
| 126 |
class AppTestReferentsMore(object): |
| 127 |
|
| 128 |
def setup_class(cls): |
| 129 |
from rpython.rlib import rgc |
| 130 |
cls._backup = [rgc.get_rpy_roots] |
| 131 |
l4 = cls.space.newlist([]) |
| 132 |
cls.ALL_ROOTS = [l4] |
| 133 |
cls.w_ALL_ROOTS = cls.space.newlist(cls.ALL_ROOTS) |
| 134 |
rgc.get_rpy_roots = lambda: ( |
| 135 |
map(rgc._GcRef, cls.ALL_ROOTS) + [rgc.NULL_GCREF]*2) |
| 136 |
cls.w_runappdirect = cls.space.wrap(option.runappdirect) |
| 137 |
|
| 138 |
def teardown_class(cls): |
| 139 |
from rpython.rlib import rgc |
| 140 |
rgc.get_rpy_roots = cls._backup[0] |
| 141 |
|
| 142 |
def test_get_referrers(self): |
| 143 |
import gc |
| 144 |
class A(object): |
| 145 |
pass |
| 146 |
a = A() |
| 147 |
if not self.runappdirect: |
| 148 |
l4 = self.ALL_ROOTS[0] |
| 149 |
l4.append(a) |
| 150 |
lst = gc.get_referrers(A) |
| 151 |
assert a in lst |
| 152 |
lst = gc.get_referrers(A) |
| 153 |
assert a in lst |
| 154 |
lst = gc.get_referrers(A) |
| 155 |
assert a in lst |
| 156 |
lst = gc.get_referrers(A) |
| 157 |
assert a in lst |