| 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, |
| 8 |
requires_specialization, requires_specialization_ft, |
| 9 |
cpython_only, requires_jit_disabled, reset_code) |
| 10 |
from test.support.import_helper import import_module |
| 11 |
|
| 12 |
# Skip this module on other interpreters, it is cpython specific: |
| 13 |
if check_impl_detail(cpython=False): |
| 14 |
raise unittest.SkipTest('implementation detail specific to cpython') |
| 15 |
|
| 16 |
_testinternalcapi = import_module("_testinternalcapi") |
| 17 |
|
| 18 |
|
| 19 |
def have_dict_key_versions(): |
| 20 |
# max version value that can be stored in the load global cache. This is |
| 21 |
# determined by the type of module_keys_version and builtin_keys_version |
| 22 |
# in _PyLoadGlobalCache, uint16_t. |
| 23 |
max_version = 1<<16 |
| 24 |
# use a wide safety margin (use only half of what's available) |
| 25 |
limit = max_version // 2 |
| 26 |
return _testinternalcapi.get_next_dict_keys_version() < limit |
| 27 |
|
| 28 |
|
| 29 |
class TestBase(unittest.TestCase): |
| 30 |
def assert_specialized(self, f, opname): |
| 31 |
instructions = dis.get_instructions(f, adaptive=True) |
| 32 |
opnames = {instruction.opname for instruction in instructions} |
| 33 |
self.assertIn(opname, opnames) |
| 34 |
|
| 35 |
def assert_no_opcode(self, f, opname): |
| 36 |
instructions = dis.get_instructions(f, adaptive=True) |
| 37 |
opnames = {instruction.opname for instruction in instructions} |
| 38 |
self.assertNotIn(opname, opnames) |
| 39 |
|
| 40 |
|
| 41 |
class TestLoadSuperAttrCache(unittest.TestCase): |
| 42 |
def test_descriptor_not_double_executed_on_spec_fail(self): |
| 43 |
calls = [] |
| 44 |
class Descriptor: |
| 45 |
def __get__(self, instance, owner): |
| 46 |
calls.append((instance, owner)) |
| 47 |
return lambda: 1 |
| 48 |
|
| 49 |
class C: |
| 50 |
d = Descriptor() |
| 51 |
|
| 52 |
class D(C): |
| 53 |
def f(self): |
| 54 |
return super().d() |
| 55 |
|
| 56 |
d = D() |
| 57 |
|
| 58 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD - 1): |
| 59 |
self.assertEqual(d.f(), 1) # warmup |
| 60 |
calls.clear() |
| 61 |
self.assertEqual(d.f(), 1) # try to specialize |
| 62 |
self.assertEqual(calls, [(d, D)]) |
| 63 |
|
| 64 |
|
| 65 |
class TestLoadAttrCache(unittest.TestCase): |
| 66 |
def test_descriptor_added_after_optimization(self): |
| 67 |
class Descriptor: |
| 68 |
pass |
| 69 |
|
| 70 |
class C: |
| 71 |
def __init__(self): |
| 72 |
self.x = 1 |
| 73 |
x = Descriptor() |
| 74 |
|
| 75 |
def f(o): |
| 76 |
return o.x |
| 77 |
|
| 78 |
o = C() |
| 79 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 80 |
assert f(o) == 1 |
| 81 |
|
| 82 |
Descriptor.__get__ = lambda self, instance, value: 2 |
| 83 |
Descriptor.__set__ = lambda *args: None |
| 84 |
|
| 85 |
self.assertEqual(f(o), 2) |
| 86 |
|
| 87 |
def test_metaclass_descriptor_added_after_optimization(self): |
| 88 |
class Descriptor: |
| 89 |
pass |
| 90 |
|
| 91 |
class Metaclass(type): |
| 92 |
attribute = Descriptor() |
| 93 |
|
| 94 |
class Class(metaclass=Metaclass): |
| 95 |
attribute = True |
| 96 |
|
| 97 |
def __get__(self, instance, owner): |
| 98 |
return False |
| 99 |
|
| 100 |
def __set__(self, instance, value): |
| 101 |
return None |
| 102 |
|
| 103 |
def f(): |
| 104 |
return Class.attribute |
| 105 |
|
| 106 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 107 |
self.assertTrue(f()) |
| 108 |
|
| 109 |
Descriptor.__get__ = __get__ |
| 110 |
Descriptor.__set__ = __set__ |
| 111 |
|
| 112 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 113 |
self.assertFalse(f()) |
| 114 |
|
| 115 |
def test_metaclass_descriptor_shadows_class_attribute(self): |
| 116 |
class Metaclass(type): |
| 117 |
@property |
| 118 |
def attribute(self): |
| 119 |
return True |
| 120 |
|
| 121 |
class Class(metaclass=Metaclass): |
| 122 |
attribute = False |
| 123 |
|
| 124 |
def f(): |
| 125 |
return Class.attribute |
| 126 |
|
| 127 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 128 |
self.assertTrue(f()) |
| 129 |
|
| 130 |
def test_metaclass_set_descriptor_after_optimization(self): |
| 131 |
class Metaclass(type): |
| 132 |
pass |
| 133 |
|
| 134 |
class Class(metaclass=Metaclass): |
| 135 |
attribute = True |
| 136 |
|
| 137 |
@property |
| 138 |
def attribute(self): |
| 139 |
return False |
| 140 |
|
| 141 |
def f(): |
| 142 |
return Class.attribute |
| 143 |
|
| 144 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 145 |
self.assertTrue(f()) |
| 146 |
|
| 147 |
Metaclass.attribute = attribute |
| 148 |
|
| 149 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 150 |
self.assertFalse(f()) |
| 151 |
|
| 152 |
def test_metaclass_del_descriptor_after_optimization(self): |
| 153 |
class Metaclass(type): |
| 154 |
@property |
| 155 |
def attribute(self): |
| 156 |
return True |
| 157 |
|
| 158 |
class Class(metaclass=Metaclass): |
| 159 |
attribute = False |
| 160 |
|
| 161 |
def f(): |
| 162 |
return Class.attribute |
| 163 |
|
| 164 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 165 |
self.assertTrue(f()) |
| 166 |
|
| 167 |
del Metaclass.attribute |
| 168 |
|
| 169 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 170 |
self.assertFalse(f()) |
| 171 |
|
| 172 |
def test_type_descriptor_shadows_attribute_method(self): |
| 173 |
class Class: |
| 174 |
mro = None |
| 175 |
|
| 176 |
def f(): |
| 177 |
return Class.mro |
| 178 |
|
| 179 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 180 |
self.assertIsNone(f()) |
| 181 |
|
| 182 |
def test_type_descriptor_shadows_attribute_member(self): |
| 183 |
class Class: |
| 184 |
__base__ = None |
| 185 |
|
| 186 |
def f(): |
| 187 |
return Class.__base__ |
| 188 |
|
| 189 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 190 |
self.assertIs(f(), object) |
| 191 |
|
| 192 |
def test_type_descriptor_shadows_attribute_getset(self): |
| 193 |
class Class: |
| 194 |
__name__ = "Spam" |
| 195 |
|
| 196 |
def f(): |
| 197 |
return Class.__name__ |
| 198 |
|
| 199 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 200 |
self.assertEqual(f(), "Class") |
| 201 |
|
| 202 |
def test_metaclass_getattribute(self): |
| 203 |
class Metaclass(type): |
| 204 |
def __getattribute__(self, name): |
| 205 |
return True |
| 206 |
|
| 207 |
class Class(metaclass=Metaclass): |
| 208 |
attribute = False |
| 209 |
|
| 210 |
def f(): |
| 211 |
return Class.attribute |
| 212 |
|
| 213 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 214 |
self.assertTrue(f()) |
| 215 |
|
| 216 |
def test_metaclass_swap(self): |
| 217 |
class OldMetaclass(type): |
| 218 |
@property |
| 219 |
def attribute(self): |
| 220 |
return True |
| 221 |
|
| 222 |
class NewMetaclass(type): |
| 223 |
@property |
| 224 |
def attribute(self): |
| 225 |
return False |
| 226 |
|
| 227 |
class Class(metaclass=OldMetaclass): |
| 228 |
pass |
| 229 |
|
| 230 |
def f(): |
| 231 |
return Class.attribute |
| 232 |
|
| 233 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 234 |
self.assertTrue(f()) |
| 235 |
|
| 236 |
Class.__class__ = NewMetaclass |
| 237 |
|
| 238 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 239 |
self.assertFalse(f()) |
| 240 |
|
| 241 |
def test_load_shadowing_slot_should_raise_type_error(self): |
| 242 |
class Class: |
| 243 |
__slots__ = ("slot",) |
| 244 |
|
| 245 |
class Sneaky: |
| 246 |
__slots__ = ("shadowed",) |
| 247 |
shadowing = Class.slot |
| 248 |
|
| 249 |
def f(o): |
| 250 |
o.shadowing |
| 251 |
|
| 252 |
o = Sneaky() |
| 253 |
o.shadowed = 42 |
| 254 |
|
| 255 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 256 |
with self.assertRaises(TypeError): |
| 257 |
f(o) |
| 258 |
|
| 259 |
def test_store_shadowing_slot_should_raise_type_error(self): |
| 260 |
class Class: |
| 261 |
__slots__ = ("slot",) |
| 262 |
|
| 263 |
class Sneaky: |
| 264 |
__slots__ = ("shadowed",) |
| 265 |
shadowing = Class.slot |
| 266 |
|
| 267 |
def f(o): |
| 268 |
o.shadowing = 42 |
| 269 |
|
| 270 |
o = Sneaky() |
| 271 |
|
| 272 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 273 |
with self.assertRaises(TypeError): |
| 274 |
f(o) |
| 275 |
|
| 276 |
def test_load_borrowed_slot_should_not_crash(self): |
| 277 |
class Class: |
| 278 |
__slots__ = ("slot",) |
| 279 |
|
| 280 |
class Sneaky: |
| 281 |
borrowed = Class.slot |
| 282 |
|
| 283 |
def f(o): |
| 284 |
o.borrowed |
| 285 |
|
| 286 |
o = Sneaky() |
| 287 |
|
| 288 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 289 |
with self.assertRaises(TypeError): |
| 290 |
f(o) |
| 291 |
|
| 292 |
def test_store_borrowed_slot_should_not_crash(self): |
| 293 |
class Class: |
| 294 |
__slots__ = ("slot",) |
| 295 |
|
| 296 |
class Sneaky: |
| 297 |
borrowed = Class.slot |
| 298 |
|
| 299 |
def f(o): |
| 300 |
o.borrowed = 42 |
| 301 |
|
| 302 |
o = Sneaky() |
| 303 |
|
| 304 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 305 |
with self.assertRaises(TypeError): |
| 306 |
f(o) |
| 307 |
|
| 308 |
|
| 309 |
class TestLoadMethodCache(unittest.TestCase): |
| 310 |
def test_descriptor_added_after_optimization(self): |
| 311 |
class Descriptor: |
| 312 |
pass |
| 313 |
|
| 314 |
class Class: |
| 315 |
attribute = Descriptor() |
| 316 |
|
| 317 |
def __get__(self, instance, owner): |
| 318 |
return lambda: False |
| 319 |
|
| 320 |
def __set__(self, instance, value): |
| 321 |
return None |
| 322 |
|
| 323 |
def attribute(): |
| 324 |
return True |
| 325 |
|
| 326 |
instance = Class() |
| 327 |
instance.attribute = attribute |
| 328 |
|
| 329 |
def f(): |
| 330 |
return instance.attribute() |
| 331 |
|
| 332 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 333 |
self.assertTrue(f()) |
| 334 |
|
| 335 |
Descriptor.__get__ = __get__ |
| 336 |
Descriptor.__set__ = __set__ |
| 337 |
|
| 338 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 339 |
self.assertFalse(f()) |
| 340 |
|
| 341 |
def test_metaclass_descriptor_added_after_optimization(self): |
| 342 |
class Descriptor: |
| 343 |
pass |
| 344 |
|
| 345 |
class Metaclass(type): |
| 346 |
attribute = Descriptor() |
| 347 |
|
| 348 |
class Class(metaclass=Metaclass): |
| 349 |
def attribute(): |
| 350 |
return True |
| 351 |
|
| 352 |
def __get__(self, instance, owner): |
| 353 |
return lambda: False |
| 354 |
|
| 355 |
def __set__(self, instance, value): |
| 356 |
return None |
| 357 |
|
| 358 |
def f(): |
| 359 |
return Class.attribute() |
| 360 |
|
| 361 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 362 |
self.assertTrue(f()) |
| 363 |
|
| 364 |
Descriptor.__get__ = __get__ |
| 365 |
Descriptor.__set__ = __set__ |
| 366 |
|
| 367 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 368 |
self.assertFalse(f()) |
| 369 |
|
| 370 |
def test_metaclass_descriptor_shadows_class_attribute(self): |
| 371 |
class Metaclass(type): |
| 372 |
@property |
| 373 |
def attribute(self): |
| 374 |
return lambda: True |
| 375 |
|
| 376 |
class Class(metaclass=Metaclass): |
| 377 |
def attribute(): |
| 378 |
return False |
| 379 |
|
| 380 |
def f(): |
| 381 |
return Class.attribute() |
| 382 |
|
| 383 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 384 |
self.assertTrue(f()) |
| 385 |
|
| 386 |
def test_metaclass_set_descriptor_after_optimization(self): |
| 387 |
class Metaclass(type): |
| 388 |
pass |
| 389 |
|
| 390 |
class Class(metaclass=Metaclass): |
| 391 |
def attribute(): |
| 392 |
return True |
| 393 |
|
| 394 |
@property |
| 395 |
def attribute(self): |
| 396 |
return lambda: False |
| 397 |
|
| 398 |
def f(): |
| 399 |
return Class.attribute() |
| 400 |
|
| 401 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 402 |
self.assertTrue(f()) |
| 403 |
|
| 404 |
Metaclass.attribute = attribute |
| 405 |
|
| 406 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 407 |
self.assertFalse(f()) |
| 408 |
|
| 409 |
def test_metaclass_del_descriptor_after_optimization(self): |
| 410 |
class Metaclass(type): |
| 411 |
@property |
| 412 |
def attribute(self): |
| 413 |
return lambda: True |
| 414 |
|
| 415 |
class Class(metaclass=Metaclass): |
| 416 |
def attribute(): |
| 417 |
return False |
| 418 |
|
| 419 |
def f(): |
| 420 |
return Class.attribute() |
| 421 |
|
| 422 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 423 |
self.assertTrue(f()) |
| 424 |
|
| 425 |
del Metaclass.attribute |
| 426 |
|
| 427 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 428 |
self.assertFalse(f()) |
| 429 |
|
| 430 |
def test_type_descriptor_shadows_attribute_method(self): |
| 431 |
class Class: |
| 432 |
def mro(): |
| 433 |
return ["Spam", "eggs"] |
| 434 |
|
| 435 |
def f(): |
| 436 |
return Class.mro() |
| 437 |
|
| 438 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 439 |
self.assertEqual(f(), ["Spam", "eggs"]) |
| 440 |
|
| 441 |
def test_type_descriptor_shadows_attribute_member(self): |
| 442 |
class Class: |
| 443 |
def __base__(): |
| 444 |
return "Spam" |
| 445 |
|
| 446 |
def f(): |
| 447 |
return Class.__base__() |
| 448 |
|
| 449 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 450 |
self.assertNotEqual(f(), "Spam") |
| 451 |
|
| 452 |
def test_metaclass_getattribute(self): |
| 453 |
class Metaclass(type): |
| 454 |
def __getattribute__(self, name): |
| 455 |
return lambda: True |
| 456 |
|
| 457 |
class Class(metaclass=Metaclass): |
| 458 |
def attribute(): |
| 459 |
return False |
| 460 |
|
| 461 |
def f(): |
| 462 |
return Class.attribute() |
| 463 |
|
| 464 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 465 |
self.assertTrue(f()) |
| 466 |
|
| 467 |
def test_metaclass_swap(self): |
| 468 |
class OldMetaclass(type): |
| 469 |
@property |
| 470 |
def attribute(self): |
| 471 |
return lambda: True |
| 472 |
|
| 473 |
class NewMetaclass(type): |
| 474 |
@property |
| 475 |
def attribute(self): |
| 476 |
return lambda: False |
| 477 |
|
| 478 |
class Class(metaclass=OldMetaclass): |
| 479 |
pass |
| 480 |
|
| 481 |
def f(): |
| 482 |
return Class.attribute() |
| 483 |
|
| 484 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 485 |
self.assertTrue(f()) |
| 486 |
|
| 487 |
Class.__class__ = NewMetaclass |
| 488 |
|
| 489 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 490 |
self.assertFalse(f()) |
| 491 |
|
| 492 |
|
| 493 |
class InitTakesArg: |
| 494 |
def __init__(self, arg): |
| 495 |
self.arg = arg |
| 496 |
|
| 497 |
|
| 498 |
class TestCallCache(TestBase): |
| 499 |
def test_too_many_defaults_0(self): |
| 500 |
def f(): |
| 501 |
pass |
| 502 |
|
| 503 |
f.__defaults__ = (None,) |
| 504 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 505 |
f() |
| 506 |
|
| 507 |
def test_too_many_defaults_1(self): |
| 508 |
def f(x): |
| 509 |
pass |
| 510 |
|
| 511 |
f.__defaults__ = (None, None) |
| 512 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 513 |
f(None) |
| 514 |
f() |
| 515 |
|
| 516 |
def test_too_many_defaults_2(self): |
| 517 |
def f(x, y): |
| 518 |
pass |
| 519 |
|
| 520 |
f.__defaults__ = (None, None, None) |
| 521 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 522 |
f(None, None) |
| 523 |
f(None) |
| 524 |
f() |
| 525 |
|
| 526 |
@requires_jit_disabled |
| 527 |
@requires_specialization_ft |
| 528 |
def test_assign_init_code(self): |
| 529 |
class MyClass: |
| 530 |
def __init__(self): |
| 531 |
pass |
| 532 |
|
| 533 |
def instantiate(): |
| 534 |
return MyClass() |
| 535 |
|
| 536 |
# Trigger specialization |
| 537 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 538 |
instantiate() |
| 539 |
self.assert_specialized(instantiate, "CALL_ALLOC_AND_ENTER_INIT") |
| 540 |
|
| 541 |
def count_args(self, *args): |
| 542 |
self.num_args = len(args) |
| 543 |
|
| 544 |
# Set MyClass.__init__.__code__ to a code object that uses different |
| 545 |
# args |
| 546 |
MyClass.__init__.__code__ = count_args.__code__ |
| 547 |
instantiate() |
| 548 |
|
| 549 |
@requires_jit_disabled |
| 550 |
@requires_specialization_ft |
| 551 |
def test_push_init_frame_fails(self): |
| 552 |
def instantiate(): |
| 553 |
return InitTakesArg() |
| 554 |
|
| 555 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 556 |
with self.assertRaises(TypeError): |
| 557 |
instantiate() |
| 558 |
self.assert_specialized(instantiate, "CALL_ALLOC_AND_ENTER_INIT") |
| 559 |
|
| 560 |
with self.assertRaises(TypeError): |
| 561 |
instantiate() |
| 562 |
|
| 563 |
def test_recursion_check_for_general_calls(self): |
| 564 |
def test(default=None): |
| 565 |
return test() |
| 566 |
|
| 567 |
with self.assertRaises(RecursionError): |
| 568 |
test() |
| 569 |
|
| 570 |
def test_dont_specialize_custom_vectorcall(self): |
| 571 |
def f(): |
| 572 |
raise Exception("no way") |
| 573 |
|
| 574 |
_testinternalcapi.set_vectorcall_nop(f) |
| 575 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 576 |
f() |
| 577 |
|
| 578 |
|
| 579 |
def make_deferred_ref_count_obj(): |
| 580 |
"""Create an object that uses deferred reference counting. |
| 581 |
|
| 582 |
Only objects that use deferred refence counting may be stored in inline |
| 583 |
caches in free-threaded builds. This constructs a new class named Foo, |
| 584 |
which uses deferred reference counting. |
| 585 |
""" |
| 586 |
return type("Foo", (object,), {}) |
| 587 |
|
| 588 |
|
| 589 |
@threading_helper.requires_working_threading() |
| 590 |
class TestRacesDoNotCrash(TestBase): |
| 591 |
# Careful with these. Bigger numbers have a higher chance of catching bugs, |
| 592 |
# but you can also burn through a *ton* of type/dict/function versions: |
| 593 |
ITEMS = 1000 |
| 594 |
LOOPS = 4 |
| 595 |
WRITERS = 2 |
| 596 |
|
| 597 |
@requires_jit_disabled |
| 598 |
def assert_races_do_not_crash( |
| 599 |
self, opname, get_items, read, write, *, check_items=False |
| 600 |
): |
| 601 |
# This might need a few dozen loops in some cases: |
| 602 |
for _ in range(self.LOOPS): |
| 603 |
items = get_items() |
| 604 |
# Reset: |
| 605 |
if check_items: |
| 606 |
for item in items: |
| 607 |
reset_code(item) |
| 608 |
else: |
| 609 |
reset_code(read) |
| 610 |
# Specialize: |
| 611 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 612 |
read(items) |
| 613 |
if check_items: |
| 614 |
for item in items: |
| 615 |
self.assert_specialized(item, opname) |
| 616 |
else: |
| 617 |
self.assert_specialized(read, opname) |
| 618 |
# Create writers: |
| 619 |
writers = [] |
| 620 |
for _ in range(self.WRITERS): |
| 621 |
writer = threading.Thread(target=write, args=[items]) |
| 622 |
writers.append(writer) |
| 623 |
# Run: |
| 624 |
for writer in writers: |
| 625 |
writer.start() |
| 626 |
read(items) # BOOM! |
| 627 |
for writer in writers: |
| 628 |
writer.join() |
| 629 |
|
| 630 |
@requires_specialization_ft |
| 631 |
def test_binary_subscr_getitem(self): |
| 632 |
def get_items(): |
| 633 |
class C: |
| 634 |
__getitem__ = lambda self, item: None |
| 635 |
|
| 636 |
items = [] |
| 637 |
for _ in range(self.ITEMS): |
| 638 |
item = C() |
| 639 |
items.append(item) |
| 640 |
return items |
| 641 |
|
| 642 |
def read(items): |
| 643 |
for item in items: |
| 644 |
try: |
| 645 |
item[None] |
| 646 |
except TypeError: |
| 647 |
pass |
| 648 |
|
| 649 |
def write(items): |
| 650 |
for item in items: |
| 651 |
try: |
| 652 |
del item.__getitem__ |
| 653 |
except AttributeError: |
| 654 |
pass |
| 655 |
type(item).__getitem__ = lambda self, item: None |
| 656 |
|
| 657 |
opname = "BINARY_OP_SUBSCR_GETITEM" |
| 658 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 659 |
|
| 660 |
@requires_specialization_ft |
| 661 |
def test_binary_subscr_list_int(self): |
| 662 |
def get_items(): |
| 663 |
items = [] |
| 664 |
for _ in range(self.ITEMS): |
| 665 |
item = [None] |
| 666 |
items.append(item) |
| 667 |
return items |
| 668 |
|
| 669 |
def read(items): |
| 670 |
for item in items: |
| 671 |
try: |
| 672 |
item[0] |
| 673 |
except IndexError: |
| 674 |
pass |
| 675 |
|
| 676 |
def write(items): |
| 677 |
for item in items: |
| 678 |
item.clear() |
| 679 |
item.append(None) |
| 680 |
|
| 681 |
opname = "BINARY_OP_SUBSCR_LIST_INT" |
| 682 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 683 |
|
| 684 |
@requires_specialization |
| 685 |
def test_for_iter_gen(self): |
| 686 |
def get_items(): |
| 687 |
def g(): |
| 688 |
yield |
| 689 |
yield |
| 690 |
|
| 691 |
items = [] |
| 692 |
for _ in range(self.ITEMS): |
| 693 |
item = g() |
| 694 |
items.append(item) |
| 695 |
return items |
| 696 |
|
| 697 |
def read(items): |
| 698 |
for item in items: |
| 699 |
try: |
| 700 |
for _ in item: |
| 701 |
break |
| 702 |
except ValueError: |
| 703 |
pass |
| 704 |
|
| 705 |
def write(items): |
| 706 |
for item in items: |
| 707 |
try: |
| 708 |
for _ in item: |
| 709 |
break |
| 710 |
except ValueError: |
| 711 |
pass |
| 712 |
|
| 713 |
opname = "FOR_ITER_GEN" |
| 714 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 715 |
|
| 716 |
@requires_specialization |
| 717 |
def test_for_iter_list(self): |
| 718 |
def get_items(): |
| 719 |
items = [] |
| 720 |
for _ in range(self.ITEMS): |
| 721 |
item = [None] |
| 722 |
items.append(item) |
| 723 |
return items |
| 724 |
|
| 725 |
def read(items): |
| 726 |
for item in items: |
| 727 |
for item in item: |
| 728 |
break |
| 729 |
|
| 730 |
def write(items): |
| 731 |
for item in items: |
| 732 |
item.clear() |
| 733 |
item.append(None) |
| 734 |
|
| 735 |
opname = "FOR_ITER_LIST" |
| 736 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 737 |
|
| 738 |
@requires_specialization_ft |
| 739 |
def test_load_attr_class(self): |
| 740 |
def get_items(): |
| 741 |
class C: |
| 742 |
a = make_deferred_ref_count_obj() |
| 743 |
|
| 744 |
items = [] |
| 745 |
for _ in range(self.ITEMS): |
| 746 |
item = C |
| 747 |
items.append(item) |
| 748 |
return items |
| 749 |
|
| 750 |
def read(items): |
| 751 |
for item in items: |
| 752 |
try: |
| 753 |
item.a |
| 754 |
except AttributeError: |
| 755 |
pass |
| 756 |
|
| 757 |
def write(items): |
| 758 |
for item in items: |
| 759 |
try: |
| 760 |
del item.a |
| 761 |
except AttributeError: |
| 762 |
pass |
| 763 |
item.a = make_deferred_ref_count_obj() |
| 764 |
|
| 765 |
opname = "LOAD_ATTR_CLASS" |
| 766 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 767 |
|
| 768 |
@requires_specialization_ft |
| 769 |
def test_load_attr_class_with_metaclass_check(self): |
| 770 |
def get_items(): |
| 771 |
class Meta(type): |
| 772 |
pass |
| 773 |
|
| 774 |
class C(metaclass=Meta): |
| 775 |
a = make_deferred_ref_count_obj() |
| 776 |
|
| 777 |
items = [] |
| 778 |
for _ in range(self.ITEMS): |
| 779 |
item = C |
| 780 |
items.append(item) |
| 781 |
return items |
| 782 |
|
| 783 |
def read(items): |
| 784 |
for item in items: |
| 785 |
try: |
| 786 |
item.a |
| 787 |
except AttributeError: |
| 788 |
pass |
| 789 |
|
| 790 |
def write(items): |
| 791 |
for item in items: |
| 792 |
try: |
| 793 |
del item.a |
| 794 |
except AttributeError: |
| 795 |
pass |
| 796 |
item.a = make_deferred_ref_count_obj() |
| 797 |
|
| 798 |
opname = "LOAD_ATTR_CLASS_WITH_METACLASS_CHECK" |
| 799 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 800 |
|
| 801 |
@requires_specialization_ft |
| 802 |
def test_load_attr_getattribute_overridden(self): |
| 803 |
def get_items(): |
| 804 |
class C: |
| 805 |
__getattribute__ = lambda self, name: None |
| 806 |
|
| 807 |
items = [] |
| 808 |
for _ in range(self.ITEMS): |
| 809 |
item = C() |
| 810 |
items.append(item) |
| 811 |
return items |
| 812 |
|
| 813 |
def read(items): |
| 814 |
for item in items: |
| 815 |
try: |
| 816 |
item.a |
| 817 |
except AttributeError: |
| 818 |
pass |
| 819 |
|
| 820 |
def write(items): |
| 821 |
for item in items: |
| 822 |
try: |
| 823 |
del item.__getattribute__ |
| 824 |
except AttributeError: |
| 825 |
pass |
| 826 |
type(item).__getattribute__ = lambda self, name: None |
| 827 |
|
| 828 |
opname = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN" |
| 829 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 830 |
|
| 831 |
@requires_specialization_ft |
| 832 |
def test_load_attr_instance_value(self): |
| 833 |
def get_items(): |
| 834 |
class C: |
| 835 |
pass |
| 836 |
|
| 837 |
items = [] |
| 838 |
for _ in range(self.ITEMS): |
| 839 |
item = C() |
| 840 |
item.a = None |
| 841 |
items.append(item) |
| 842 |
return items |
| 843 |
|
| 844 |
def read(items): |
| 845 |
for item in items: |
| 846 |
item.a |
| 847 |
|
| 848 |
def write(items): |
| 849 |
for item in items: |
| 850 |
item.__dict__[None] = None |
| 851 |
|
| 852 |
opname = "LOAD_ATTR_INSTANCE_VALUE" |
| 853 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 854 |
|
| 855 |
@requires_specialization_ft |
| 856 |
def test_load_attr_method_lazy_dict(self): |
| 857 |
def get_items(): |
| 858 |
class C(Exception): |
| 859 |
m = lambda self: None |
| 860 |
|
| 861 |
items = [] |
| 862 |
for _ in range(self.ITEMS): |
| 863 |
item = C() |
| 864 |
items.append(item) |
| 865 |
return items |
| 866 |
|
| 867 |
def read(items): |
| 868 |
for item in items: |
| 869 |
try: |
| 870 |
item.m() |
| 871 |
except AttributeError: |
| 872 |
pass |
| 873 |
|
| 874 |
def write(items): |
| 875 |
for item in items: |
| 876 |
try: |
| 877 |
del item.m |
| 878 |
except AttributeError: |
| 879 |
pass |
| 880 |
type(item).m = lambda self: None |
| 881 |
|
| 882 |
opname = "LOAD_ATTR_METHOD_LAZY_DICT" |
| 883 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 884 |
|
| 885 |
@requires_specialization_ft |
| 886 |
def test_load_attr_method_no_dict(self): |
| 887 |
def get_items(): |
| 888 |
class C: |
| 889 |
__slots__ = () |
| 890 |
m = lambda self: None |
| 891 |
|
| 892 |
items = [] |
| 893 |
for _ in range(self.ITEMS): |
| 894 |
item = C() |
| 895 |
items.append(item) |
| 896 |
return items |
| 897 |
|
| 898 |
def read(items): |
| 899 |
for item in items: |
| 900 |
try: |
| 901 |
item.m() |
| 902 |
except AttributeError: |
| 903 |
pass |
| 904 |
|
| 905 |
def write(items): |
| 906 |
for item in items: |
| 907 |
try: |
| 908 |
del item.m |
| 909 |
except AttributeError: |
| 910 |
pass |
| 911 |
type(item).m = lambda self: None |
| 912 |
|
| 913 |
opname = "LOAD_ATTR_METHOD_NO_DICT" |
| 914 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 915 |
|
| 916 |
@requires_specialization_ft |
| 917 |
def test_load_attr_method_with_values(self): |
| 918 |
def get_items(): |
| 919 |
class C: |
| 920 |
m = lambda self: None |
| 921 |
|
| 922 |
items = [] |
| 923 |
for _ in range(self.ITEMS): |
| 924 |
item = C() |
| 925 |
items.append(item) |
| 926 |
return items |
| 927 |
|
| 928 |
def read(items): |
| 929 |
for item in items: |
| 930 |
try: |
| 931 |
item.m() |
| 932 |
except AttributeError: |
| 933 |
pass |
| 934 |
|
| 935 |
def write(items): |
| 936 |
for item in items: |
| 937 |
try: |
| 938 |
del item.m |
| 939 |
except AttributeError: |
| 940 |
pass |
| 941 |
type(item).m = lambda self: None |
| 942 |
|
| 943 |
opname = "LOAD_ATTR_METHOD_WITH_VALUES" |
| 944 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 945 |
|
| 946 |
@requires_specialization_ft |
| 947 |
def test_load_attr_module(self): |
| 948 |
def get_items(): |
| 949 |
items = [] |
| 950 |
for _ in range(self.ITEMS): |
| 951 |
item = types.ModuleType("<item>") |
| 952 |
items.append(item) |
| 953 |
return items |
| 954 |
|
| 955 |
def read(items): |
| 956 |
for item in items: |
| 957 |
try: |
| 958 |
item.__name__ |
| 959 |
except AttributeError: |
| 960 |
pass |
| 961 |
|
| 962 |
def write(items): |
| 963 |
for item in items: |
| 964 |
d = item.__dict__.copy() |
| 965 |
item.__dict__.clear() |
| 966 |
item.__dict__.update(d) |
| 967 |
|
| 968 |
opname = "LOAD_ATTR_MODULE" |
| 969 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 970 |
|
| 971 |
@requires_specialization_ft |
| 972 |
def test_load_attr_property(self): |
| 973 |
def get_items(): |
| 974 |
class C: |
| 975 |
a = property(lambda self: None) |
| 976 |
|
| 977 |
items = [] |
| 978 |
for _ in range(self.ITEMS): |
| 979 |
item = C() |
| 980 |
items.append(item) |
| 981 |
return items |
| 982 |
|
| 983 |
def read(items): |
| 984 |
for item in items: |
| 985 |
try: |
| 986 |
item.a |
| 987 |
except AttributeError: |
| 988 |
pass |
| 989 |
|
| 990 |
def write(items): |
| 991 |
for item in items: |
| 992 |
try: |
| 993 |
del type(item).a |
| 994 |
except AttributeError: |
| 995 |
pass |
| 996 |
type(item).a = property(lambda self: None) |
| 997 |
|
| 998 |
opname = "LOAD_ATTR_PROPERTY" |
| 999 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1000 |
|
| 1001 |
@requires_specialization_ft |
| 1002 |
def test_load_attr_slot(self): |
| 1003 |
def get_items(): |
| 1004 |
class C: |
| 1005 |
__slots__ = ["a", "b"] |
| 1006 |
|
| 1007 |
items = [] |
| 1008 |
for i in range(self.ITEMS): |
| 1009 |
item = C() |
| 1010 |
item.a = i |
| 1011 |
item.b = i + self.ITEMS |
| 1012 |
items.append(item) |
| 1013 |
return items |
| 1014 |
|
| 1015 |
def read(items): |
| 1016 |
for item in items: |
| 1017 |
item.a |
| 1018 |
item.b |
| 1019 |
|
| 1020 |
def write(items): |
| 1021 |
for item in items: |
| 1022 |
item.a = 100 |
| 1023 |
item.b = 200 |
| 1024 |
|
| 1025 |
opname = "LOAD_ATTR_SLOT" |
| 1026 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1027 |
|
| 1028 |
@requires_specialization_ft |
| 1029 |
def test_load_attr_with_hint(self): |
| 1030 |
def get_items(): |
| 1031 |
class C: |
| 1032 |
pass |
| 1033 |
|
| 1034 |
items = [] |
| 1035 |
for _ in range(self.ITEMS): |
| 1036 |
item = C() |
| 1037 |
item.a = None |
| 1038 |
# Resize into a combined unicode dict: |
| 1039 |
for i in range(_testinternalcapi.SHARED_KEYS_MAX_SIZE - 1): |
| 1040 |
setattr(item, f"_{i}", None) |
| 1041 |
items.append(item) |
| 1042 |
return items |
| 1043 |
|
| 1044 |
def read(items): |
| 1045 |
for item in items: |
| 1046 |
item.a |
| 1047 |
|
| 1048 |
def write(items): |
| 1049 |
for item in items: |
| 1050 |
item.__dict__[None] = None |
| 1051 |
|
| 1052 |
opname = "LOAD_ATTR_WITH_HINT" |
| 1053 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1054 |
|
| 1055 |
@requires_specialization_ft |
| 1056 |
def test_load_global_module(self): |
| 1057 |
if not have_dict_key_versions(): |
| 1058 |
raise unittest.SkipTest("Low on dict key versions") |
| 1059 |
def get_items(): |
| 1060 |
items = [] |
| 1061 |
for _ in range(self.ITEMS): |
| 1062 |
item = eval("lambda: x", {"x": None}) |
| 1063 |
items.append(item) |
| 1064 |
return items |
| 1065 |
|
| 1066 |
def read(items): |
| 1067 |
for item in items: |
| 1068 |
item() |
| 1069 |
|
| 1070 |
def write(items): |
| 1071 |
for item in items: |
| 1072 |
item.__globals__[None] = None |
| 1073 |
|
| 1074 |
opname = "LOAD_GLOBAL_MODULE" |
| 1075 |
self.assert_races_do_not_crash( |
| 1076 |
opname, get_items, read, write, check_items=True |
| 1077 |
) |
| 1078 |
|
| 1079 |
@requires_specialization |
| 1080 |
def test_store_attr_instance_value(self): |
| 1081 |
def get_items(): |
| 1082 |
class C: |
| 1083 |
pass |
| 1084 |
|
| 1085 |
items = [] |
| 1086 |
for _ in range(self.ITEMS): |
| 1087 |
item = C() |
| 1088 |
items.append(item) |
| 1089 |
return items |
| 1090 |
|
| 1091 |
def read(items): |
| 1092 |
for item in items: |
| 1093 |
item.a = None |
| 1094 |
|
| 1095 |
def write(items): |
| 1096 |
for item in items: |
| 1097 |
item.__dict__[None] = None |
| 1098 |
|
| 1099 |
opname = "STORE_ATTR_INSTANCE_VALUE" |
| 1100 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1101 |
|
| 1102 |
@requires_specialization |
| 1103 |
def test_store_attr_with_hint(self): |
| 1104 |
def get_items(): |
| 1105 |
class C: |
| 1106 |
pass |
| 1107 |
|
| 1108 |
items = [] |
| 1109 |
for _ in range(self.ITEMS): |
| 1110 |
item = C() |
| 1111 |
# Resize into a combined unicode dict: |
| 1112 |
for i in range(_testinternalcapi.SHARED_KEYS_MAX_SIZE - 1): |
| 1113 |
setattr(item, f"_{i}", None) |
| 1114 |
items.append(item) |
| 1115 |
return items |
| 1116 |
|
| 1117 |
def read(items): |
| 1118 |
for item in items: |
| 1119 |
item.a = None |
| 1120 |
|
| 1121 |
def write(items): |
| 1122 |
for item in items: |
| 1123 |
item.__dict__[None] = None |
| 1124 |
|
| 1125 |
opname = "STORE_ATTR_WITH_HINT" |
| 1126 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1127 |
|
| 1128 |
@requires_specialization_ft |
| 1129 |
def test_store_subscr_list_int(self): |
| 1130 |
def get_items(): |
| 1131 |
items = [] |
| 1132 |
for _ in range(self.ITEMS): |
| 1133 |
item = [None] |
| 1134 |
items.append(item) |
| 1135 |
return items |
| 1136 |
|
| 1137 |
def read(items): |
| 1138 |
for item in items: |
| 1139 |
try: |
| 1140 |
item[0] = None |
| 1141 |
except IndexError: |
| 1142 |
pass |
| 1143 |
|
| 1144 |
def write(items): |
| 1145 |
for item in items: |
| 1146 |
item.clear() |
| 1147 |
item.append(None) |
| 1148 |
|
| 1149 |
opname = "STORE_SUBSCR_LIST_INT" |
| 1150 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1151 |
|
| 1152 |
@requires_specialization_ft |
| 1153 |
def test_unpack_sequence_list(self): |
| 1154 |
def get_items(): |
| 1155 |
items = [] |
| 1156 |
for _ in range(self.ITEMS): |
| 1157 |
item = [None] |
| 1158 |
items.append(item) |
| 1159 |
return items |
| 1160 |
|
| 1161 |
def read(items): |
| 1162 |
for item in items: |
| 1163 |
try: |
| 1164 |
[_] = item |
| 1165 |
except ValueError: |
| 1166 |
pass |
| 1167 |
|
| 1168 |
def write(items): |
| 1169 |
for item in items: |
| 1170 |
item.clear() |
| 1171 |
item.append(None) |
| 1172 |
|
| 1173 |
opname = "UNPACK_SEQUENCE_LIST" |
| 1174 |
self.assert_races_do_not_crash(opname, get_items, read, write) |
| 1175 |
|
| 1176 |
class C: |
| 1177 |
pass |
| 1178 |
|
| 1179 |
@requires_specialization |
| 1180 |
class TestInstanceDict(unittest.TestCase): |
| 1181 |
|
| 1182 |
def setUp(self): |
| 1183 |
c = C() |
| 1184 |
c.a, c.b, c.c = 0,0,0 |
| 1185 |
|
| 1186 |
def test_values_on_instance(self): |
| 1187 |
c = C() |
| 1188 |
c.a = 1 |
| 1189 |
C().b = 2 |
| 1190 |
c.c = 3 |
| 1191 |
self.assertEqual( |
| 1192 |
_testinternalcapi.get_object_dict_values(c), |
| 1193 |
(1, '<NULL>', 3) |
| 1194 |
) |
| 1195 |
|
| 1196 |
def test_dict_materialization(self): |
| 1197 |
c = C() |
| 1198 |
c.a = 1 |
| 1199 |
c.b = 2 |
| 1200 |
c.__dict__ |
| 1201 |
self.assertEqual(c.__dict__, {"a":1, "b": 2}) |
| 1202 |
|
| 1203 |
def test_dict_dematerialization(self): |
| 1204 |
c = C() |
| 1205 |
c.a = 1 |
| 1206 |
c.b = 2 |
| 1207 |
c.__dict__ |
| 1208 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1209 |
c.a |
| 1210 |
self.assertEqual( |
| 1211 |
_testinternalcapi.get_object_dict_values(c), |
| 1212 |
(1, 2, '<NULL>') |
| 1213 |
) |
| 1214 |
|
| 1215 |
def test_dict_dematerialization_multiple_refs(self): |
| 1216 |
c = C() |
| 1217 |
c.a = 1 |
| 1218 |
c.b = 2 |
| 1219 |
d = c.__dict__ |
| 1220 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1221 |
c.a |
| 1222 |
self.assertIs(c.__dict__, d) |
| 1223 |
|
| 1224 |
def test_dict_dematerialization_copy(self): |
| 1225 |
c = C() |
| 1226 |
c.a = 1 |
| 1227 |
c.b = 2 |
| 1228 |
c2 = copy.copy(c) |
| 1229 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1230 |
c.a |
| 1231 |
c2.a |
| 1232 |
self.assertEqual( |
| 1233 |
_testinternalcapi.get_object_dict_values(c), |
| 1234 |
(1, 2, '<NULL>') |
| 1235 |
) |
| 1236 |
self.assertEqual( |
| 1237 |
_testinternalcapi.get_object_dict_values(c2), |
| 1238 |
(1, 2, '<NULL>') |
| 1239 |
) |
| 1240 |
c3 = copy.deepcopy(c) |
| 1241 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1242 |
c.a |
| 1243 |
c3.a |
| 1244 |
self.assertEqual( |
| 1245 |
_testinternalcapi.get_object_dict_values(c), |
| 1246 |
(1, 2, '<NULL>') |
| 1247 |
) |
| 1248 |
#NOTE -- c3.__dict__ does not de-materialize |
| 1249 |
|
| 1250 |
def test_dict_dematerialization_pickle(self): |
| 1251 |
c = C() |
| 1252 |
c.a = 1 |
| 1253 |
c.b = 2 |
| 1254 |
c2 = pickle.loads(pickle.dumps(c)) |
| 1255 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1256 |
c.a |
| 1257 |
c2.a |
| 1258 |
self.assertEqual( |
| 1259 |
_testinternalcapi.get_object_dict_values(c), |
| 1260 |
(1, 2, '<NULL>') |
| 1261 |
) |
| 1262 |
self.assertEqual( |
| 1263 |
_testinternalcapi.get_object_dict_values(c2), |
| 1264 |
(1, 2, '<NULL>') |
| 1265 |
) |
| 1266 |
|
| 1267 |
def test_dict_dematerialization_subclass(self): |
| 1268 |
class D(dict): pass |
| 1269 |
c = C() |
| 1270 |
c.a = 1 |
| 1271 |
c.b = 2 |
| 1272 |
c.__dict__ = D(c.__dict__) |
| 1273 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1274 |
c.a |
| 1275 |
self.assertIs( |
| 1276 |
_testinternalcapi.get_object_dict_values(c), |
| 1277 |
None |
| 1278 |
) |
| 1279 |
self.assertEqual( |
| 1280 |
c.__dict__, |
| 1281 |
{'a':1, 'b':2} |
| 1282 |
) |
| 1283 |
|
| 1284 |
def test_125868(self): |
| 1285 |
|
| 1286 |
def make_special_dict(): |
| 1287 |
"""Create a dictionary an object with a this table: |
| 1288 |
index | key | value |
| 1289 |
----- | --- | ----- |
| 1290 |
0 | 'b' | 'value' |
| 1291 |
1 | 'b' | NULL |
| 1292 |
""" |
| 1293 |
class A: |
| 1294 |
pass |
| 1295 |
a = A() |
| 1296 |
a.a = 1 |
| 1297 |
a.b = 2 |
| 1298 |
d = a.__dict__.copy() |
| 1299 |
del d['a'] |
| 1300 |
del d['b'] |
| 1301 |
d['b'] = "value" |
| 1302 |
return d |
| 1303 |
|
| 1304 |
class NoInlineAorB: |
| 1305 |
pass |
| 1306 |
for i in range(ord('c'), ord('z')): |
| 1307 |
setattr(NoInlineAorB(), chr(i), i) |
| 1308 |
|
| 1309 |
c = NoInlineAorB() |
| 1310 |
c.a = 0 |
| 1311 |
c.b = 1 |
| 1312 |
self.assertFalse(_testinternalcapi.has_inline_values(c)) |
| 1313 |
|
| 1314 |
def f(o, n): |
| 1315 |
for i in range(n): |
| 1316 |
o.b = i |
| 1317 |
# Prime f to store to dict slot 1 |
| 1318 |
f(c, _testinternalcapi.SPECIALIZATION_THRESHOLD) |
| 1319 |
|
| 1320 |
test_obj = NoInlineAorB() |
| 1321 |
test_obj.__dict__ = make_special_dict() |
| 1322 |
self.assertEqual(test_obj.b, "value") |
| 1323 |
|
| 1324 |
#This should set x.b = 0 |
| 1325 |
f(test_obj, 1) |
| 1326 |
self.assertEqual(test_obj.b, 0) |
| 1327 |
|
| 1328 |
|
| 1329 |
class TestSpecializer(TestBase): |
| 1330 |
|
| 1331 |
@cpython_only |
| 1332 |
@requires_specialization_ft |
| 1333 |
def test_binary_op(self): |
| 1334 |
def binary_op_add_int(): |
| 1335 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1336 |
a, b = 1, 2 |
| 1337 |
c = a + b |
| 1338 |
self.assertEqual(c, 3) |
| 1339 |
|
| 1340 |
binary_op_add_int() |
| 1341 |
self.assert_specialized(binary_op_add_int, "BINARY_OP_ADD_INT") |
| 1342 |
self.assert_no_opcode(binary_op_add_int, "BINARY_OP") |
| 1343 |
|
| 1344 |
def binary_op_add_unicode(): |
| 1345 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1346 |
a, b = "foo", "bar" |
| 1347 |
c = a + b |
| 1348 |
self.assertEqual(c, "foobar") |
| 1349 |
|
| 1350 |
binary_op_add_unicode() |
| 1351 |
self.assert_specialized(binary_op_add_unicode, "BINARY_OP_ADD_UNICODE") |
| 1352 |
self.assert_no_opcode(binary_op_add_unicode, "BINARY_OP") |
| 1353 |
|
| 1354 |
def binary_op_add_extend(): |
| 1355 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1356 |
a, b = 6, 3.0 |
| 1357 |
c = a + b |
| 1358 |
self.assertEqual(c, 9.0) |
| 1359 |
c = b + a |
| 1360 |
self.assertEqual(c, 9.0) |
| 1361 |
c = a - b |
| 1362 |
self.assertEqual(c, 3.0) |
| 1363 |
c = b - a |
| 1364 |
self.assertEqual(c, -3.0) |
| 1365 |
c = a * b |
| 1366 |
self.assertEqual(c, 18.0) |
| 1367 |
c = b * a |
| 1368 |
self.assertEqual(c, 18.0) |
| 1369 |
c = a / b |
| 1370 |
self.assertEqual(c, 2.0) |
| 1371 |
c = b / a |
| 1372 |
self.assertEqual(c, 0.5) |
| 1373 |
|
| 1374 |
binary_op_add_extend() |
| 1375 |
self.assert_specialized(binary_op_add_extend, "BINARY_OP_EXTEND") |
| 1376 |
self.assert_no_opcode(binary_op_add_extend, "BINARY_OP") |
| 1377 |
|
| 1378 |
def binary_op_zero_division(): |
| 1379 |
def compactlong_lhs(arg): |
| 1380 |
42 / arg |
| 1381 |
def float_lhs(arg): |
| 1382 |
42.0 / arg |
| 1383 |
|
| 1384 |
with self.assertRaises(ZeroDivisionError): |
| 1385 |
compactlong_lhs(0) |
| 1386 |
with self.assertRaises(ZeroDivisionError): |
| 1387 |
compactlong_lhs(0.0) |
| 1388 |
with self.assertRaises(ZeroDivisionError): |
| 1389 |
float_lhs(0.0) |
| 1390 |
with self.assertRaises(ZeroDivisionError): |
| 1391 |
float_lhs(0) |
| 1392 |
|
| 1393 |
self.assert_no_opcode(compactlong_lhs, "BINARY_OP_EXTEND") |
| 1394 |
self.assert_no_opcode(float_lhs, "BINARY_OP_EXTEND") |
| 1395 |
|
| 1396 |
binary_op_zero_division() |
| 1397 |
|
| 1398 |
def binary_op_nan(): |
| 1399 |
def compactlong_lhs(arg): |
| 1400 |
return ( |
| 1401 |
42 + arg, |
| 1402 |
42 - arg, |
| 1403 |
42 * arg, |
| 1404 |
42 / arg, |
| 1405 |
) |
| 1406 |
def compactlong_rhs(arg): |
| 1407 |
return ( |
| 1408 |
arg + 42, |
| 1409 |
arg - 42, |
| 1410 |
arg * 2, |
| 1411 |
arg / 42, |
| 1412 |
) |
| 1413 |
nan = float('nan') |
| 1414 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1415 |
self.assertEqual(compactlong_lhs(1.0), (43.0, 41.0, 42.0, 42.0)) |
| 1416 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 1417 |
self.assertTrue(all(filter(lambda x: x is nan, compactlong_lhs(nan)))) |
| 1418 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1419 |
self.assertEqual(compactlong_rhs(42.0), (84.0, 0.0, 84.0, 1.0)) |
| 1420 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 1421 |
self.assertTrue(all(filter(lambda x: x is nan, compactlong_rhs(nan)))) |
| 1422 |
|
| 1423 |
self.assert_no_opcode(compactlong_lhs, "BINARY_OP_EXTEND") |
| 1424 |
self.assert_no_opcode(compactlong_rhs, "BINARY_OP_EXTEND") |
| 1425 |
|
| 1426 |
binary_op_nan() |
| 1427 |
|
| 1428 |
def binary_op_bitwise_extend(): |
| 1429 |
for _ in range(100): |
| 1430 |
a, b = 2, 7 |
| 1431 |
x = a | b |
| 1432 |
self.assertEqual(x, 7) |
| 1433 |
y = a & b |
| 1434 |
self.assertEqual(y, 2) |
| 1435 |
z = a ^ b |
| 1436 |
self.assertEqual(z, 5) |
| 1437 |
a, b = 3, 9 |
| 1438 |
a |= b |
| 1439 |
self.assertEqual(a, 11) |
| 1440 |
a, b = 11, 9 |
| 1441 |
a &= b |
| 1442 |
self.assertEqual(a, 9) |
| 1443 |
a, b = 3, 9 |
| 1444 |
a ^= b |
| 1445 |
self.assertEqual(a, 10) |
| 1446 |
|
| 1447 |
binary_op_bitwise_extend() |
| 1448 |
self.assert_specialized(binary_op_bitwise_extend, "BINARY_OP_EXTEND") |
| 1449 |
self.assert_no_opcode(binary_op_bitwise_extend, "BINARY_OP") |
| 1450 |
|
| 1451 |
@cpython_only |
| 1452 |
@requires_specialization_ft |
| 1453 |
def test_load_super_attr(self): |
| 1454 |
"""Ensure that LOAD_SUPER_ATTR is specialized as expected.""" |
| 1455 |
|
| 1456 |
class A: |
| 1457 |
def __init__(self): |
| 1458 |
meth = super().__init__ |
| 1459 |
super().__init__() |
| 1460 |
|
| 1461 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1462 |
A() |
| 1463 |
|
| 1464 |
self.assert_specialized(A.__init__, "LOAD_SUPER_ATTR_ATTR") |
| 1465 |
self.assert_specialized(A.__init__, "LOAD_SUPER_ATTR_METHOD") |
| 1466 |
self.assert_no_opcode(A.__init__, "LOAD_SUPER_ATTR") |
| 1467 |
|
| 1468 |
# Temporarily replace super() with something else. |
| 1469 |
real_super = super |
| 1470 |
|
| 1471 |
def fake_super(): |
| 1472 |
def init(self): |
| 1473 |
pass |
| 1474 |
|
| 1475 |
return init |
| 1476 |
|
| 1477 |
# Force unspecialize |
| 1478 |
globals()['super'] = fake_super |
| 1479 |
try: |
| 1480 |
# Should be unspecialized after enough calls. |
| 1481 |
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN): |
| 1482 |
A() |
| 1483 |
finally: |
| 1484 |
globals()['super'] = real_super |
| 1485 |
|
| 1486 |
# Ensure the specialized instructions are not present |
| 1487 |
self.assert_no_opcode(A.__init__, "LOAD_SUPER_ATTR_ATTR") |
| 1488 |
self.assert_no_opcode(A.__init__, "LOAD_SUPER_ATTR_METHOD") |
| 1489 |
|
| 1490 |
@cpython_only |
| 1491 |
@requires_specialization_ft |
| 1492 |
def test_contain_op(self): |
| 1493 |
def contains_op_dict(): |
| 1494 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1495 |
a, b = 1, {1: 2, 2: 5} |
| 1496 |
self.assertTrue(a in b) |
| 1497 |
self.assertFalse(3 in b) |
| 1498 |
|
| 1499 |
contains_op_dict() |
| 1500 |
self.assert_specialized(contains_op_dict, "CONTAINS_OP_DICT") |
| 1501 |
self.assert_no_opcode(contains_op_dict, "CONTAINS_OP") |
| 1502 |
|
| 1503 |
def contains_op_set(): |
| 1504 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1505 |
a, b = 1, {1, 2} |
| 1506 |
self.assertTrue(a in b) |
| 1507 |
self.assertFalse(3 in b) |
| 1508 |
|
| 1509 |
contains_op_set() |
| 1510 |
self.assert_specialized(contains_op_set, "CONTAINS_OP_SET") |
| 1511 |
self.assert_no_opcode(contains_op_set, "CONTAINS_OP") |
| 1512 |
|
| 1513 |
@cpython_only |
| 1514 |
@requires_specialization_ft |
| 1515 |
def test_send_with(self): |
| 1516 |
def run_async(coro): |
| 1517 |
while True: |
| 1518 |
try: |
| 1519 |
coro.send(None) |
| 1520 |
except StopIteration: |
| 1521 |
break |
| 1522 |
|
| 1523 |
class CM: |
| 1524 |
async def __aenter__(self): |
| 1525 |
return self |
| 1526 |
|
| 1527 |
async def __aexit__(self, *exc): |
| 1528 |
pass |
| 1529 |
|
| 1530 |
async def send_with(): |
| 1531 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1532 |
async with CM(): |
| 1533 |
x = 1 |
| 1534 |
|
| 1535 |
run_async(send_with()) |
| 1536 |
# Note there are still unspecialized "SEND" opcodes in the |
| 1537 |
# cleanup paths of the 'with' statement. |
| 1538 |
self.assert_specialized(send_with, "SEND_GEN") |
| 1539 |
|
| 1540 |
@cpython_only |
| 1541 |
@requires_specialization_ft |
| 1542 |
def test_send_yield_from(self): |
| 1543 |
def g(): |
| 1544 |
yield None |
| 1545 |
|
| 1546 |
def send_yield_from(): |
| 1547 |
yield from g() |
| 1548 |
|
| 1549 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1550 |
list(send_yield_from()) |
| 1551 |
|
| 1552 |
self.assert_specialized(send_yield_from, "SEND_GEN") |
| 1553 |
self.assert_no_opcode(send_yield_from, "SEND") |
| 1554 |
|
| 1555 |
@cpython_only |
| 1556 |
@requires_specialization_ft |
| 1557 |
def test_store_attr_slot(self): |
| 1558 |
class C: |
| 1559 |
__slots__ = ['x'] |
| 1560 |
|
| 1561 |
def set_slot(n): |
| 1562 |
c = C() |
| 1563 |
for i in range(n): |
| 1564 |
c.x = i |
| 1565 |
|
| 1566 |
set_slot(_testinternalcapi.SPECIALIZATION_THRESHOLD) |
| 1567 |
|
| 1568 |
self.assert_specialized(set_slot, "STORE_ATTR_SLOT") |
| 1569 |
self.assert_no_opcode(set_slot, "STORE_ATTR") |
| 1570 |
|
| 1571 |
# Adding a property for 'x' should unspecialize it. |
| 1572 |
C.x = property(lambda self: None, lambda self, x: None) |
| 1573 |
set_slot(_testinternalcapi.SPECIALIZATION_COOLDOWN) |
| 1574 |
self.assert_no_opcode(set_slot, "STORE_ATTR_SLOT") |
| 1575 |
|
| 1576 |
@cpython_only |
| 1577 |
@requires_specialization_ft |
| 1578 |
def test_store_attr_instance_value(self): |
| 1579 |
class C: |
| 1580 |
pass |
| 1581 |
|
| 1582 |
@reset_code |
| 1583 |
def set_value(n): |
| 1584 |
c = C() |
| 1585 |
for i in range(n): |
| 1586 |
c.x = i |
| 1587 |
|
| 1588 |
set_value(_testinternalcapi.SPECIALIZATION_THRESHOLD) |
| 1589 |
|
| 1590 |
self.assert_specialized(set_value, "STORE_ATTR_INSTANCE_VALUE") |
| 1591 |
self.assert_no_opcode(set_value, "STORE_ATTR") |
| 1592 |
|
| 1593 |
# Adding a property for 'x' should unspecialize it. |
| 1594 |
C.x = property(lambda self: None, lambda self, x: None) |
| 1595 |
set_value(_testinternalcapi.SPECIALIZATION_COOLDOWN) |
| 1596 |
self.assert_no_opcode(set_value, "STORE_ATTR_INSTANCE_VALUE") |
| 1597 |
|
| 1598 |
@cpython_only |
| 1599 |
@requires_specialization_ft |
| 1600 |
def test_store_attr_with_hint(self): |
| 1601 |
class C: |
| 1602 |
pass |
| 1603 |
|
| 1604 |
c = C() |
| 1605 |
for i in range(_testinternalcapi.SHARED_KEYS_MAX_SIZE - 1): |
| 1606 |
setattr(c, f"_{i}", None) |
| 1607 |
|
| 1608 |
@reset_code |
| 1609 |
def set_value(n): |
| 1610 |
for i in range(n): |
| 1611 |
c.x = i |
| 1612 |
|
| 1613 |
set_value(_testinternalcapi.SPECIALIZATION_THRESHOLD) |
| 1614 |
|
| 1615 |
self.assert_specialized(set_value, "STORE_ATTR_WITH_HINT") |
| 1616 |
self.assert_no_opcode(set_value, "STORE_ATTR") |
| 1617 |
|
| 1618 |
# Adding a property for 'x' should unspecialize it. |
| 1619 |
C.x = property(lambda self: None, lambda self, x: None) |
| 1620 |
set_value(_testinternalcapi.SPECIALIZATION_COOLDOWN) |
| 1621 |
self.assert_no_opcode(set_value, "STORE_ATTR_WITH_HINT") |
| 1622 |
|
| 1623 |
@cpython_only |
| 1624 |
@requires_specialization_ft |
| 1625 |
def test_to_bool(self): |
| 1626 |
def to_bool_bool(): |
| 1627 |
true_cnt, false_cnt = 0, 0 |
| 1628 |
elems = [e % 2 == 0 for e in range(_testinternalcapi.SPECIALIZATION_THRESHOLD)] |
| 1629 |
for e in elems: |
| 1630 |
if e: |
| 1631 |
true_cnt += 1 |
| 1632 |
else: |
| 1633 |
false_cnt += 1 |
| 1634 |
d, m = divmod(_testinternalcapi.SPECIALIZATION_THRESHOLD, 2) |
| 1635 |
self.assertEqual(true_cnt, d + m) |
| 1636 |
self.assertEqual(false_cnt, d) |
| 1637 |
|
| 1638 |
to_bool_bool() |
| 1639 |
self.assert_specialized(to_bool_bool, "TO_BOOL_BOOL") |
| 1640 |
self.assert_no_opcode(to_bool_bool, "TO_BOOL") |
| 1641 |
|
| 1642 |
def to_bool_int(): |
| 1643 |
count = 0 |
| 1644 |
for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1645 |
if i: |
| 1646 |
count += 1 |
| 1647 |
else: |
| 1648 |
count -= 1 |
| 1649 |
self.assertEqual(count, _testinternalcapi.SPECIALIZATION_THRESHOLD - 2) |
| 1650 |
|
| 1651 |
to_bool_int() |
| 1652 |
self.assert_specialized(to_bool_int, "TO_BOOL_INT") |
| 1653 |
self.assert_no_opcode(to_bool_int, "TO_BOOL") |
| 1654 |
|
| 1655 |
def to_bool_list(): |
| 1656 |
count = 0 |
| 1657 |
elems = list(range(_testinternalcapi.SPECIALIZATION_THRESHOLD)) |
| 1658 |
while elems: |
| 1659 |
count += elems.pop() |
| 1660 |
self.assertEqual(elems, []) |
| 1661 |
self.assertEqual(count, sum(range(_testinternalcapi.SPECIALIZATION_THRESHOLD))) |
| 1662 |
|
| 1663 |
to_bool_list() |
| 1664 |
self.assert_specialized(to_bool_list, "TO_BOOL_LIST") |
| 1665 |
self.assert_no_opcode(to_bool_list, "TO_BOOL") |
| 1666 |
|
| 1667 |
def to_bool_none(): |
| 1668 |
count = 0 |
| 1669 |
elems = [None] * _testinternalcapi.SPECIALIZATION_THRESHOLD |
| 1670 |
for e in elems: |
| 1671 |
if not e: |
| 1672 |
count += 1 |
| 1673 |
self.assertEqual(count, _testinternalcapi.SPECIALIZATION_THRESHOLD) |
| 1674 |
|
| 1675 |
to_bool_none() |
| 1676 |
self.assert_specialized(to_bool_none, "TO_BOOL_NONE") |
| 1677 |
self.assert_no_opcode(to_bool_none, "TO_BOOL") |
| 1678 |
|
| 1679 |
def to_bool_str(): |
| 1680 |
count = 0 |
| 1681 |
elems = [""] + ["foo"] * (_testinternalcapi.SPECIALIZATION_THRESHOLD - 1) |
| 1682 |
for e in elems: |
| 1683 |
if e: |
| 1684 |
count += 1 |
| 1685 |
self.assertEqual(count, _testinternalcapi.SPECIALIZATION_THRESHOLD - 1) |
| 1686 |
|
| 1687 |
to_bool_str() |
| 1688 |
self.assert_specialized(to_bool_str, "TO_BOOL_STR") |
| 1689 |
self.assert_no_opcode(to_bool_str, "TO_BOOL") |
| 1690 |
|
| 1691 |
@cpython_only |
| 1692 |
@requires_specialization_ft |
| 1693 |
def test_unpack_sequence(self): |
| 1694 |
def unpack_sequence_two_tuple(): |
| 1695 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1696 |
t = 1, 2 |
| 1697 |
a, b = t |
| 1698 |
self.assertEqual(a, 1) |
| 1699 |
self.assertEqual(b, 2) |
| 1700 |
|
| 1701 |
unpack_sequence_two_tuple() |
| 1702 |
self.assert_specialized(unpack_sequence_two_tuple, |
| 1703 |
"UNPACK_SEQUENCE_TWO_TUPLE") |
| 1704 |
self.assert_no_opcode(unpack_sequence_two_tuple, "UNPACK_SEQUENCE") |
| 1705 |
|
| 1706 |
def unpack_sequence_tuple(): |
| 1707 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1708 |
a, b, c, d = 1, 2, 3, 4 |
| 1709 |
self.assertEqual(a, 1) |
| 1710 |
self.assertEqual(b, 2) |
| 1711 |
self.assertEqual(c, 3) |
| 1712 |
self.assertEqual(d, 4) |
| 1713 |
|
| 1714 |
unpack_sequence_tuple() |
| 1715 |
self.assert_specialized(unpack_sequence_tuple, "UNPACK_SEQUENCE_TUPLE") |
| 1716 |
self.assert_no_opcode(unpack_sequence_tuple, "UNPACK_SEQUENCE") |
| 1717 |
|
| 1718 |
def unpack_sequence_list(): |
| 1719 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1720 |
a, b = [1, 2] |
| 1721 |
self.assertEqual(a, 1) |
| 1722 |
self.assertEqual(b, 2) |
| 1723 |
|
| 1724 |
unpack_sequence_list() |
| 1725 |
self.assert_specialized(unpack_sequence_list, "UNPACK_SEQUENCE_LIST") |
| 1726 |
self.assert_no_opcode(unpack_sequence_list, "UNPACK_SEQUENCE") |
| 1727 |
|
| 1728 |
@cpython_only |
| 1729 |
@requires_specialization_ft |
| 1730 |
def test_binary_subscr(self): |
| 1731 |
def binary_subscr_list_int(): |
| 1732 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1733 |
a = [1, 2, 3] |
| 1734 |
for idx, expected in enumerate(a): |
| 1735 |
self.assertEqual(a[idx], expected) |
| 1736 |
|
| 1737 |
binary_subscr_list_int() |
| 1738 |
self.assert_specialized(binary_subscr_list_int, |
| 1739 |
"BINARY_OP_SUBSCR_LIST_INT") |
| 1740 |
self.assert_no_opcode(binary_subscr_list_int, "BINARY_OP") |
| 1741 |
|
| 1742 |
def binary_subscr_tuple_int(): |
| 1743 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1744 |
a = (1, 2, 3) |
| 1745 |
for idx, expected in enumerate(a): |
| 1746 |
self.assertEqual(a[idx], expected) |
| 1747 |
|
| 1748 |
binary_subscr_tuple_int() |
| 1749 |
self.assert_specialized(binary_subscr_tuple_int, |
| 1750 |
"BINARY_OP_SUBSCR_TUPLE_INT") |
| 1751 |
self.assert_no_opcode(binary_subscr_tuple_int, "BINARY_OP") |
| 1752 |
|
| 1753 |
def binary_subscr_dict(): |
| 1754 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1755 |
a = {1: 2, 2: 3} |
| 1756 |
self.assertEqual(a[1], 2) |
| 1757 |
self.assertEqual(a[2], 3) |
| 1758 |
|
| 1759 |
binary_subscr_dict() |
| 1760 |
self.assert_specialized(binary_subscr_dict, "BINARY_OP_SUBSCR_DICT") |
| 1761 |
self.assert_no_opcode(binary_subscr_dict, "BINARY_OP") |
| 1762 |
|
| 1763 |
def binary_subscr_str_int(): |
| 1764 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1765 |
a = "foobar" |
| 1766 |
for idx, expected in enumerate(a): |
| 1767 |
self.assertEqual(a[idx], expected) |
| 1768 |
|
| 1769 |
binary_subscr_str_int() |
| 1770 |
self.assert_specialized(binary_subscr_str_int, "BINARY_OP_SUBSCR_STR_INT") |
| 1771 |
self.assert_no_opcode(binary_subscr_str_int, "BINARY_OP") |
| 1772 |
|
| 1773 |
def binary_subscr_getitems(): |
| 1774 |
class C: |
| 1775 |
def __init__(self, val): |
| 1776 |
self.val = val |
| 1777 |
def __getitem__(self, item): |
| 1778 |
return self.val |
| 1779 |
|
| 1780 |
items = [C(i) for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD)] |
| 1781 |
for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1782 |
self.assertEqual(items[i][i], i) |
| 1783 |
|
| 1784 |
binary_subscr_getitems() |
| 1785 |
self.assert_specialized(binary_subscr_getitems, "BINARY_OP_SUBSCR_GETITEM") |
| 1786 |
self.assert_no_opcode(binary_subscr_getitems, "BINARY_OP") |
| 1787 |
|
| 1788 |
@cpython_only |
| 1789 |
@requires_specialization_ft |
| 1790 |
def test_compare_op(self): |
| 1791 |
def compare_op_int(): |
| 1792 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1793 |
a, b = 1, 2 |
| 1794 |
c = a == b |
| 1795 |
self.assertFalse(c) |
| 1796 |
|
| 1797 |
compare_op_int() |
| 1798 |
self.assert_specialized(compare_op_int, "COMPARE_OP_INT") |
| 1799 |
self.assert_no_opcode(compare_op_int, "COMPARE_OP") |
| 1800 |
|
| 1801 |
def compare_op_float(): |
| 1802 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1803 |
a, b = 1.0, 2.0 |
| 1804 |
c = a == b |
| 1805 |
self.assertFalse(c) |
| 1806 |
|
| 1807 |
compare_op_float() |
| 1808 |
self.assert_specialized(compare_op_float, "COMPARE_OP_FLOAT") |
| 1809 |
self.assert_no_opcode(compare_op_float, "COMPARE_OP") |
| 1810 |
|
| 1811 |
def compare_op_str(): |
| 1812 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 1813 |
a, b = "spam", "ham" |
| 1814 |
c = a == b |
| 1815 |
self.assertFalse(c) |
| 1816 |
|
| 1817 |
compare_op_str() |
| 1818 |
self.assert_specialized(compare_op_str, "COMPARE_OP_STR") |
| 1819 |
self.assert_no_opcode(compare_op_str, "COMPARE_OP") |
| 1820 |
|
| 1821 |
@cpython_only |
| 1822 |
@requires_specialization_ft |
| 1823 |
def test_load_const(self): |
| 1824 |
def load_const(): |
| 1825 |
def unused(): pass |
| 1826 |
# Currently, the empty tuple is immortal, and the otherwise |
| 1827 |
# unused nested function's code object is mortal. This test will |
| 1828 |
# have to use different values if either of that changes. |
| 1829 |
return () |
| 1830 |
|
| 1831 |
load_const() |
| 1832 |
self.assert_specialized(load_const, "LOAD_CONST_IMMORTAL") |
| 1833 |
self.assert_specialized(load_const, "LOAD_CONST_MORTAL") |
| 1834 |
self.assert_no_opcode(load_const, "LOAD_CONST") |
| 1835 |
|
| 1836 |
@cpython_only |
| 1837 |
@requires_specialization_ft |
| 1838 |
def test_for_iter(self): |
| 1839 |
L = list(range(10)) |
| 1840 |
def for_iter_list(): |
| 1841 |
for i in L: |
| 1842 |
self.assertIn(i, L) |
| 1843 |
|
| 1844 |
for_iter_list() |
| 1845 |
self.assert_specialized(for_iter_list, "FOR_ITER_LIST") |
| 1846 |
self.assert_no_opcode(for_iter_list, "FOR_ITER") |
| 1847 |
|
| 1848 |
t = tuple(range(10)) |
| 1849 |
def for_iter_tuple(): |
| 1850 |
for i in t: |
| 1851 |
self.assertIn(i, t) |
| 1852 |
|
| 1853 |
for_iter_tuple() |
| 1854 |
self.assert_specialized(for_iter_tuple, "FOR_ITER_TUPLE") |
| 1855 |
self.assert_no_opcode(for_iter_tuple, "FOR_ITER") |
| 1856 |
|
| 1857 |
r = range(10) |
| 1858 |
def for_iter_range(): |
| 1859 |
for i in r: |
| 1860 |
self.assertIn(i, r) |
| 1861 |
|
| 1862 |
for_iter_range() |
| 1863 |
self.assert_specialized(for_iter_range, "FOR_ITER_RANGE") |
| 1864 |
self.assert_no_opcode(for_iter_range, "FOR_ITER") |
| 1865 |
|
| 1866 |
def for_iter_generator(): |
| 1867 |
for i in (i for i in range(10)): |
| 1868 |
i + 1 |
| 1869 |
|
| 1870 |
for_iter_generator() |
| 1871 |
self.assert_specialized(for_iter_generator, "FOR_ITER_GEN") |
| 1872 |
self.assert_no_opcode(for_iter_generator, "FOR_ITER") |
| 1873 |
|
| 1874 |
|
| 1875 |
if __name__ == "__main__": |
| 1876 |
unittest.main() |