| 1 |
import copy |
| 2 |
import pickle |
| 3 |
import dis |
| 4 |
import threading |
| 5 |
import types |
| 6 |
import unittest |
| 7 |
from test.support import threading_helper, check_impl_detail, requires_specialization |
| 8 |
from test.support.import_helper import import_module |
| 9 |
|
| 10 |
# Skip this module on other interpreters, it is cpython specific: |
| 11 |
if check_impl_detail(cpython=False): |
| 12 |
raise unittest.SkipTest('implementation detail specific to cpython') |
| 13 |
|
| 14 |
_testinternalcapi = import_module("_testinternalcapi") |
| 15 |
|
| 16 |
|
| 17 |
def disabling_optimizer(func): |
| 18 |
def wrapper(*args, **kwargs): |
| 19 |
if not hasattr(_testinternalcapi, "get_optimizer"): |
| 20 |
return func(*args, **kwargs) |
| 21 |
old_opt = _testinternalcapi.get_optimizer() |
| 22 |
_testinternalcapi.set_optimizer(None) |
| 23 |
try: |
| 24 |
return func(*args, **kwargs) |
| 25 |
finally: |
| 26 |
_testinternalcapi.set_optimizer(old_opt) |
| 27 |
|
| 28 |
return wrapper |
| 29 |
|
| 30 |
|
| 31 |
class TestBase(unittest.TestCase): |
| 32 |
def assert_specialized(self, f, opname): |
| 33 |
instructions = dis.get_instructions(f, adaptive=True) |
| 34 |
opnames = {instruction.opname for instruction in instructions} |
| 35 |
self.assertIn(opname, opnames) |
| 36 |
|
| 37 |
|
| 38 |
class TestLoadSuperAttrCache(unittest.TestCase): |
| 39 |
def test_descriptor_not_double_executed_on_spec_fail(self): |
| 40 |
calls = [] |
| 41 |
class Descriptor: |
| 42 |
def __get__(self, instance, owner): |
| 43 |
calls.append((instance, owner)) |
| 44 |
return lambda: 1 |
| 45 |
|
| 46 |
class C: |
| 47 |
d = Descriptor() |
| 48 |
|
| 49 |
class D(C): |
| 50 |
def f(self): |
| 51 |
return super().d() |
| 52 |
|
| 53 |
d = D() |
| 54 |
|
| 55 |
self.assertEqual(d.f(), 1) # warmup |
| 56 |
calls.clear() |
| 57 |
self.assertEqual(d.f(), 1) # try to specialize |
| 58 |
self.assertEqual(calls, [(d, D)]) |
| 59 |
|
| 60 |
|
| 61 |
class TestLoadAttrCache(unittest.TestCase): |
| 62 |
def test_descriptor_added_after_optimization(self): |
| 63 |
class Descriptor: |
| 64 |
pass |
| 65 |
|
| 66 |
class C: |
| 67 |
def __init__(self): |
| 68 |
self.x = 1 |
| 69 |
x = Descriptor() |
| 70 |
|
| 71 |
def f(o): |
| 72 |
return o.x |
| 73 |
|
| 74 |
o = C() |
| 75 |
for i in range(1025): |
| 76 |
assert f(o) == 1 |
| 77 |
|
| 78 |
Descriptor.__get__ = lambda self, instance, value: 2 |
| 79 |
Descriptor.__set__ = lambda *args: None |
| 80 |
|
| 81 |
self.assertEqual(f(o), 2) |
| 82 |
|
| 83 |
def test_metaclass_descriptor_added_after_optimization(self): |
| 84 |
class Descriptor: |
| 85 |
pass |
| 86 |
|
| 87 |
class Metaclass(type): |
| 88 |
attribute = Descriptor() |
| 89 |
|
| 90 |
class Class(metaclass=Metaclass): |
| 91 |
attribute = True |
| 92 |
|
| 93 |
def __get__(self, instance, owner): |
| 94 |
return False |
| 95 |
|
| 96 |
def __set__(self, instance, value): |
| 97 |
return None |
| 98 |
|
| 99 |
def f(): |
| 100 |
return Class.attribute |
| 101 |
|
| 102 |
for _ in range(1025): |
| 103 |
self.assertTrue(f()) |
| 104 |
|
| 105 |
Descriptor.__get__ = __get__ |
| 106 |
Descriptor.__set__ = __set__ |
| 107 |
|
| 108 |
for _ in range(1025): |
| 109 |
self.assertFalse(f()) |
| 110 |
|
| 111 |
def test_metaclass_descriptor_shadows_class_attribute(self): |
| 112 |
class Metaclass(type): |
| 113 |
@property |
| 114 |
def attribute(self): |
| 115 |
return True |
| 116 |
|
| 117 |
class Class(metaclass=Metaclass): |
| 118 |
attribute = False |
| 119 |
|
| 120 |
def f(): |
| 121 |
return Class.attribute |
| 122 |
|
| 123 |
for _ in range(1025): |
| 124 |
self.assertTrue(f()) |
| 125 |
|
| 126 |
def test_metaclass_set_descriptor_after_optimization(self): |
| 127 |
class Metaclass(type): |
| 128 |
pass |
| 129 |
|
| 130 |
class Class(metaclass=Metaclass): |
| 131 |
attribute = True |
| 132 |
|
| 133 |
@property |
| 134 |
def attribute(self): |
| 135 |
return False |
| 136 |
|
| 137 |
def f(): |
| 138 |
return Class.attribute |
| 139 |
|
| 140 |
for _ in range(1025): |
| 141 |
self.assertTrue(f()) |
| 142 |
|
| 143 |
Metaclass.attribute = attribute |
| 144 |
|
| 145 |
for _ in range(1025): |
| 146 |
self.assertFalse(f()) |
| 147 |
|
| 148 |
def test_metaclass_del_descriptor_after_optimization(self): |
| 149 |
class Metaclass(type): |
| 150 |
@property |
| 151 |
def attribute(self): |
| 152 |
return True |
| 153 |
|
| 154 |
class Class(metaclass=Metaclass): |
| 155 |
attribute = False |
| 156 |
|
| 157 |
def f(): |
| 158 |
return Class.attribute |
| 159 |
|
| 160 |
for _ in range(1025): |
| 161 |
self.assertTrue(f()) |
| 162 |
|
| 163 |
del Metaclass.attribute |
| 164 |
|
| 165 |
for _ in range(1025): |
| 166 |
self.assertFalse(f()) |
| 167 |
|
| 168 |
def test_type_descriptor_shadows_attribute_method(self): |
| 169 |
class Class: |
| 170 |
mro = None |
| 171 |
|
| 172 |
def f(): |
| 173 |
return Class.mro |
| 174 |
|
| 175 |
for _ in range(1025): |
| 176 |
self.assertIsNone(f()) |
| 177 |
|
| 178 |
def test_type_descriptor_shadows_attribute_member(self): |
| 179 |
class Class: |
| 180 |
__base__ = None |
| 181 |
|
| 182 |
def f(): |
| 183 |
return Class.__base__ |
| 184 |
|
| 185 |
for _ in range(1025): |
| 186 |
self.assertIs(f(), object) |
| 187 |
|
| 188 |
def test_type_descriptor_shadows_attribute_getset(self): |
| 189 |
class Class: |
| 190 |
__name__ = "Spam" |
| 191 |
|
| 192 |
def f(): |
| 193 |
return Class.__name__ |
| 194 |
|
| 195 |
for _ in range(1025): |
| 196 |
self.assertEqual(f(), "Class") |
| 197 |
|
| 198 |
def test_metaclass_getattribute(self): |
| 199 |
class Metaclass(type): |
| 200 |
def __getattribute__(self, name): |
| 201 |
return True |
| 202 |
|
| 203 |
class Class(metaclass=Metaclass): |
| 204 |
attribute = False |
| 205 |
|
| 206 |
def f(): |
| 207 |
return Class.attribute |
| 208 |
|
| 209 |
for _ in range(1025): |
| 210 |
self.assertTrue(f()) |
| 211 |
|
| 212 |
def test_metaclass_swap(self): |
| 213 |
class OldMetaclass(type): |
| 214 |
@property |
| 215 |
def attribute(self): |
| 216 |
return True |
| 217 |
|
| 218 |
class NewMetaclass(type): |
| 219 |
@property |
| 220 |
def attribute(self): |
| 221 |
return False |
| 222 |
|
| 223 |
class Class(metaclass=OldMetaclass): |
| 224 |
pass |
| 225 |
|
| 226 |
def f(): |
| 227 |
return Class.attribute |
| 228 |
|
| 229 |
for _ in range(1025): |
| 230 |
self.assertTrue(f()) |
| 231 |
|
| 232 |
Class.__class__ = NewMetaclass |
| 233 |
|
| 234 |
for _ in range(1025): |
| 235 |
self.assertFalse(f()) |
| 236 |
|
| 237 |
def test_load_shadowing_slot_should_raise_type_error(self): |
| 238 |
class Class: |
| 239 |
__slots__ = ("slot",) |
| 240 |
|
| 241 |
class Sneaky: |
| 242 |
__slots__ = ("shadowed",) |
| 243 |
shadowing = Class.slot |
| 244 |
|
| 245 |
def f(o): |
| 246 |
o.shadowing |
| 247 |
|
| 248 |
o = Sneaky() |
| 249 |
o.shadowed = 42 |
| 250 |
|
| 251 |
for _ in range(1025): |
| 252 |
with self.assertRaises(TypeError): |
| 253 |
f(o) |
| 254 |
|
| 255 |
def test_store_shadowing_slot_should_raise_type_error(self): |
| 256 |
class Class: |
| 257 |
__slots__ = ("slot",) |
| 258 |
|
| 259 |
class Sneaky: |
| 260 |
__slots__ = ("shadowed",) |
| 261 |
shadowing = Class.slot |
| 262 |
|
| 263 |
def f(o): |
| 264 |
o.shadowing = 42 |
| 265 |
|
| 266 |
o = Sneaky() |
| 267 |
|
| 268 |
for _ in range(1025): |
| 269 |
with self.assertRaises(TypeError): |
| 270 |
f(o) |
| 271 |
|
| 272 |
def test_load_borrowed_slot_should_not_crash(self): |
| 273 |
class Class: |
| 274 |
__slots__ = ("slot",) |
| 275 |
|
| 276 |
class Sneaky: |
| 277 |
borrowed = Class.slot |
| 278 |
|
| 279 |
def f(o): |
| 280 |
o.borrowed |
| 281 |
|
| 282 |
o = Sneaky() |
| 283 |
|
| 284 |
for _ in range(1025): |
| 285 |
with self.assertRaises(TypeError): |
| 286 |
f(o) |
| 287 |
|
| 288 |
def test_store_borrowed_slot_should_not_crash(self): |
| 289 |
class Class: |
| 290 |
__slots__ = ("slot",) |
| 291 |
|
| 292 |
class Sneaky: |
| 293 |
borrowed = Class.slot |
| 294 |
|
| 295 |
def f(o): |
| 296 |
o.borrowed = 42 |
| 297 |
|
| 298 |
o = Sneaky() |
| 299 |
|
| 300 |
for _ in range(1025): |
| 301 |
with self.assertRaises(TypeError): |
| 302 |
f(o) |
| 303 |
|
| 304 |
|
| 305 |
class TestLoadMethodCache(unittest.TestCase): |
| 306 |
def test_descriptor_added_after_optimization(self): |
| 307 |
class Descriptor: |
| 308 |
pass |
| 309 |
|
| 310 |
class Class: |
| 311 |
attribute = Descriptor() |
| 312 |
|
| 313 |
def __get__(self, instance, owner): |
| 314 |
return lambda: False |
| 315 |
|
| 316 |
def __set__(self, instance, value): |
| 317 |
return None |
| 318 |
|
| 319 |
def attribute(): |
| 320 |
return True |
| 321 |
|
| 322 |
instance = Class() |
| 323 |
instance.attribute = attribute |
| 324 |
|
| 325 |
def f(): |
| 326 |
return instance.attribute() |
| 327 |
|
| 328 |
for _ in range(1025): |
| 329 |
self.assertTrue(f()) |
| 330 |
|
| 331 |
Descriptor.__get__ = __get__ |
| 332 |
Descriptor.__set__ = __set__ |
| 333 |
|
| 334 |
for _ in range(1025): |
| 335 |
self.assertFalse(f()) |
| 336 |
|
| 337 |
def test_metaclass_descriptor_added_after_optimization(self): |
| 338 |
class Descriptor: |
| 339 |
pass |
| 340 |
|
| 341 |
class Metaclass(type): |
| 342 |
attribute = Descriptor() |
| 343 |
|
| 344 |
class Class(metaclass=Metaclass): |
| 345 |
def attribute(): |
| 346 |
return True |
| 347 |
|
| 348 |
def __get__(self, instance, owner): |
| 349 |
return lambda: False |
| 350 |
|
| 351 |
def __set__(self, instance, value): |
| 352 |
return None |
| 353 |
|
| 354 |
def f(): |
| 355 |
return Class.attribute() |
| 356 |
|
| 357 |
for _ in range(1025): |
| 358 |
self.assertTrue(f()) |
| 359 |
|
| 360 |
Descriptor.__get__ = __get__ |
| 361 |
Descriptor.__set__ = __set__ |
| 362 |
|
| 363 |
for _ in range(1025): |
| 364 |
self.assertFalse(f()) |
| 365 |
|
| 366 |
def test_metaclass_descriptor_shadows_class_attribute(self): |
| 367 |
class Metaclass(type): |
| 368 |
@property |
| 369 |
def attribute(self): |
| 370 |
return lambda: True |
| 371 |
|
| 372 |
class Class(metaclass=Metaclass): |
| 373 |
def attribute(): |
| 374 |
return False |
| 375 |
|
| 376 |
def f(): |
| 377 |
return Class.attribute() |
| 378 |
|
| 379 |
for _ in range(1025): |
| 380 |
self.assertTrue(f()) |
| 381 |
|
| 382 |
def test_metaclass_set_descriptor_after_optimization(self): |
| 383 |
class Metaclass(type): |
| 384 |
pass |
| 385 |
|
| 386 |
class Class(metaclass=Metaclass): |
| 387 |
def attribute(): |
| 388 |
return True |
| 389 |
|
| 390 |
@property |
| 391 |
def attribute(self): |
| 392 |
return lambda: False |
| 393 |
|
| 394 |
def f(): |
| 395 |
return Class.attribute() |
| 396 |
|
| 397 |
for _ in range(1025): |
| 398 |
self.assertTrue(f()) |
| 399 |
|
| 400 |
Metaclass.attribute = attribute |
| 401 |
|
| 402 |
for _ in range(1025): |
| 403 |
self.assertFalse(f()) |
| 404 |
|
| 405 |
def test_metaclass_del_descriptor_after_optimization(self): |
| 406 |
class Metaclass(type): |
| 407 |
@property |
| 408 |
def attribute(self): |
| 409 |
return lambda: True |
| 410 |
|
| 411 |
class Class(metaclass=Metaclass): |
| 412 |
def attribute(): |
| 413 |
return False |
| 414 |
|
| 415 |
def f(): |
| 416 |
return Class.attribute() |
| 417 |
|
| 418 |
for _ in range(1025): |
| 419 |
self.assertTrue(f()) |
| 420 |
|
| 421 |
del Metaclass.attribute |
| 422 |
|
| 423 |
for _ in range(1025): |
| 424 |
self.assertFalse(f()) |
| 425 |
|
| 426 |
def test_type_descriptor_shadows_attribute_method(self): |
| 427 |
class Class: |
| 428 |
def mro(): |
| 429 |
return ["Spam", "eggs"] |
| 430 |
|
| 431 |
def f(): |
| 432 |
return Class.mro() |
| 433 |
|
| 434 |
for _ in range(1025): |
| 435 |
self.assertEqual(f(), ["Spam", "eggs"]) |
| 436 |
|
| 437 |
def test_type_descriptor_shadows_attribute_member(self): |
| 438 |
class Class: |
| 439 |
def __base__(): |
| 440 |
return "Spam" |
| 441 |
|
| 442 |
def f(): |
| 443 |
return Class.__base__() |
| 444 |
|
| 445 |
for _ in range(1025): |
| 446 |
self.assertNotEqual(f(), "Spam") |
| 447 |
|
| 448 |
def test_metaclass_getattribute(self): |
| 449 |
class Metaclass(type): |
| 450 |
def __getattribute__(self, name): |
| 451 |
return lambda: True |
| 452 |
|
| 453 |
class Class(metaclass=Metaclass): |
| 454 |
def attribute(): |
| 455 |
return False |
| 456 |
|
| 457 |
def f(): |
| 458 |
return Class.attribute() |
| 459 |
|
| 460 |
for _ in range(1025): |
| 461 |
self.assertTrue(f()) |
| 462 |
|
| 463 |
def test_metaclass_swap(self): |
| 464 |
class OldMetaclass(type): |
| 465 |
@property |
| 466 |
def attribute(self): |
| 467 |
return lambda: True |
| 468 |
|
| 469 |
class NewMetaclass(type): |
| 470 |
@property |
| 471 |
def attribute(self): |
| 472 |
return lambda: False |
| 473 |
|
| 474 |
class Class(metaclass=OldMetaclass): |
| 475 |
pass |
| 476 |
|
| 477 |
def f(): |
| 478 |
return Class.attribute() |
| 479 |
|
| 480 |
for _ in range(1025): |
| 481 |
self.assertTrue(f()) |
| 482 |
|
| 483 |
Class.__class__ = NewMetaclass |
| 484 |
|
| 485 |
for _ in range(1025): |
| 486 |
self.assertFalse(f()) |
| 487 |
|
| 488 |
|
| 489 |
class TestCallCache(TestBase): |
| 490 |
def test_too_many_defaults_0(self): |
| 491 |
def f(): |
| 492 |
pass |
| 493 |
|
| 494 |
f.__defaults__ = (None,) |
| 495 |
for _ in range(1025): |
| 496 |
f() |
| 497 |
|
| 498 |
def test_too_many_defaults_1(self): |
| 499 |
def f(x): |
| 500 |
pass |
| 501 |
|
| 502 |
f.__defaults__ = (None, None) |
| 503 |
for _ in range(1025): |
| 504 |
f(None) |
| 505 |
f() |
| 506 |
|
| 507 |
def test_too_many_defaults_2(self): |
| 508 |
def f(x, y): |
| 509 |
pass |
| 510 |
|
| 511 |
f.__defaults__ = (None, None, None) |
| 512 |
for _ in range(1025): |
| 513 |
f(None, None) |
| 514 |
f(None) |
| 515 |
f() |
| 516 |
|
| 517 |
@disabling_optimizer |
| 518 |
@requires_specialization |
| 519 |
def test_assign_init_code(self): |
| 520 |
class MyClass: |
| 521 |
def __init__(self): |
| 522 |
pass |
| 523 |
|
| 524 |
def instantiate(): |
| 525 |
return MyClass() |
| 526 |
|
| 527 |
# Trigger specialization |
| 528 |
for _ in range(1025): |
| 529 |
instantiate() |
| 530 |
self.assert_specialized(instantiate, "CALL_ALLOC_AND_ENTER_INIT") |
| 531 |
|
| 532 |
def count_args(self, *args): |
| 533 |
self.num_args = len(args) |
| 534 |
|
| 535 |
# Set MyClass.__init__.__code__ to a code object that is incompatible |
| 536 |
# (uses varargs) with the current specialization |
| 537 |
MyClass.__init__.__code__ = count_args.__code__ |
| 538 |
instantiate() |
| 539 |
|
| 540 |
def test_recursion_check_for_general_calls(self): |
| 541 |
def test(default=None): |
| 542 |
return test() |
| 543 |
|
| 544 |
with self.assertRaises(RecursionError): |
| 545 |
test() |
| 546 |
|
| 547 |
|
| 548 |
@threading_helper.requires_working_threading() |
| 549 |
@requires_specialization |
| 550 |
class TestRacesDoNotCrash(TestBase): |
| 551 |
# Careful with these. Bigger numbers have a higher chance of catching bugs, |
| 552 |
# but you can also burn through a *ton* of type/dict/function versions: |
| 553 |
ITEMS = 1000 |
| 554 |
LOOPS = 4 |
| 555 |
WARMUPS = 2 |
| 556 |
WRITERS = 2 |
| 557 |
|
| 558 |
@disabling_optimizer |
| 559 |
def assert_races_do_not_crash( |
| 560 |
self, opname, get_items, read, write, *, check_items=False |
| 561 |
): |
| 562 |
# This might need a few dozen loops in some cases: |
| 563 |
for _ in range(self.LOOPS): |
| 564 |
items = get_items() |
| 565 |
# Reset: |
| 566 |
if check_items: |
| 567 |
for item in items: |
| 568 |
item.__code__ = item.__code__.replace() |
| 569 |
else: |
| 570 |
read.__code__ = read.__code__.replace() |
| 571 |
# Specialize: |
| 572 |
for _ in range(self.WARMUPS): |
| 573 |
read(items) |
| 574 |
if check_items: |
| 575 |
for item in items: |
| 576 |
self.assert_specialized(item, opname) |
| 577 |
else: |
| 578 |
self.assert_specialized(read, opname) |
| 579 |
# Create writers: |
| 580 |
writers = [] |
| 581 |
for _ in range(self.WRITERS): |
| 582 |
writer = threading.Thread(target=write, args=[items]) |
| 583 |
writers.append(writer) |
| 584 |
# Run: |
| 585 |
for writer in writers: |
| 586 |
writer.start() |
| 587 |
read(items) # BOOM! |
| 588 |
for writer in writers: |
| 589 |
writer.join() |
| 590 |
|
| 591 |
def test_binary_subscr_getitem(self): |
| 592 |
def get_items(): |
| 593 |
class C: |
| 594 |
__getitem__ = lambda self, item: None |
| 595 |
|
| 596 |
items = [] |
| 597 |
for _ in range(self.ITEMS): |
| 598 |
item = C() |
| 599 |
items.append(item) |
| 600 |
return items |
| 601 |
|
| 602 |
def read(items): |
| 603 |
for item in items: |
| 604 |
try: |
| 605 |
item[None] |
| 606 |
except TypeError: |
| 607 |
pass |
| 608 |
|
| 609 |
def write(items): |
| 610 |
for item in items: |
| 611 |
try: |
| 612 |
del item.__getitem__ |
| 613 |
except AttributeError: |
| 614 |
pass |
| 615 |
type(item).__getitem__ = lambda self, item: None |
| 616 |
|
| 617 |
opname = "BINARY_SUBSCR_GETITEM" |
| 618 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 619 |
|
| 620 |
def test_binary_subscr_list_int(self): |
| 621 |
def get_items(): |
| 622 |
items = [] |
| 623 |
for _ in range(self.ITEMS): |
| 624 |
item = [None] |
| 625 |
items.append(item) |
| 626 |
return items |
| 627 |
|
| 628 |
def read(items): |
| 629 |
for item in items: |
| 630 |
try: |
| 631 |
item[0] |
| 632 |
except IndexError: |
| 633 |
pass |
| 634 |
|
| 635 |
def write(items): |
| 636 |
for item in items: |
| 637 |
item.clear() |
| 638 |
item.append(None) |
| 639 |
|
| 640 |
opname = "BINARY_SUBSCR_LIST_INT" |
| 641 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 642 |
|
| 643 |
def test_for_iter_gen(self): |
| 644 |
def get_items(): |
| 645 |
def g(): |
| 646 |
yield |
| 647 |
yield |
| 648 |
|
| 649 |
items = [] |
| 650 |
for _ in range(self.ITEMS): |
| 651 |
item = g() |
| 652 |
items.append(item) |
| 653 |
return items |
| 654 |
|
| 655 |
def read(items): |
| 656 |
for item in items: |
| 657 |
try: |
| 658 |
for _ in item: |
| 659 |
break |
| 660 |
except ValueError: |
| 661 |
pass |
| 662 |
|
| 663 |
def write(items): |
| 664 |
for item in items: |
| 665 |
try: |
| 666 |
for _ in item: |
| 667 |
break |
| 668 |
except ValueError: |
| 669 |
pass |
| 670 |
|
| 671 |
opname = "FOR_ITER_GEN" |
| 672 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 673 |
|
| 674 |
def test_for_iter_list(self): |
| 675 |
def get_items(): |
| 676 |
items = [] |
| 677 |
for _ in range(self.ITEMS): |
| 678 |
item = [None] |
| 679 |
items.append(item) |
| 680 |
return items |
| 681 |
|
| 682 |
def read(items): |
| 683 |
for item in items: |
| 684 |
for item in item: |
| 685 |
break |
| 686 |
|
| 687 |
def write(items): |
| 688 |
for item in items: |
| 689 |
item.clear() |
| 690 |
item.append(None) |
| 691 |
|
| 692 |
opname = "FOR_ITER_LIST" |
| 693 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 694 |
|
| 695 |
def test_load_attr_class(self): |
| 696 |
def get_items(): |
| 697 |
class C: |
| 698 |
a = object() |
| 699 |
|
| 700 |
items = [] |
| 701 |
for _ in range(self.ITEMS): |
| 702 |
item = C |
| 703 |
items.append(item) |
| 704 |
return items |
| 705 |
|
| 706 |
def read(items): |
| 707 |
for item in items: |
| 708 |
try: |
| 709 |
item.a |
| 710 |
except AttributeError: |
| 711 |
pass |
| 712 |
|
| 713 |
def write(items): |
| 714 |
for item in items: |
| 715 |
try: |
| 716 |
del item.a |
| 717 |
except AttributeError: |
| 718 |
pass |
| 719 |
item.a = object() |
| 720 |
|
| 721 |
opname = "LOAD_ATTR_CLASS" |
| 722 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 723 |
|
| 724 |
def test_load_attr_getattribute_overridden(self): |
| 725 |
def get_items(): |
| 726 |
class C: |
| 727 |
__getattribute__ = lambda self, name: None |
| 728 |
|
| 729 |
items = [] |
| 730 |
for _ in range(self.ITEMS): |
| 731 |
item = C() |
| 732 |
items.append(item) |
| 733 |
return items |
| 734 |
|
| 735 |
def read(items): |
| 736 |
for item in items: |
| 737 |
try: |
| 738 |
item.a |
| 739 |
except AttributeError: |
| 740 |
pass |
| 741 |
|
| 742 |
def write(items): |
| 743 |
for item in items: |
| 744 |
try: |
| 745 |
del item.__getattribute__ |
| 746 |
except AttributeError: |
| 747 |
pass |
| 748 |
type(item).__getattribute__ = lambda self, name: None |
| 749 |
|
| 750 |
opname = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN" |
| 751 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 752 |
|
| 753 |
def test_load_attr_instance_value(self): |
| 754 |
def get_items(): |
| 755 |
class C: |
| 756 |
pass |
| 757 |
|
| 758 |
items = [] |
| 759 |
for _ in range(self.ITEMS): |
| 760 |
item = C() |
| 761 |
item.a = None |
| 762 |
items.append(item) |
| 763 |
return items |
| 764 |
|
| 765 |
def read(items): |
| 766 |
for item in items: |
| 767 |
item.a |
| 768 |
|
| 769 |
def write(items): |
| 770 |
for item in items: |
| 771 |
item.__dict__[None] = None |
| 772 |
|
| 773 |
opname = "LOAD_ATTR_INSTANCE_VALUE" |
| 774 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 775 |
|
| 776 |
def test_load_attr_method_lazy_dict(self): |
| 777 |
def get_items(): |
| 778 |
class C(Exception): |
| 779 |
m = lambda self: None |
| 780 |
|
| 781 |
items = [] |
| 782 |
for _ in range(self.ITEMS): |
| 783 |
item = C() |
| 784 |
items.append(item) |
| 785 |
return items |
| 786 |
|
| 787 |
def read(items): |
| 788 |
for item in items: |
| 789 |
try: |
| 790 |
item.m() |
| 791 |
except AttributeError: |
| 792 |
pass |
| 793 |
|
| 794 |
def write(items): |
| 795 |
for item in items: |
| 796 |
try: |
| 797 |
del item.m |
| 798 |
except AttributeError: |
| 799 |
pass |
| 800 |
type(item).m = lambda self: None |
| 801 |
|
| 802 |
opname = "LOAD_ATTR_METHOD_LAZY_DICT" |
| 803 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 804 |
|
| 805 |
def test_load_attr_method_no_dict(self): |
| 806 |
def get_items(): |
| 807 |
class C: |
| 808 |
__slots__ = () |
| 809 |
m = lambda self: None |
| 810 |
|
| 811 |
items = [] |
| 812 |
for _ in range(self.ITEMS): |
| 813 |
item = C() |
| 814 |
items.append(item) |
| 815 |
return items |
| 816 |
|
| 817 |
def read(items): |
| 818 |
for item in items: |
| 819 |
try: |
| 820 |
item.m() |
| 821 |
except AttributeError: |
| 822 |
pass |
| 823 |
|
| 824 |
def write(items): |
| 825 |
for item in items: |
| 826 |
try: |
| 827 |
del item.m |
| 828 |
except AttributeError: |
| 829 |
pass |
| 830 |
type(item).m = lambda self: None |
| 831 |
|
| 832 |
opname = "LOAD_ATTR_METHOD_NO_DICT" |
| 833 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 834 |
|
| 835 |
def test_load_attr_method_with_values(self): |
| 836 |
def get_items(): |
| 837 |
class C: |
| 838 |
m = lambda self: None |
| 839 |
|
| 840 |
items = [] |
| 841 |
for _ in range(self.ITEMS): |
| 842 |
item = C() |
| 843 |
items.append(item) |
| 844 |
return items |
| 845 |
|
| 846 |
def read(items): |
| 847 |
for item in items: |
| 848 |
try: |
| 849 |
item.m() |
| 850 |
except AttributeError: |
| 851 |
pass |
| 852 |
|
| 853 |
def write(items): |
| 854 |
for item in items: |
| 855 |
try: |
| 856 |
del item.m |
| 857 |
except AttributeError: |
| 858 |
pass |
| 859 |
type(item).m = lambda self: None |
| 860 |
|
| 861 |
opname = "LOAD_ATTR_METHOD_WITH_VALUES" |
| 862 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 863 |
|
| 864 |
def test_load_attr_module(self): |
| 865 |
def get_items(): |
| 866 |
items = [] |
| 867 |
for _ in range(self.ITEMS): |
| 868 |
item = types.ModuleType("<item>") |
| 869 |
items.append(item) |
| 870 |
return items |
| 871 |
|
| 872 |
def read(items): |
| 873 |
for item in items: |
| 874 |
try: |
| 875 |
item.__name__ |
| 876 |
except AttributeError: |
| 877 |
pass |
| 878 |
|
| 879 |
def write(items): |
| 880 |
for item in items: |
| 881 |
d = item.__dict__.copy() |
| 882 |
item.__dict__.clear() |
| 883 |
item.__dict__.update(d) |
| 884 |
|
| 885 |
opname = "LOAD_ATTR_MODULE" |
| 886 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 887 |
|
| 888 |
def test_load_attr_property(self): |
| 889 |
def get_items(): |
| 890 |
class C: |
| 891 |
a = property(lambda self: None) |
| 892 |
|
| 893 |
items = [] |
| 894 |
for _ in range(self.ITEMS): |
| 895 |
item = C() |
| 896 |
items.append(item) |
| 897 |
return items |
| 898 |
|
| 899 |
def read(items): |
| 900 |
for item in items: |
| 901 |
try: |
| 902 |
item.a |
| 903 |
except AttributeError: |
| 904 |
pass |
| 905 |
|
| 906 |
def write(items): |
| 907 |
for item in items: |
| 908 |
try: |
| 909 |
del type(item).a |
| 910 |
except AttributeError: |
| 911 |
pass |
| 912 |
type(item).a = property(lambda self: None) |
| 913 |
|
| 914 |
opname = "LOAD_ATTR_PROPERTY" |
| 915 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 916 |
|
| 917 |
def test_load_attr_with_hint(self): |
| 918 |
def get_items(): |
| 919 |
class C: |
| 920 |
pass |
| 921 |
|
| 922 |
items = [] |
| 923 |
for _ in range(self.ITEMS): |
| 924 |
item = C() |
| 925 |
item.a = None |
| 926 |
# Resize into a combined unicode dict: |
| 927 |
for i in range(29): |
| 928 |
setattr(item, f"_{i}", None) |
| 929 |
items.append(item) |
| 930 |
return items |
| 931 |
|
| 932 |
def read(items): |
| 933 |
for item in items: |
| 934 |
item.a |
| 935 |
|
| 936 |
def write(items): |
| 937 |
for item in items: |
| 938 |
item.__dict__[None] = None |
| 939 |
|
| 940 |
opname = "LOAD_ATTR_WITH_HINT" |
| 941 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 942 |
|
| 943 |
def test_load_global_module(self): |
| 944 |
def get_items(): |
| 945 |
items = [] |
| 946 |
for _ in range(self.ITEMS): |
| 947 |
item = eval("lambda: x", {"x": None}) |
| 948 |
items.append(item) |
| 949 |
return items |
| 950 |
|
| 951 |
def read(items): |
| 952 |
for item in items: |
| 953 |
item() |
| 954 |
|
| 955 |
def write(items): |
| 956 |
for item in items: |
| 957 |
item.__globals__[None] = None |
| 958 |
|
| 959 |
opname = "LOAD_GLOBAL_MODULE" |
| 960 |
self.assert_races_do_not_crash( |
| 961 |
opname, get_items, read, write, check_items=True |
| 962 |
) |
| 963 |
|
| 964 |
def test_store_attr_instance_value(self): |
| 965 |
def get_items(): |
| 966 |
class C: |
| 967 |
pass |
| 968 |
|
| 969 |
items = [] |
| 970 |
for _ in range(self.ITEMS): |
| 971 |
item = C() |
| 972 |
items.append(item) |
| 973 |
return items |
| 974 |
|
| 975 |
def read(items): |
| 976 |
for item in items: |
| 977 |
item.a = None |
| 978 |
|
| 979 |
def write(items): |
| 980 |
for item in items: |
| 981 |
item.__dict__[None] = None |
| 982 |
|
| 983 |
opname = "STORE_ATTR_INSTANCE_VALUE" |
| 984 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 985 |
|
| 986 |
def test_store_attr_with_hint(self): |
| 987 |
def get_items(): |
| 988 |
class C: |
| 989 |
pass |
| 990 |
|
| 991 |
items = [] |
| 992 |
for _ in range(self.ITEMS): |
| 993 |
item = C() |
| 994 |
# Resize into a combined unicode dict: |
| 995 |
for i in range(29): |
| 996 |
setattr(item, f"_{i}", None) |
| 997 |
items.append(item) |
| 998 |
return items |
| 999 |
|
| 1000 |
def read(items): |
| 1001 |
for item in items: |
| 1002 |
item.a = None |
| 1003 |
|
| 1004 |
def write(items): |
| 1005 |
for item in items: |
| 1006 |
item.__dict__[None] = None |
| 1007 |
|
| 1008 |
opname = "STORE_ATTR_WITH_HINT" |
| 1009 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1010 |
|
| 1011 |
def test_store_subscr_list_int(self): |
| 1012 |
def get_items(): |
| 1013 |
items = [] |
| 1014 |
for _ in range(self.ITEMS): |
| 1015 |
item = [None] |
| 1016 |
items.append(item) |
| 1017 |
return items |
| 1018 |
|
| 1019 |
def read(items): |
| 1020 |
for item in items: |
| 1021 |
try: |
| 1022 |
item[0] = None |
| 1023 |
except IndexError: |
| 1024 |
pass |
| 1025 |
|
| 1026 |
def write(items): |
| 1027 |
for item in items: |
| 1028 |
item.clear() |
| 1029 |
item.append(None) |
| 1030 |
|
| 1031 |
opname = "STORE_SUBSCR_LIST_INT" |
| 1032 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1033 |
|
| 1034 |
def test_unpack_sequence_list(self): |
| 1035 |
def get_items(): |
| 1036 |
items = [] |
| 1037 |
for _ in range(self.ITEMS): |
| 1038 |
item = [None] |
| 1039 |
items.append(item) |
| 1040 |
return items |
| 1041 |
|
| 1042 |
def read(items): |
| 1043 |
for item in items: |
| 1044 |
try: |
| 1045 |
[_] = item |
| 1046 |
except ValueError: |
| 1047 |
pass |
| 1048 |
|
| 1049 |
def write(items): |
| 1050 |
for item in items: |
| 1051 |
item.clear() |
| 1052 |
item.append(None) |
| 1053 |
|
| 1054 |
opname = "UNPACK_SEQUENCE_LIST" |
| 1055 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1056 |
|
| 1057 |
class C: |
| 1058 |
pass |
| 1059 |
|
| 1060 |
@requires_specialization |
| 1061 |
class TestInstanceDict(unittest.TestCase): |
| 1062 |
|
| 1063 |
def setUp(self): |
| 1064 |
c = C() |
| 1065 |
c.a, c.b, c.c = 0,0,0 |
| 1066 |
|
| 1067 |
def test_values_on_instance(self): |
| 1068 |
c = C() |
| 1069 |
c.a = 1 |
| 1070 |
C().b = 2 |
| 1071 |
c.c = 3 |
| 1072 |
self.assertEqual( |
| 1073 |
_testinternalcapi.get_object_dict_values(c), |
| 1074 |
(1, '<NULL>', 3) |
| 1075 |
) |
| 1076 |
|
| 1077 |
def test_dict_materialization(self): |
| 1078 |
c = C() |
| 1079 |
c.a = 1 |
| 1080 |
c.b = 2 |
| 1081 |
c.__dict__ |
| 1082 |
self.assertEqual(c.__dict__, {"a":1, "b": 2}) |
| 1083 |
|
| 1084 |
def test_dict_dematerialization(self): |
| 1085 |
c = C() |
| 1086 |
c.a = 1 |
| 1087 |
c.b = 2 |
| 1088 |
c.__dict__ |
| 1089 |
for _ in range(100): |
| 1090 |
c.a |
| 1091 |
self.assertEqual( |
| 1092 |
_testinternalcapi.get_object_dict_values(c), |
| 1093 |
(1, 2, '<NULL>') |
| 1094 |
) |
| 1095 |
|
| 1096 |
def test_dict_dematerialization_multiple_refs(self): |
| 1097 |
c = C() |
| 1098 |
c.a = 1 |
| 1099 |
c.b = 2 |
| 1100 |
d = c.__dict__ |
| 1101 |
for _ in range(100): |
| 1102 |
c.a |
| 1103 |
self.assertIs(c.__dict__, d) |
| 1104 |
|
| 1105 |
def test_dict_dematerialization_copy(self): |
| 1106 |
c = C() |
| 1107 |
c.a = 1 |
| 1108 |
c.b = 2 |
| 1109 |
c2 = copy.copy(c) |
| 1110 |
for _ in range(100): |
| 1111 |
c.a |
| 1112 |
c2.a |
| 1113 |
self.assertEqual( |
| 1114 |
_testinternalcapi.get_object_dict_values(c), |
| 1115 |
(1, 2, '<NULL>') |
| 1116 |
) |
| 1117 |
self.assertEqual( |
| 1118 |
_testinternalcapi.get_object_dict_values(c2), |
| 1119 |
(1, 2, '<NULL>') |
| 1120 |
) |
| 1121 |
c3 = copy.deepcopy(c) |
| 1122 |
for _ in range(100): |
| 1123 |
c.a |
| 1124 |
c3.a |
| 1125 |
self.assertEqual( |
| 1126 |
_testinternalcapi.get_object_dict_values(c), |
| 1127 |
(1, 2, '<NULL>') |
| 1128 |
) |
| 1129 |
#NOTE -- c3.__dict__ does not de-materialize |
| 1130 |
|
| 1131 |
def test_dict_dematerialization_pickle(self): |
| 1132 |
c = C() |
| 1133 |
c.a = 1 |
| 1134 |
c.b = 2 |
| 1135 |
c2 = pickle.loads(pickle.dumps(c)) |
| 1136 |
for _ in range(100): |
| 1137 |
c.a |
| 1138 |
c2.a |
| 1139 |
self.assertEqual( |
| 1140 |
_testinternalcapi.get_object_dict_values(c), |
| 1141 |
(1, 2, '<NULL>') |
| 1142 |
) |
| 1143 |
self.assertEqual( |
| 1144 |
_testinternalcapi.get_object_dict_values(c2), |
| 1145 |
(1, 2, '<NULL>') |
| 1146 |
) |
| 1147 |
|
| 1148 |
def test_dict_dematerialization_subclass(self): |
| 1149 |
class D(dict): pass |
| 1150 |
c = C() |
| 1151 |
c.a = 1 |
| 1152 |
c.b = 2 |
| 1153 |
c.__dict__ = D(c.__dict__) |
| 1154 |
for _ in range(100): |
| 1155 |
c.a |
| 1156 |
self.assertIs( |
| 1157 |
_testinternalcapi.get_object_dict_values(c), |
| 1158 |
None |
| 1159 |
) |
| 1160 |
self.assertEqual( |
| 1161 |
c.__dict__, |
| 1162 |
{'a':1, 'b':2} |
| 1163 |
) |
| 1164 |
|
| 1165 |
def test_store_attr_with_hint(self): |
| 1166 |
# gh-133441: Regression test for STORE_ATTR_WITH_HINT bytecode |
| 1167 |
class Node: |
| 1168 |
def __init__(self): |
| 1169 |
self.parents = {} |
| 1170 |
|
| 1171 |
def __setstate__(self, data_dict): |
| 1172 |
self.__dict__ = data_dict |
| 1173 |
self.parents = {} |
| 1174 |
|
| 1175 |
class Dict(dict): |
| 1176 |
pass |
| 1177 |
|
| 1178 |
obj = Node() |
| 1179 |
obj.__setstate__({'parents': {}}) |
| 1180 |
obj.__setstate__({'parents': {}}) |
| 1181 |
obj.__setstate__(Dict({'parents': {}})) |
| 1182 |
|
| 1183 |
|
| 1184 |
if __name__ == "__main__": |
| 1185 |
unittest.main() |