| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import py |
| 4 |
import sys |
| 5 |
|
| 6 |
from rpython.memory import gcwrapper |
| 7 |
from rpython.memory.test import snippet |
| 8 |
from rpython.rtyper import llinterp |
| 9 |
from rpython.rtyper.test.test_llinterp import get_interpreter |
| 10 |
from rpython.rtyper.lltypesystem import lltype |
| 11 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 12 |
from rpython.rlib.objectmodel import we_are_translated, keepalive_until_here |
| 13 |
from rpython.rlib.objectmodel import compute_unique_id |
| 14 |
from rpython.rlib import rgc |
| 15 |
from rpython.rlib.rstring import StringBuilder |
| 16 |
from rpython.rlib.rarithmetic import LONG_BIT |
| 17 |
|
| 18 |
WORD = LONG_BIT // 8 |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class GCTest(object): |
| 29 |
GC_PARAMS = {} |
| 30 |
GC_CAN_MOVE = False |
| 31 |
GC_CAN_SHRINK_ARRAY = False |
| 32 |
GC_CAN_SHRINK_BIG_ARRAY = False |
| 33 |
BUT_HOW_BIG_IS_A_BIG_STRING = 3*WORD |
| 34 |
WREF_IS_INVALID_BEFORE_DEL_IS_CALLED = False |
| 35 |
|
| 36 |
def setup_class(cls): |
| 37 |
|
| 38 |
llinterp.log.output_disabled = False |
| 39 |
|
| 40 |
def teardown_class(cls): |
| 41 |
llinterp.log.output_disabled = True |
| 42 |
|
| 43 |
def interpret(self, func, values, **kwds): |
| 44 |
interp, graph = get_interpreter(func, values, **kwds) |
| 45 |
gcwrapper.prepare_graphs_and_create_gc(interp, self.GCClass, |
| 46 |
self.GC_PARAMS) |
| 47 |
return interp.eval_graph(graph, values) |
| 48 |
|
| 49 |
def test_llinterp_lists(self): |
| 50 |
|
| 51 |
def malloc_a_lot(): |
| 52 |
i = 0 |
| 53 |
while i < 10: |
| 54 |
i += 1 |
| 55 |
a = [1] * 10 |
| 56 |
j = 0 |
| 57 |
while j < 20: |
| 58 |
j += 1 |
| 59 |
a.append(j) |
| 60 |
self.interpret(malloc_a_lot, []) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
def test_llinterp_tuples(self): |
| 65 |
|
| 66 |
def malloc_a_lot(): |
| 67 |
i = 0 |
| 68 |
while i < 10: |
| 69 |
i += 1 |
| 70 |
a = (1, 2, i) |
| 71 |
b = [a] * 10 |
| 72 |
j = 0 |
| 73 |
while j < 20: |
| 74 |
j += 1 |
| 75 |
b.append((1, j, i)) |
| 76 |
self.interpret(malloc_a_lot, []) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
def test_global_list(self): |
| 81 |
lst = [] |
| 82 |
def append_to_list(i, j): |
| 83 |
lst.append([i] * 50) |
| 84 |
return lst[j][0] |
| 85 |
res = self.interpret(append_to_list, [0, 0]) |
| 86 |
assert res == 0 |
| 87 |
for i in range(1, 15): |
| 88 |
res = self.interpret(append_to_list, [i, i - 1]) |
| 89 |
assert res == i - 1 |
| 90 |
|
| 91 |
def test_string_concatenation(self): |
| 92 |
|
| 93 |
def concat(j): |
| 94 |
lst = [] |
| 95 |
for i in range(j): |
| 96 |
lst.append(str(i)) |
| 97 |
return len("".join(lst)) |
| 98 |
res = self.interpret(concat, [100]) |
| 99 |
assert res == concat(100) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
def test_collect(self): |
| 104 |
|
| 105 |
def concat(j): |
| 106 |
lst = [] |
| 107 |
for i in range(j): |
| 108 |
lst.append(str(i)) |
| 109 |
result = len("".join(lst)) |
| 110 |
if we_are_translated(): |
| 111 |
|
| 112 |
llop.gc__collect(lltype.Void) |
| 113 |
return result |
| 114 |
res = self.interpret(concat, [100]) |
| 115 |
assert res == concat(100) |
| 116 |
|
| 117 |
|
| 118 |
def test_collect_0(self): |
| 119 |
|
| 120 |
def concat(j): |
| 121 |
lst = [] |
| 122 |
for i in range(j): |
| 123 |
lst.append(str(i)) |
| 124 |
result = len("".join(lst)) |
| 125 |
if we_are_translated(): |
| 126 |
|
| 127 |
llop.gc__collect(lltype.Void, 0) |
| 128 |
return result |
| 129 |
res = self.interpret(concat, [100]) |
| 130 |
assert res == concat(100) |
| 131 |
|
| 132 |
|
| 133 |
def test_destructor(self): |
| 134 |
class B(object): |
| 135 |
pass |
| 136 |
b = B() |
| 137 |
b.nextid = 0 |
| 138 |
b.num_deleted = 0 |
| 139 |
class A(object): |
| 140 |
def __init__(self): |
| 141 |
self.id = b.nextid |
| 142 |
b.nextid += 1 |
| 143 |
def __del__(self): |
| 144 |
b.num_deleted += 1 |
| 145 |
def f(x): |
| 146 |
a = A() |
| 147 |
i = 0 |
| 148 |
while i < x: |
| 149 |
i += 1 |
| 150 |
a = A() |
| 151 |
llop.gc__collect(lltype.Void) |
| 152 |
llop.gc__collect(lltype.Void) |
| 153 |
return b.num_deleted |
| 154 |
res = self.interpret(f, [5]) |
| 155 |
assert res == 6 |
| 156 |
|
| 157 |
def test_old_style_finalizer(self): |
| 158 |
class B(object): |
| 159 |
pass |
| 160 |
b = B() |
| 161 |
b.nextid = 0 |
| 162 |
b.num_deleted = 0 |
| 163 |
class A(object): |
| 164 |
def __init__(self): |
| 165 |
self.id = b.nextid |
| 166 |
b.nextid += 1 |
| 167 |
def __del__(self): |
| 168 |
llop.gc__collect(lltype.Void) |
| 169 |
b.num_deleted += 1 |
| 170 |
def f(x): |
| 171 |
a = A() |
| 172 |
i = 0 |
| 173 |
while i < x: |
| 174 |
i += 1 |
| 175 |
a = A() |
| 176 |
llop.gc__collect(lltype.Void) |
| 177 |
llop.gc__collect(lltype.Void) |
| 178 |
return b.num_deleted |
| 179 |
res = self.interpret(f, [5]) |
| 180 |
assert res == 6 |
| 181 |
|
| 182 |
def test_finalizer(self): |
| 183 |
class B(object): |
| 184 |
pass |
| 185 |
b = B() |
| 186 |
b.nextid = 0 |
| 187 |
b.num_deleted = 0 |
| 188 |
class A(object): |
| 189 |
def __init__(self): |
| 190 |
self.id = b.nextid |
| 191 |
b.nextid += 1 |
| 192 |
fq.register_finalizer(self) |
| 193 |
class FQ(rgc.FinalizerQueue): |
| 194 |
Class = A |
| 195 |
def finalizer_trigger(self): |
| 196 |
while self.next_dead() is not None: |
| 197 |
b.num_deleted += 1 |
| 198 |
fq = FQ() |
| 199 |
def f(x): |
| 200 |
a = A() |
| 201 |
i = 0 |
| 202 |
while i < x: |
| 203 |
i += 1 |
| 204 |
a = A() |
| 205 |
a = None |
| 206 |
llop.gc__collect(lltype.Void) |
| 207 |
llop.gc__collect(lltype.Void) |
| 208 |
return b.num_deleted |
| 209 |
res = self.interpret(f, [5]) |
| 210 |
assert res == 6 |
| 211 |
|
| 212 |
def test_finalizer_delaying_next_dead(self): |
| 213 |
class B(object): |
| 214 |
pass |
| 215 |
b = B() |
| 216 |
b.nextid = 0 |
| 217 |
class A(object): |
| 218 |
def __init__(self): |
| 219 |
self.id = b.nextid |
| 220 |
b.nextid += 1 |
| 221 |
fq.register_finalizer(self) |
| 222 |
class FQ(rgc.FinalizerQueue): |
| 223 |
Class = A |
| 224 |
def finalizer_trigger(self): |
| 225 |
b.triggered += 1 |
| 226 |
fq = FQ() |
| 227 |
def g(): |
| 228 |
A() |
| 229 |
def f(x): |
| 230 |
b.triggered = 0 |
| 231 |
g() |
| 232 |
i = 0 |
| 233 |
while i < x: |
| 234 |
i += 1 |
| 235 |
g() |
| 236 |
llop.gc__collect(lltype.Void) |
| 237 |
llop.gc__collect(lltype.Void) |
| 238 |
assert b.triggered > 0 |
| 239 |
g(); g() |
| 240 |
llop.gc__collect(lltype.Void) |
| 241 |
llop.gc__collect(lltype.Void) |
| 242 |
num_deleted = 0 |
| 243 |
while fq.next_dead() is not None: |
| 244 |
num_deleted += 1 |
| 245 |
return num_deleted + 1000 * b.triggered |
| 246 |
res = self.interpret(f, [5]) |
| 247 |
assert res in (3008, 4008, 5008), "res == %d" % (res,) |
| 248 |
|
| 249 |
def test_finalizer_two_queues_in_sequence(self): |
| 250 |
class B(object): |
| 251 |
pass |
| 252 |
b = B() |
| 253 |
b.nextid = 0 |
| 254 |
b.num_deleted_1 = 0 |
| 255 |
b.num_deleted_2 = 0 |
| 256 |
class A(object): |
| 257 |
def __init__(self): |
| 258 |
self.id = b.nextid |
| 259 |
b.nextid += 1 |
| 260 |
fq1.register_finalizer(self) |
| 261 |
class FQ1(rgc.FinalizerQueue): |
| 262 |
Class = A |
| 263 |
def finalizer_trigger(self): |
| 264 |
while True: |
| 265 |
a = self.next_dead() |
| 266 |
if a is None: |
| 267 |
break |
| 268 |
b.num_deleted_1 += 1 |
| 269 |
fq2.register_finalizer(a) |
| 270 |
class FQ2(rgc.FinalizerQueue): |
| 271 |
Class = A |
| 272 |
def finalizer_trigger(self): |
| 273 |
while self.next_dead() is not None: |
| 274 |
b.num_deleted_2 += 1 |
| 275 |
fq1 = FQ1() |
| 276 |
fq2 = FQ2() |
| 277 |
def f(x): |
| 278 |
A() |
| 279 |
i = 0 |
| 280 |
while i < x: |
| 281 |
i += 1 |
| 282 |
A() |
| 283 |
llop.gc__collect(lltype.Void) |
| 284 |
llop.gc__collect(lltype.Void) |
| 285 |
llop.gc__collect(lltype.Void) |
| 286 |
llop.gc__collect(lltype.Void) |
| 287 |
return b.num_deleted_1 + b.num_deleted_2 * 1000 |
| 288 |
res = self.interpret(f, [5]) |
| 289 |
assert res == 6006 |
| 290 |
|
| 291 |
def test_finalizer_calls_malloc(self): |
| 292 |
class B(object): |
| 293 |
pass |
| 294 |
b = B() |
| 295 |
b.nextid = 0 |
| 296 |
b.num_deleted = 0 |
| 297 |
class A(object): |
| 298 |
def __init__(self): |
| 299 |
self.id = b.nextid |
| 300 |
b.nextid += 1 |
| 301 |
fq.register_finalizer(self) |
| 302 |
class C(A): |
| 303 |
pass |
| 304 |
class FQ(rgc.FinalizerQueue): |
| 305 |
Class = A |
| 306 |
def finalizer_trigger(self): |
| 307 |
while True: |
| 308 |
a = self.next_dead() |
| 309 |
if a is None: |
| 310 |
break |
| 311 |
b.num_deleted += 1 |
| 312 |
if not isinstance(a, C): |
| 313 |
C() |
| 314 |
fq = FQ() |
| 315 |
def f(x): |
| 316 |
a = A() |
| 317 |
i = 0 |
| 318 |
while i < x: |
| 319 |
i += 1 |
| 320 |
a = A() |
| 321 |
a = None |
| 322 |
llop.gc__collect(lltype.Void) |
| 323 |
llop.gc__collect(lltype.Void) |
| 324 |
return b.num_deleted |
| 325 |
res = self.interpret(f, [5]) |
| 326 |
assert res == 12 |
| 327 |
|
| 328 |
def test_finalizer_calls_collect(self): |
| 329 |
class B(object): |
| 330 |
pass |
| 331 |
b = B() |
| 332 |
b.nextid = 0 |
| 333 |
b.num_deleted = 0 |
| 334 |
class A(object): |
| 335 |
def __init__(self): |
| 336 |
self.id = b.nextid |
| 337 |
b.nextid += 1 |
| 338 |
fq.register_finalizer(self) |
| 339 |
class FQ(rgc.FinalizerQueue): |
| 340 |
Class = A |
| 341 |
def finalizer_trigger(self): |
| 342 |
while self.next_dead() is not None: |
| 343 |
b.num_deleted += 1 |
| 344 |
llop.gc__collect(lltype.Void) |
| 345 |
fq = FQ() |
| 346 |
def f(x): |
| 347 |
a = A() |
| 348 |
i = 0 |
| 349 |
while i < x: |
| 350 |
i += 1 |
| 351 |
a = A() |
| 352 |
a = None |
| 353 |
llop.gc__collect(lltype.Void) |
| 354 |
llop.gc__collect(lltype.Void) |
| 355 |
return b.num_deleted |
| 356 |
res = self.interpret(f, [5]) |
| 357 |
assert res == 6 |
| 358 |
|
| 359 |
def test_finalizer_resurrects(self): |
| 360 |
class B(object): |
| 361 |
pass |
| 362 |
b = B() |
| 363 |
b.nextid = 0 |
| 364 |
b.num_deleted = 0 |
| 365 |
class A(object): |
| 366 |
def __init__(self): |
| 367 |
self.id = b.nextid |
| 368 |
b.nextid += 1 |
| 369 |
fq.register_finalizer(self) |
| 370 |
class FQ(rgc.FinalizerQueue): |
| 371 |
Class = A |
| 372 |
def finalizer_trigger(self): |
| 373 |
while True: |
| 374 |
a = self.next_dead() |
| 375 |
if a is None: |
| 376 |
break |
| 377 |
b.num_deleted += 1 |
| 378 |
b.a = a |
| 379 |
fq = FQ() |
| 380 |
def f(x): |
| 381 |
a = A() |
| 382 |
i = 0 |
| 383 |
while i < x: |
| 384 |
i += 1 |
| 385 |
a = A() |
| 386 |
a = None |
| 387 |
llop.gc__collect(lltype.Void) |
| 388 |
llop.gc__collect(lltype.Void) |
| 389 |
aid = b.a.id |
| 390 |
b.a = None |
| 391 |
|
| 392 |
llop.gc__collect(lltype.Void) |
| 393 |
llop.gc__collect(lltype.Void) |
| 394 |
return b.num_deleted * 10 + aid + 100 * (b.a is None) |
| 395 |
res = self.interpret(f, [5]) |
| 396 |
assert 160 <= res <= 165 |
| 397 |
|
| 398 |
def test_custom_trace(self): |
| 399 |
from rpython.rtyper.lltypesystem import llmemory |
| 400 |
from rpython.rtyper.lltypesystem.llarena import ArenaError |
| 401 |
|
| 402 |
S = lltype.GcStruct('S', ('x', llmemory.Address), |
| 403 |
('y', llmemory.Address)) |
| 404 |
T = lltype.GcStruct('T', ('z', lltype.Signed)) |
| 405 |
offset_of_x = llmemory.offsetof(S, 'x') |
| 406 |
def customtrace(gc, obj, callback, arg1, arg2): |
| 407 |
gc._trace_callback(callback, arg1, arg2, obj + offset_of_x) |
| 408 |
lambda_customtrace = lambda: customtrace |
| 409 |
|
| 410 |
for attrname in ['x', 'y']: |
| 411 |
def setup(): |
| 412 |
rgc.register_custom_trace_hook(S, lambda_customtrace) |
| 413 |
s1 = lltype.malloc(S) |
| 414 |
tx = lltype.malloc(T) |
| 415 |
tx.z = 42 |
| 416 |
ty = lltype.malloc(T) |
| 417 |
s1.x = llmemory.cast_ptr_to_adr(tx) |
| 418 |
s1.y = llmemory.cast_ptr_to_adr(ty) |
| 419 |
return s1 |
| 420 |
def f(): |
| 421 |
s1 = setup() |
| 422 |
llop.gc__collect(lltype.Void) |
| 423 |
return llmemory.cast_adr_to_ptr(getattr(s1, attrname), |
| 424 |
lltype.Ptr(T)) |
| 425 |
if attrname == 'x': |
| 426 |
res = self.interpret(f, []) |
| 427 |
assert res.z == 42 |
| 428 |
else: |
| 429 |
py.test.raises((RuntimeError, ArenaError), |
| 430 |
self.interpret, f, []) |
| 431 |
|
| 432 |
def test_weakref(self): |
| 433 |
import weakref |
| 434 |
class A(object): |
| 435 |
pass |
| 436 |
def g(): |
| 437 |
a = A() |
| 438 |
return weakref.ref(a) |
| 439 |
def f(): |
| 440 |
a = A() |
| 441 |
ref = weakref.ref(a) |
| 442 |
result = ref() is a |
| 443 |
ref = g() |
| 444 |
llop.gc__collect(lltype.Void) |
| 445 |
result = result and (ref() is None) |
| 446 |
|
| 447 |
llop.gc__collect(lltype.Void) |
| 448 |
result = result and (ref() is None) |
| 449 |
return result |
| 450 |
res = self.interpret(f, []) |
| 451 |
assert res |
| 452 |
|
| 453 |
def test_weakref_to_object_with_destructor(self): |
| 454 |
import weakref |
| 455 |
class A(object): |
| 456 |
count = 0 |
| 457 |
a = A() |
| 458 |
class B(object): |
| 459 |
def __del__(self): |
| 460 |
a.count += 1 |
| 461 |
def g(): |
| 462 |
b = B() |
| 463 |
return weakref.ref(b) |
| 464 |
def f(): |
| 465 |
ref = g() |
| 466 |
llop.gc__collect(lltype.Void) |
| 467 |
llop.gc__collect(lltype.Void) |
| 468 |
result = a.count == 1 and (ref() is None) |
| 469 |
return result |
| 470 |
res = self.interpret(f, []) |
| 471 |
assert res |
| 472 |
|
| 473 |
def test_weakref_to_object_with_finalizer(self): |
| 474 |
import weakref |
| 475 |
class A(object): |
| 476 |
count = 0 |
| 477 |
a = A() |
| 478 |
class B(object): |
| 479 |
pass |
| 480 |
class FQ(rgc.FinalizerQueue): |
| 481 |
Class = B |
| 482 |
def finalizer_trigger(self): |
| 483 |
while self.next_dead() is not None: |
| 484 |
a.count += 1 |
| 485 |
fq = FQ() |
| 486 |
def g(): |
| 487 |
b = B() |
| 488 |
fq.register_finalizer(b) |
| 489 |
return weakref.ref(b) |
| 490 |
def f(): |
| 491 |
ref = g() |
| 492 |
llop.gc__collect(lltype.Void) |
| 493 |
llop.gc__collect(lltype.Void) |
| 494 |
result = a.count == 1 and (ref() is None) |
| 495 |
return result |
| 496 |
res = self.interpret(f, []) |
| 497 |
assert res |
| 498 |
|
| 499 |
def test_bug_1(self): |
| 500 |
import weakref |
| 501 |
class B(object): |
| 502 |
pass |
| 503 |
def g(): |
| 504 |
b = B() |
| 505 |
llop.gc__collect(lltype.Void) |
| 506 |
ref = weakref.ref(B()) |
| 507 |
b.ref = ref |
| 508 |
return ref |
| 509 |
def f(): |
| 510 |
ref = g() |
| 511 |
llop.gc__collect(lltype.Void) |
| 512 |
llop.gc__collect(lltype.Void) |
| 513 |
result = (ref() is None) |
| 514 |
return result |
| 515 |
res = self.interpret(f, []) |
| 516 |
assert res |
| 517 |
|
| 518 |
def test_cycle_with_weakref_and_finalizer(self): |
| 519 |
import weakref |
| 520 |
class A(object): |
| 521 |
count = 0 |
| 522 |
a = A() |
| 523 |
class B(object): |
| 524 |
pass |
| 525 |
class FQ(rgc.FinalizerQueue): |
| 526 |
Class = B |
| 527 |
def finalizer_trigger(self): |
| 528 |
while True: |
| 529 |
b = self.next_dead() |
| 530 |
if b is None: |
| 531 |
break |
| 532 |
|
| 533 |
if b.ref() is None: |
| 534 |
a.count += 10 |
| 535 |
else: |
| 536 |
a.count = 666 |
| 537 |
fq = FQ() |
| 538 |
class C(object): |
| 539 |
pass |
| 540 |
def g(): |
| 541 |
c = C() |
| 542 |
c.b = B() |
| 543 |
fq.register_finalizer(c.b) |
| 544 |
ref = weakref.ref(c) |
| 545 |
c.b.ref = ref |
| 546 |
return ref |
| 547 |
def f(): |
| 548 |
ref = g() |
| 549 |
llop.gc__collect(lltype.Void) |
| 550 |
llop.gc__collect(lltype.Void) |
| 551 |
result = a.count + (ref() is None) |
| 552 |
return result |
| 553 |
res = self.interpret(f, []) |
| 554 |
assert res == 11 |
| 555 |
|
| 556 |
def test_weakref_to_object_with_finalizer_ordering(self): |
| 557 |
import weakref |
| 558 |
class A(object): |
| 559 |
count = 0 |
| 560 |
a = A() |
| 561 |
expected_invalid = self.WREF_IS_INVALID_BEFORE_DEL_IS_CALLED |
| 562 |
class B(object): |
| 563 |
pass |
| 564 |
class FQ(rgc.FinalizerQueue): |
| 565 |
Class = B |
| 566 |
def finalizer_trigger(self): |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
while True: |
| 572 |
b = self.next_dead() |
| 573 |
if b is None: |
| 574 |
break |
| 575 |
if expected_invalid: |
| 576 |
if b.ref() is None: |
| 577 |
a.count += 10 |
| 578 |
else: |
| 579 |
a.count = 666 |
| 580 |
else: |
| 581 |
if b.ref() is b: |
| 582 |
a.count += 10 |
| 583 |
else: |
| 584 |
a.count = 666 |
| 585 |
fq = FQ() |
| 586 |
def g(): |
| 587 |
b = B() |
| 588 |
fq.register_finalizer(b) |
| 589 |
ref = weakref.ref(b) |
| 590 |
b.ref = ref |
| 591 |
return ref |
| 592 |
def f(): |
| 593 |
ref = g() |
| 594 |
llop.gc__collect(lltype.Void) |
| 595 |
llop.gc__collect(lltype.Void) |
| 596 |
result = a.count + (ref() is None) |
| 597 |
return result |
| 598 |
res = self.interpret(f, []) |
| 599 |
assert res == 11 |
| 600 |
|
| 601 |
def test_weakref_bug_1(self): |
| 602 |
import weakref |
| 603 |
class A(object): |
| 604 |
pass |
| 605 |
class B(object): |
| 606 |
pass |
| 607 |
class FQ(rgc.FinalizerQueue): |
| 608 |
Class = B |
| 609 |
def finalizer_trigger(self): |
| 610 |
while True: |
| 611 |
b = self.next_dead() |
| 612 |
if b is None: |
| 613 |
break |
| 614 |
b.wref().x += 1 |
| 615 |
fq = FQ() |
| 616 |
def g(a): |
| 617 |
b = B() |
| 618 |
fq.register_finalizer(b) |
| 619 |
b.wref = weakref.ref(a) |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
def f(): |
| 625 |
a = A() |
| 626 |
a.x = 10 |
| 627 |
g(a) |
| 628 |
llop.gc__collect(lltype.Void) |
| 629 |
return a.x |
| 630 |
res = self.interpret(f, []) |
| 631 |
assert res == 11 |
| 632 |
|
| 633 |
def test_id(self): |
| 634 |
class A(object): |
| 635 |
pass |
| 636 |
a1 = A() |
| 637 |
def f(): |
| 638 |
a2 = A() |
| 639 |
a3 = A() |
| 640 |
id1 = compute_unique_id(a1) |
| 641 |
id2 = compute_unique_id(a2) |
| 642 |
id3 = compute_unique_id(a3) |
| 643 |
llop.gc__collect(lltype.Void) |
| 644 |
error = 0 |
| 645 |
if id1 != compute_unique_id(a1): error += 1 |
| 646 |
if id2 != compute_unique_id(a2): error += 2 |
| 647 |
if id3 != compute_unique_id(a3): error += 4 |
| 648 |
return error |
| 649 |
res = self.interpret(f, []) |
| 650 |
assert res == 0 |
| 651 |
|
| 652 |
def test_finalizer_calls_malloc_during_minor_collect(self): |
| 653 |
|
| 654 |
class B(object): |
| 655 |
pass |
| 656 |
b = B() |
| 657 |
b.nextid = 0 |
| 658 |
b.num_deleted = 0 |
| 659 |
b.all = [] |
| 660 |
class A(object): |
| 661 |
def __init__(self): |
| 662 |
self.id = b.nextid |
| 663 |
b.nextid += 1 |
| 664 |
fq.register_finalizer(self) |
| 665 |
class FQ(rgc.FinalizerQueue): |
| 666 |
Class = A |
| 667 |
def finalizer_trigger(self): |
| 668 |
while self.next_dead() is not None: |
| 669 |
b.num_deleted += 1 |
| 670 |
b.all.append(D(b.num_deleted)) |
| 671 |
fq = FQ() |
| 672 |
class D(object): |
| 673 |
|
| 674 |
def __init__(self, x): |
| 675 |
self.x00 = self.x01 = self.x02 = self.x03 = self.x04 = x |
| 676 |
self.x10 = self.x11 = self.x12 = self.x13 = self.x14 = x |
| 677 |
self.x20 = self.x21 = self.x22 = self.x23 = self.x24 = x |
| 678 |
def f(x): |
| 679 |
i = 0 |
| 680 |
all = [None] * x |
| 681 |
a = A() |
| 682 |
del a |
| 683 |
while i < x: |
| 684 |
d = D(i) |
| 685 |
all[i] = d |
| 686 |
i += 1 |
| 687 |
return b.num_deleted + len(all) |
| 688 |
res = self.interpret(f, [500]) |
| 689 |
assert res == 1 + 500 |
| 690 |
|
| 691 |
|
| 692 |
def test_collect_during_collect(self): |
| 693 |
class B(object): |
| 694 |
pass |
| 695 |
b = B() |
| 696 |
b.nextid = 1 |
| 697 |
b.num_deleted = 0 |
| 698 |
b.num_deleted_c = 0 |
| 699 |
class A(object): |
| 700 |
def __init__(self): |
| 701 |
self.id = b.nextid |
| 702 |
b.nextid += 1 |
| 703 |
fq.register_finalizer(self) |
| 704 |
class C(A): |
| 705 |
pass |
| 706 |
class FQ(rgc.FinalizerQueue): |
| 707 |
Class = A |
| 708 |
def finalizer_trigger(self): |
| 709 |
while True: |
| 710 |
a = self.next_dead() |
| 711 |
if a is None: |
| 712 |
break |
| 713 |
llop.gc__collect(lltype.Void) |
| 714 |
b.num_deleted += 1 |
| 715 |
if isinstance(a, C): |
| 716 |
b.num_deleted_c += 1 |
| 717 |
else: |
| 718 |
C() |
| 719 |
C() |
| 720 |
fq = FQ() |
| 721 |
def f(x, y): |
| 722 |
persistent_a1 = A() |
| 723 |
persistent_a2 = A() |
| 724 |
i = 0 |
| 725 |
while i < x: |
| 726 |
i += 1 |
| 727 |
a = A() |
| 728 |
persistent_a3 = A() |
| 729 |
persistent_a4 = A() |
| 730 |
llop.gc__collect(lltype.Void) |
| 731 |
llop.gc__collect(lltype.Void) |
| 732 |
b.bla = persistent_a1.id + persistent_a2.id + persistent_a3.id + persistent_a4.id |
| 733 |
print(b.num_deleted_c) |
| 734 |
return b.num_deleted |
| 735 |
res = self.interpret(f, [4, 42]) |
| 736 |
assert res == 12 |
| 737 |
|
| 738 |
def test_print_leak(self): |
| 739 |
def f(n): |
| 740 |
for i in range(n): |
| 741 |
print(i) |
| 742 |
return 42 |
| 743 |
res = self.interpret(f, [10]) |
| 744 |
assert res == 42 |
| 745 |
|
| 746 |
def test_weakref_across_minor_collection(self): |
| 747 |
import weakref |
| 748 |
class A: |
| 749 |
pass |
| 750 |
def f(x): |
| 751 |
a = A() |
| 752 |
a.foo = x |
| 753 |
ref = weakref.ref(a) |
| 754 |
all = [None] * x |
| 755 |
i = 0 |
| 756 |
while i < x: |
| 757 |
all[i] = [i] * i |
| 758 |
i += 1 |
| 759 |
assert ref() is a |
| 760 |
llop.gc__collect(lltype.Void) |
| 761 |
assert ref() is a |
| 762 |
return a.foo + len(all) |
| 763 |
res = self.interpret(f, [20]) |
| 764 |
assert res == 20 + 20 |
| 765 |
|
| 766 |
def test_young_weakref_to_old_object(self): |
| 767 |
import weakref |
| 768 |
class A: |
| 769 |
pass |
| 770 |
def f(x): |
| 771 |
a = A() |
| 772 |
llop.gc__collect(lltype.Void) |
| 773 |
|
| 774 |
ref = weakref.ref(a) |
| 775 |
|
| 776 |
all = [None] * x |
| 777 |
i = 0 |
| 778 |
while i < x: |
| 779 |
all[i] = [i] * i |
| 780 |
i += 1 |
| 781 |
|
| 782 |
assert ref() is a |
| 783 |
llop.gc__collect(lltype.Void) |
| 784 |
|
| 785 |
return ref() is a |
| 786 |
res = self.interpret(f, [20]) |
| 787 |
assert res == True |
| 788 |
|
| 789 |
def test_weakref_to_prebuilt(self): |
| 790 |
import weakref |
| 791 |
class A: |
| 792 |
pass |
| 793 |
a = A() |
| 794 |
def f(x): |
| 795 |
ref = weakref.ref(a) |
| 796 |
assert ref() is a |
| 797 |
llop.gc__collect(lltype.Void) |
| 798 |
return ref() is a |
| 799 |
res = self.interpret(f, [20]) |
| 800 |
assert res == True |
| 801 |
|
| 802 |
def test_many_weakrefs(self): |
| 803 |
|
| 804 |
|
| 805 |
import weakref |
| 806 |
class A: |
| 807 |
pass |
| 808 |
def f(x): |
| 809 |
a = A() |
| 810 |
i = 0 |
| 811 |
while i < x: |
| 812 |
ref = weakref.ref(a) |
| 813 |
assert ref() is a |
| 814 |
i += 1 |
| 815 |
self.interpret(f, [1100]) |
| 816 |
|
| 817 |
def test_nongc_static_root(self): |
| 818 |
from rpython.rtyper.lltypesystem import lltype |
| 819 |
T1 = lltype.GcStruct("C", ('x', lltype.Signed)) |
| 820 |
T2 = lltype.Struct("C", ('p', lltype.Ptr(T1))) |
| 821 |
static = lltype.malloc(T2, immortal=True) |
| 822 |
def f(): |
| 823 |
t1 = lltype.malloc(T1) |
| 824 |
t1.x = 42 |
| 825 |
static.p = t1 |
| 826 |
llop.gc__collect(lltype.Void) |
| 827 |
return static.p.x |
| 828 |
res = self.interpret(f, []) |
| 829 |
assert res == 42 |
| 830 |
|
| 831 |
def test_can_move(self): |
| 832 |
TP = lltype.GcArray(lltype.Float) |
| 833 |
def func(): |
| 834 |
return rgc.can_move(lltype.malloc(TP, 1)) |
| 835 |
assert self.interpret(func, []) == self.GC_CAN_MOVE |
| 836 |
|
| 837 |
def test_trace_array_of_structs(self): |
| 838 |
R = lltype.GcStruct('R', ('i', lltype.Signed)) |
| 839 |
S1 = lltype.GcArray(('p1', lltype.Ptr(R))) |
| 840 |
S2 = lltype.GcArray(('p1', lltype.Ptr(R)), |
| 841 |
('p2', lltype.Ptr(R))) |
| 842 |
S3 = lltype.GcArray(('p1', lltype.Ptr(R)), |
| 843 |
('p2', lltype.Ptr(R)), |
| 844 |
('p3', lltype.Ptr(R))) |
| 845 |
def func(): |
| 846 |
s1 = lltype.malloc(S1, 2) |
| 847 |
s1[0].p1 = lltype.malloc(R) |
| 848 |
s1[1].p1 = lltype.malloc(R) |
| 849 |
s2 = lltype.malloc(S2, 2) |
| 850 |
s2[0].p1 = lltype.malloc(R) |
| 851 |
s2[0].p2 = lltype.malloc(R) |
| 852 |
s2[1].p1 = lltype.malloc(R) |
| 853 |
s2[1].p2 = lltype.malloc(R) |
| 854 |
s3 = lltype.malloc(S3, 2) |
| 855 |
s3[0].p1 = lltype.malloc(R) |
| 856 |
s3[0].p2 = lltype.malloc(R) |
| 857 |
s3[0].p3 = lltype.malloc(R) |
| 858 |
s3[1].p1 = lltype.malloc(R) |
| 859 |
s3[1].p2 = lltype.malloc(R) |
| 860 |
s3[1].p3 = lltype.malloc(R) |
| 861 |
s1[0].p1.i = 100 |
| 862 |
s1[1].p1.i = 101 |
| 863 |
s2[0].p1.i = 102 |
| 864 |
s2[0].p2.i = 103 |
| 865 |
s2[1].p1.i = 104 |
| 866 |
s2[1].p2.i = 105 |
| 867 |
s3[0].p1.i = 106 |
| 868 |
s3[0].p2.i = 107 |
| 869 |
s3[0].p3.i = 108 |
| 870 |
s3[1].p1.i = 109 |
| 871 |
s3[1].p2.i = 110 |
| 872 |
s3[1].p3.i = 111 |
| 873 |
rgc.collect() |
| 874 |
return ((s1[0].p1.i == 100) + |
| 875 |
(s1[1].p1.i == 101) + |
| 876 |
(s2[0].p1.i == 102) + |
| 877 |
(s2[0].p2.i == 103) + |
| 878 |
(s2[1].p1.i == 104) + |
| 879 |
(s2[1].p2.i == 105) + |
| 880 |
(s3[0].p1.i == 106) + |
| 881 |
(s3[0].p2.i == 107) + |
| 882 |
(s3[0].p3.i == 108) + |
| 883 |
(s3[1].p1.i == 109) + |
| 884 |
(s3[1].p2.i == 110) + |
| 885 |
(s3[1].p3.i == 111)) |
| 886 |
res = self.interpret(func, []) |
| 887 |
assert res == 12 |
| 888 |
|
| 889 |
def test_shrink_array(self): |
| 890 |
from rpython.rtyper.lltypesystem.rstr import STR |
| 891 |
|
| 892 |
def f(n, m, gc_can_shrink_array): |
| 893 |
ptr = lltype.malloc(STR, n) |
| 894 |
ptr.hash = 0x62 |
| 895 |
ptr.chars[0] = 'A' |
| 896 |
ptr.chars[1] = 'B' |
| 897 |
ptr.chars[2] = 'C' |
| 898 |
ptr2 = rgc.ll_shrink_array(ptr, 2) |
| 899 |
assert (ptr == ptr2) == gc_can_shrink_array |
| 900 |
rgc.collect() |
| 901 |
return ( ord(ptr2.chars[0]) + |
| 902 |
(ord(ptr2.chars[1]) << 8) + |
| 903 |
(len(ptr2.chars) << 16) + |
| 904 |
(ptr2.hash << 24)) |
| 905 |
|
| 906 |
flag = self.GC_CAN_SHRINK_ARRAY |
| 907 |
assert self.interpret(f, [3, 0, flag]) == 0x62024241 |
| 908 |
|
| 909 |
|
| 910 |
flag = self.GC_CAN_SHRINK_BIG_ARRAY |
| 911 |
bigsize = self.BUT_HOW_BIG_IS_A_BIG_STRING |
| 912 |
assert self.interpret(f, [bigsize, 0, flag]) == 0x62024241 |
| 913 |
|
| 914 |
def test_tagged_simple(self): |
| 915 |
class Unrelated(object): |
| 916 |
pass |
| 917 |
|
| 918 |
u = Unrelated() |
| 919 |
u.x = UnboxedObject(47) |
| 920 |
def fn(n): |
| 921 |
rgc.collect() |
| 922 |
if n > 0: |
| 923 |
x = BoxedObject(n) |
| 924 |
else: |
| 925 |
x = UnboxedObject(n) |
| 926 |
u.x = x |
| 927 |
rgc.collect() |
| 928 |
return x.meth(100) |
| 929 |
res = self.interpret(fn, [1000], taggedpointers=True) |
| 930 |
assert res == 1102 |
| 931 |
res = self.interpret(fn, [-1000], taggedpointers=True) |
| 932 |
assert res == -897 |
| 933 |
|
| 934 |
def test_tagged_prebuilt(self): |
| 935 |
|
| 936 |
class F: |
| 937 |
pass |
| 938 |
|
| 939 |
f = F() |
| 940 |
f.l = [UnboxedObject(10)] |
| 941 |
def fn(n): |
| 942 |
if n > 0: |
| 943 |
x = BoxedObject(n) |
| 944 |
else: |
| 945 |
x = UnboxedObject(n) |
| 946 |
f.l.append(x) |
| 947 |
rgc.collect() |
| 948 |
return f.l[-1].meth(100) |
| 949 |
res = self.interpret(fn, [1000], taggedpointers=True) |
| 950 |
assert res == 1102 |
| 951 |
res = self.interpret(fn, [-1000], taggedpointers=True) |
| 952 |
assert res == -897 |
| 953 |
|
| 954 |
def test_tagged_id(self): |
| 955 |
class Unrelated(object): |
| 956 |
pass |
| 957 |
|
| 958 |
u = Unrelated() |
| 959 |
u.x = UnboxedObject(0) |
| 960 |
def fn(n): |
| 961 |
id_prebuilt1 = compute_unique_id(u.x) |
| 962 |
if n > 0: |
| 963 |
x = BoxedObject(n) |
| 964 |
else: |
| 965 |
x = UnboxedObject(n) |
| 966 |
id_x1 = compute_unique_id(x) |
| 967 |
rgc.collect() |
| 968 |
id_prebuilt2 = compute_unique_id(u.x) |
| 969 |
id_x2 = compute_unique_id(x) |
| 970 |
print(u.x, id_prebuilt1, id_prebuilt2) |
| 971 |
print(x, id_x1, id_x2) |
| 972 |
return ((id_x1 == id_x2) * 1 + |
| 973 |
(id_prebuilt1 == id_prebuilt2) * 10 + |
| 974 |
(id_x1 != id_prebuilt1) * 100) |
| 975 |
res = self.interpret(fn, [1000], taggedpointers=True) |
| 976 |
assert res == 111 |
| 977 |
res = self.interpret(fn, [-1000], taggedpointers=True) |
| 978 |
assert res == 111 |
| 979 |
|
| 980 |
def test_writebarrier_before_copy(self): |
| 981 |
S = lltype.GcStruct('S', ('x', lltype.Char)) |
| 982 |
TP = lltype.GcArray(lltype.Ptr(S)) |
| 983 |
def fn(): |
| 984 |
l = lltype.malloc(TP, 100) |
| 985 |
l2 = lltype.malloc(TP, 100) |
| 986 |
for i in range(100): |
| 987 |
l[i] = lltype.malloc(S) |
| 988 |
rgc.ll_arraycopy(l, l2, 50, 0, 50) |
| 989 |
x = [] |
| 990 |
|
| 991 |
t = (1, lltype.malloc(S)) |
| 992 |
for i in range(20): |
| 993 |
x.append(t) |
| 994 |
for i in range(50): |
| 995 |
assert l2[i] == l[50 + i] |
| 996 |
return 0 |
| 997 |
|
| 998 |
self.interpret(fn, []) |
| 999 |
|
| 1000 |
def test_writebarrier_before_copy_manually_copy_card_bits(self): |
| 1001 |
S = lltype.GcStruct('S', ('x', lltype.Char)) |
| 1002 |
TP = lltype.GcArray(lltype.Ptr(S)) |
| 1003 |
def fn(): |
| 1004 |
l1 = lltype.malloc(TP, 65) |
| 1005 |
l2 = lltype.malloc(TP, 33) |
| 1006 |
for i in range(65): |
| 1007 |
l1[i] = lltype.malloc(S) |
| 1008 |
l = lltype.malloc(TP, 100) |
| 1009 |
i = 0 |
| 1010 |
while i < 65: |
| 1011 |
l[i] = l1[i] |
| 1012 |
i += 1 |
| 1013 |
rgc.ll_arraycopy(l, l2, 0, 0, 33) |
| 1014 |
x = [] |
| 1015 |
|
| 1016 |
t = (1, lltype.malloc(S)) |
| 1017 |
for i in range(20): |
| 1018 |
x.append(t) |
| 1019 |
for i in range(33): |
| 1020 |
assert l2[i] == l[i] |
| 1021 |
return 0 |
| 1022 |
|
| 1023 |
self.interpret(fn, []) |
| 1024 |
|
| 1025 |
def test_stringbuilder(self): |
| 1026 |
def fn(): |
| 1027 |
s = StringBuilder(4) |
| 1028 |
s.append("abcd") |
| 1029 |
s.append("defg") |
| 1030 |
s.append("rty") |
| 1031 |
s.append_multiple_char('y', 1000) |
| 1032 |
rgc.collect() |
| 1033 |
s.append_multiple_char('y', 1000) |
| 1034 |
res = s.build()[1000] |
| 1035 |
rgc.collect() |
| 1036 |
return ord(res) |
| 1037 |
res = self.interpret(fn, []) |
| 1038 |
assert res == ord('y') |
| 1039 |
|
| 1040 |
def test_gcflag_extra(self): |
| 1041 |
class A: |
| 1042 |
pass |
| 1043 |
a1 = A() |
| 1044 |
def fn(): |
| 1045 |
a2 = A() |
| 1046 |
if not rgc.has_gcflag_extra(): |
| 1047 |
return |
| 1048 |
assert rgc.get_gcflag_extra(a1) == False |
| 1049 |
assert rgc.get_gcflag_extra(a2) == False |
| 1050 |
rgc.toggle_gcflag_extra(a1) |
| 1051 |
assert rgc.get_gcflag_extra(a1) == True |
| 1052 |
assert rgc.get_gcflag_extra(a2) == False |
| 1053 |
rgc.toggle_gcflag_extra(a2) |
| 1054 |
assert rgc.get_gcflag_extra(a1) == True |
| 1055 |
assert rgc.get_gcflag_extra(a2) == True |
| 1056 |
rgc.toggle_gcflag_extra(a1) |
| 1057 |
assert rgc.get_gcflag_extra(a1) == False |
| 1058 |
assert rgc.get_gcflag_extra(a2) == True |
| 1059 |
rgc.toggle_gcflag_extra(a2) |
| 1060 |
assert rgc.get_gcflag_extra(a1) == False |
| 1061 |
assert rgc.get_gcflag_extra(a2) == False |
| 1062 |
self.interpret(fn, []) |
| 1063 |
|
| 1064 |
def test_register_custom_trace_hook(self): |
| 1065 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 1066 |
called = [] |
| 1067 |
|
| 1068 |
def trace_hook(gc, obj, callback, arg1, arg2): |
| 1069 |
called.append("called") |
| 1070 |
lambda_trace_hook = lambda: trace_hook |
| 1071 |
|
| 1072 |
def f(): |
| 1073 |
rgc.register_custom_trace_hook(S, lambda_trace_hook) |
| 1074 |
s = lltype.malloc(S) |
| 1075 |
rgc.collect() |
| 1076 |
keepalive_until_here(s) |
| 1077 |
|
| 1078 |
self.interpret(f, []) |
| 1079 |
assert called |
| 1080 |
|
| 1081 |
def test_pinning(self): |
| 1082 |
def fn(n): |
| 1083 |
s = str(n) |
| 1084 |
if not rgc.can_move(s): |
| 1085 |
return 13 |
| 1086 |
res = int(rgc.pin(s)) |
| 1087 |
if res: |
| 1088 |
rgc.unpin(s) |
| 1089 |
return res |
| 1090 |
|
| 1091 |
res = self.interpret(fn, [10]) |
| 1092 |
if not self.GCClass.moving_gc: |
| 1093 |
assert res == 13 |
| 1094 |
elif self.GCClass.can_usually_pin_objects: |
| 1095 |
assert res == 1 |
| 1096 |
else: |
| 1097 |
assert res == 0 or res == 13 |
| 1098 |
|
| 1099 |
def test__is_pinned(self): |
| 1100 |
def fn(n): |
| 1101 |
from rpython.rlib.debug import debug_print |
| 1102 |
s = str(n) |
| 1103 |
if not rgc.can_move(s): |
| 1104 |
return 13 |
| 1105 |
res = int(rgc.pin(s)) |
| 1106 |
if res: |
| 1107 |
res += int(rgc._is_pinned(s)) |
| 1108 |
rgc.unpin(s) |
| 1109 |
return res |
| 1110 |
|
| 1111 |
res = self.interpret(fn, [10]) |
| 1112 |
if not self.GCClass.moving_gc: |
| 1113 |
assert res == 13 |
| 1114 |
elif self.GCClass.can_usually_pin_objects: |
| 1115 |
assert res == 2 |
| 1116 |
else: |
| 1117 |
assert res == 0 or res == 13 |
| 1118 |
|
| 1119 |
def test_gettypeid(self): |
| 1120 |
class A(object): |
| 1121 |
pass |
| 1122 |
|
| 1123 |
def fn(): |
| 1124 |
a = A() |
| 1125 |
return rgc.get_typeid(a) |
| 1126 |
|
| 1127 |
self.interpret(fn, []) |
| 1128 |
|
| 1129 |
|
| 1130 |
from rpython.rlib.objectmodel import UnboxedValue |
| 1131 |
|
| 1132 |
class TaggedBase(object): |
| 1133 |
__slots__ = () |
| 1134 |
def meth(self, x): |
| 1135 |
raise NotImplementedError |
| 1136 |
|
| 1137 |
class BoxedObject(TaggedBase): |
| 1138 |
attrvalue = 66 |
| 1139 |
def __init__(self, normalint): |
| 1140 |
self.normalint = normalint |
| 1141 |
def meth(self, x): |
| 1142 |
return self.normalint + x + 2 |
| 1143 |
|
| 1144 |
class UnboxedObject(TaggedBase, UnboxedValue): |
| 1145 |
__slots__ = 'smallint' |
| 1146 |
def meth(self, x): |
| 1147 |
return self.smallint + x + 3 |