| 1 |
import py |
| 2 |
from rpython.rtyper.lltypesystem import lltype |
| 3 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 4 |
from rpython.rlib import rgc |
| 5 |
|
| 6 |
from rpython.memory.test import test_minimark_gc |
| 7 |
|
| 8 |
class TestIncrementalMiniMarkGC(test_minimark_gc.TestMiniMarkGC): |
| 9 |
from rpython.memory.gc.incminimark import IncrementalMiniMarkGC as GCClass |
| 10 |
WREF_IS_INVALID_BEFORE_DEL_IS_CALLED = True |
| 11 |
|
| 12 |
def test_weakref_not_in_stack(self): |
| 13 |
import weakref |
| 14 |
class A(object): |
| 15 |
pass |
| 16 |
class B(object): |
| 17 |
def __init__(self, next): |
| 18 |
self.next = next |
| 19 |
def g(): |
| 20 |
a = A() |
| 21 |
a.x = 5 |
| 22 |
wr = weakref.ref(a) |
| 23 |
llop.gc__collect(lltype.Void) |
| 24 |
assert wr() is not None |
| 25 |
assert a.x == 5 |
| 26 |
return wr |
| 27 |
def f(): |
| 28 |
ref = g() |
| 29 |
llop.gc__collect(lltype.Void, 1) |
| 30 |
|
| 31 |
|
| 32 |
b = ref() |
| 33 |
llop.debug_print(lltype.Void, b) |
| 34 |
assert b is not None |
| 35 |
llop.gc__collect(lltype.Void) |
| 36 |
|
| 37 |
b.x = 42 |
| 38 |
return ref() is b |
| 39 |
res = self.interpret(f, []) |
| 40 |
assert res == True |
| 41 |
|
| 42 |
def test_pin_weakref_not_implemented(self): |
| 43 |
import weakref |
| 44 |
class A: |
| 45 |
pass |
| 46 |
def f(): |
| 47 |
a = A() |
| 48 |
ref = weakref.ref(a) |
| 49 |
assert not rgc.pin(ref) |
| 50 |
self.interpret(f, []) |
| 51 |
|
| 52 |
def test_pin_finalizer_not_implemented(self): |
| 53 |
import weakref |
| 54 |
class A: |
| 55 |
def __del__(self): |
| 56 |
pass |
| 57 |
class B: |
| 58 |
def __del__(self): |
| 59 |
foo.bar += 1 |
| 60 |
class Foo: |
| 61 |
bar = 0 |
| 62 |
foo = Foo() |
| 63 |
def f(): |
| 64 |
a = A() |
| 65 |
b = B() |
| 66 |
assert not rgc.pin(a) |
| 67 |
assert not rgc.pin(b) |
| 68 |
self.interpret(f, []) |
| 69 |
|
| 70 |
def test_weakref_to_pinned(self): |
| 71 |
py.test.skip("weakref to pinned object: not supported") |
| 72 |
import weakref |
| 73 |
from rpython.rlib import rgc |
| 74 |
class A(object): |
| 75 |
pass |
| 76 |
def g(): |
| 77 |
a = A() |
| 78 |
assert rgc.pin(a) |
| 79 |
a.x = 100 |
| 80 |
wr = weakref.ref(a) |
| 81 |
llop.gc__collect(lltype.Void) |
| 82 |
assert wr() is not None |
| 83 |
assert a.x == 100 |
| 84 |
return wr |
| 85 |
def f(): |
| 86 |
ref = g() |
| 87 |
llop.gc__collect(lltype.Void, 1) |
| 88 |
b = ref() |
| 89 |
assert b is not None |
| 90 |
b.x = 101 |
| 91 |
return ref() is b |
| 92 |
res = self.interpret(f, []) |
| 93 |
assert res == True |