Debian Codegraph @

Lib/test/test_capi/test_opt.py

raw
1 import contextlib
2 import sys
3 import textwrap
4 import unittest
5 import gc
6 import os
7
8 import _opcode
9
10 from test.support import script_helper, requires_specialization, import_helper
11
12 _testinternalcapi = import_helper.import_module("_testinternalcapi")
13
14 from _testinternalcapi import TIER2_THRESHOLD
15
16 @contextlib.contextmanager
17 def temporary_optimizer(opt):
18 old_opt = _testinternalcapi.get_optimizer()
19 _testinternalcapi.set_optimizer(opt)
20 try:
21 yield
22 finally:
23 _testinternalcapi.set_optimizer(old_opt)
24
25
26 @contextlib.contextmanager
27 def clear_executors(func):
28 # Clear executors in func before and after running a block
29 func.__code__ = func.__code__.replace()
30 try:
31 yield
32 finally:
33 func.__code__ = func.__code__.replace()
34
35
36 @requires_specialization
37 @unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
38 "Requires optimizer infrastructure")
39 class TestOptimizerAPI(unittest.TestCase):
40
41 def test_new_counter_optimizer_dealloc(self):
42 # See gh-108727
43 def f():
44 _testinternalcapi.new_counter_optimizer()
45
46 f()
47
48 def test_get_set_optimizer(self):
49 old = _testinternalcapi.get_optimizer()
50 opt = _testinternalcapi.new_counter_optimizer()
51 try:
52 _testinternalcapi.set_optimizer(opt)
53 self.assertEqual(_testinternalcapi.get_optimizer(), opt)
54 _testinternalcapi.set_optimizer(None)
55 self.assertEqual(_testinternalcapi.get_optimizer(), None)
56 finally:
57 _testinternalcapi.set_optimizer(old)
58
59
60 def test_counter_optimizer(self):
61 # Generate a new function at each call
62 ns = {}
63 exec(textwrap.dedent("""
64 def loop():
65 for _ in range(1000):
66 pass
67 """), ns, ns)
68 loop = ns['loop']
69
70 for repeat in range(5):
71 opt = _testinternalcapi.new_counter_optimizer()
72 with temporary_optimizer(opt):
73 self.assertEqual(opt.get_count(), 0)
74 with clear_executors(loop):
75 loop()
76 # Subtract because optimizer doesn't kick in sooner
77 self.assertEqual(opt.get_count(), 1000 - TIER2_THRESHOLD)
78
79 def test_long_loop(self):
80 "Check that we aren't confused by EXTENDED_ARG"
81
82 # Generate a new function at each call
83 ns = {}
84 exec(textwrap.dedent("""
85 def nop():
86 pass
87
88 def long_loop():
89 for _ in range(20):
90 nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
91 nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
92 nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
93 nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
94 nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
95 nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
96 nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
97 """), ns, ns)
98 long_loop = ns['long_loop']
99
100 opt = _testinternalcapi.new_counter_optimizer()
101 with temporary_optimizer(opt):
102 self.assertEqual(opt.get_count(), 0)
103 long_loop()
104 self.assertEqual(opt.get_count(), 20 - TIER2_THRESHOLD) # Need iterations to warm up
105
106 def test_code_restore_for_ENTER_EXECUTOR(self):
107 def testfunc(x):
108 i = 0
109 while i < x:
110 i += 1
111
112 opt = _testinternalcapi.new_counter_optimizer()
113 with temporary_optimizer(opt):
114 testfunc(1000)
115 code, replace_code = testfunc.__code__, testfunc.__code__.replace()
116 self.assertEqual(code, replace_code)
117 self.assertEqual(hash(code), hash(replace_code))
118
119
120 def get_first_executor(func):
121 code = func.__code__
122 co_code = code.co_code
123 for i in range(0, len(co_code), 2):
124 try:
125 return _opcode.get_executor(code, i)
126 except ValueError:
127 pass
128 return None
129
130
131 def iter_opnames(ex):
132 for item in ex:
133 yield item[0]
134
135
136 def get_opnames(ex):
137 return list(iter_opnames(ex))
138
139
140 @requires_specialization
141 @unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
142 "Requires optimizer infrastructure")
143 class TestExecutorInvalidation(unittest.TestCase):
144
145 def setUp(self):
146 self.old = _testinternalcapi.get_optimizer()
147 self.opt = _testinternalcapi.new_counter_optimizer()
148 _testinternalcapi.set_optimizer(self.opt)
149
150 def tearDown(self):
151 _testinternalcapi.set_optimizer(self.old)
152
153 def test_invalidate_object(self):
154 # Generate a new set of functions at each call
155 ns = {}
156 func_src = "\n".join(
157 f"""
158 def f{n}():
159 for _ in range(1000):
160 pass
161 """ for n in range(5)
162 )
163 exec(textwrap.dedent(func_src), ns, ns)
164 funcs = [ ns[f'f{n}'] for n in range(5)]
165 objects = [object() for _ in range(5)]
166
167 for f in funcs:
168 f()
169 executors = [get_first_executor(f) for f in funcs]
170 # Set things up so each executor depends on the objects
171 # with an equal or lower index.
172 for i, exe in enumerate(executors):
173 self.assertTrue(exe.is_valid())
174 for obj in objects[:i+1]:
175 _testinternalcapi.add_executor_dependency(exe, obj)
176 self.assertTrue(exe.is_valid())
177 # Assert that the correct executors are invalidated
178 # and check that nothing crashes when we invalidate
179 # an executor mutliple times.
180 for i in (4,3,2,1,0):
181 _testinternalcapi.invalidate_executors(objects[i])
182 for exe in executors[i:]:
183 self.assertFalse(exe.is_valid())
184 for exe in executors[:i]:
185 self.assertTrue(exe.is_valid())
186
187 def test_uop_optimizer_invalidation(self):
188 # Generate a new function at each call
189 ns = {}
190 exec(textwrap.dedent("""
191 def f():
192 for i in range(1000):
193 pass
194 """), ns, ns)
195 f = ns['f']
196 opt = _testinternalcapi.new_uop_optimizer()
197 with temporary_optimizer(opt):
198 f()
199 exe = get_first_executor(f)
200 self.assertIsNotNone(exe)
201 self.assertTrue(exe.is_valid())
202 _testinternalcapi.invalidate_executors(f.__code__)
203 self.assertFalse(exe.is_valid())
204
205 def test_sys__clear_internal_caches(self):
206 def f():
207 for _ in range(1000):
208 pass
209 opt = _testinternalcapi.new_uop_optimizer()
210 with temporary_optimizer(opt):
211 f()
212 exe = get_first_executor(f)
213 self.assertIsNotNone(exe)
214 self.assertTrue(exe.is_valid())
215 sys._clear_internal_caches()
216 self.assertFalse(exe.is_valid())
217 exe = get_first_executor(f)
218 self.assertIsNone(exe)
219
220
221 @requires_specialization
222 @unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
223 "Requires optimizer infrastructure")
224 @unittest.skipIf(os.getenv("PYTHON_UOPS_OPTIMIZE") == "0", "Needs uop optimizer to run.")
225 class TestUops(unittest.TestCase):
226
227 def test_basic_loop(self):
228 def testfunc(x):
229 i = 0
230 while i < x:
231 i += 1
232
233 opt = _testinternalcapi.new_uop_optimizer()
234 with temporary_optimizer(opt):
235 testfunc(1000)
236
237 ex = get_first_executor(testfunc)
238 self.assertIsNotNone(ex)
239 uops = get_opnames(ex)
240 self.assertIn("_JUMP_TO_TOP", uops)
241 self.assertIn("_LOAD_FAST_0", uops)
242
243 def test_extended_arg(self):
244 "Check EXTENDED_ARG handling in superblock creation"
245 ns = {}
246 exec(textwrap.dedent("""
247 def many_vars():
248 # 260 vars, so z9 should have index 259
249 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = a9 = 42
250 b0 = b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = 42
251 c0 = c1 = c2 = c3 = c4 = c5 = c6 = c7 = c8 = c9 = 42
252 d0 = d1 = d2 = d3 = d4 = d5 = d6 = d7 = d8 = d9 = 42
253 e0 = e1 = e2 = e3 = e4 = e5 = e6 = e7 = e8 = e9 = 42
254 f0 = f1 = f2 = f3 = f4 = f5 = f6 = f7 = f8 = f9 = 42
255 g0 = g1 = g2 = g3 = g4 = g5 = g6 = g7 = g8 = g9 = 42
256 h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = h8 = h9 = 42
257 i0 = i1 = i2 = i3 = i4 = i5 = i6 = i7 = i8 = i9 = 42
258 j0 = j1 = j2 = j3 = j4 = j5 = j6 = j7 = j8 = j9 = 42
259 k0 = k1 = k2 = k3 = k4 = k5 = k6 = k7 = k8 = k9 = 42
260 l0 = l1 = l2 = l3 = l4 = l5 = l6 = l7 = l8 = l9 = 42
261 m0 = m1 = m2 = m3 = m4 = m5 = m6 = m7 = m8 = m9 = 42
262 n0 = n1 = n2 = n3 = n4 = n5 = n6 = n7 = n8 = n9 = 42
263 o0 = o1 = o2 = o3 = o4 = o5 = o6 = o7 = o8 = o9 = 42
264 p0 = p1 = p2 = p3 = p4 = p5 = p6 = p7 = p8 = p9 = 42
265 q0 = q1 = q2 = q3 = q4 = q5 = q6 = q7 = q8 = q9 = 42
266 r0 = r1 = r2 = r3 = r4 = r5 = r6 = r7 = r8 = r9 = 42
267 s0 = s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = 42
268 t0 = t1 = t2 = t3 = t4 = t5 = t6 = t7 = t8 = t9 = 42
269 u0 = u1 = u2 = u3 = u4 = u5 = u6 = u7 = u8 = u9 = 42
270 v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = v8 = v9 = 42
271 w0 = w1 = w2 = w3 = w4 = w5 = w6 = w7 = w8 = w9 = 42
272 x0 = x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = 42
273 y0 = y1 = y2 = y3 = y4 = y5 = y6 = y7 = y8 = y9 = 42
274 z0 = z1 = z2 = z3 = z4 = z5 = z6 = z7 = z8 = z9 = 42
275 while z9 > 0:
276 z9 = z9 - 1
277 """), ns, ns)
278 many_vars = ns["many_vars"]
279
280 opt = _testinternalcapi.new_uop_optimizer()
281 with temporary_optimizer(opt):
282 ex = get_first_executor(many_vars)
283 self.assertIsNone(ex)
284 many_vars()
285
286 ex = get_first_executor(many_vars)
287 self.assertIsNotNone(ex)
288 self.assertTrue(any((opcode, oparg, operand) == ("_LOAD_FAST", 259, 0)
289 for opcode, oparg, _, operand in list(ex)))
290
291 def test_unspecialized_unpack(self):
292 # An example of an unspecialized opcode
293 def testfunc(x):
294 i = 0
295 while i < x:
296 i += 1
297 a, b = {1: 2, 3: 3}
298 assert a == 1 and b == 3
299 i = 0
300 while i < x:
301 i += 1
302
303 opt = _testinternalcapi.new_uop_optimizer()
304
305 with temporary_optimizer(opt):
306 testfunc(20)
307
308 ex = get_first_executor(testfunc)
309 self.assertIsNotNone(ex)
310 uops = get_opnames(ex)
311 self.assertIn("_UNPACK_SEQUENCE", uops)
312
313 def test_pop_jump_if_false(self):
314 def testfunc(n):
315 i = 0
316 while i < n:
317 i += 1
318
319 opt = _testinternalcapi.new_uop_optimizer()
320 with temporary_optimizer(opt):
321 testfunc(20)
322
323 ex = get_first_executor(testfunc)
324 self.assertIsNotNone(ex)
325 uops = get_opnames(ex)
326 self.assertIn("_GUARD_IS_TRUE_POP", uops)
327
328 def test_pop_jump_if_none(self):
329 def testfunc(a):
330 for x in a:
331 if x is None:
332 x = 0
333
334 opt = _testinternalcapi.new_uop_optimizer()
335 with temporary_optimizer(opt):
336 testfunc(range(20))
337
338 ex = get_first_executor(testfunc)
339 self.assertIsNotNone(ex)
340 uops = get_opnames(ex)
341 self.assertNotIn("_GUARD_IS_NONE_POP", uops)
342 self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops)
343
344 def test_pop_jump_if_not_none(self):
345 def testfunc(a):
346 for x in a:
347 x = None
348 if x is not None:
349 x = 0
350
351 opt = _testinternalcapi.new_uop_optimizer()
352 with temporary_optimizer(opt):
353 testfunc(range(20))
354
355 ex = get_first_executor(testfunc)
356 self.assertIsNotNone(ex)
357 uops = get_opnames(ex)
358 self.assertNotIn("_GUARD_IS_NONE_POP", uops)
359 self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops)
360
361 def test_pop_jump_if_true(self):
362 def testfunc(n):
363 i = 0
364 while not i >= n:
365 i += 1
366
367 opt = _testinternalcapi.new_uop_optimizer()
368 with temporary_optimizer(opt):
369 testfunc(20)
370
371 ex = get_first_executor(testfunc)
372 self.assertIsNotNone(ex)
373 uops = get_opnames(ex)
374 self.assertIn("_GUARD_IS_FALSE_POP", uops)
375
376 def test_jump_backward(self):
377 def testfunc(n):
378 i = 0
379 while i < n:
380 i += 1
381
382 opt = _testinternalcapi.new_uop_optimizer()
383 with temporary_optimizer(opt):
384 testfunc(20)
385
386 ex = get_first_executor(testfunc)
387 self.assertIsNotNone(ex)
388 uops = get_opnames(ex)
389 self.assertIn("_JUMP_TO_TOP", uops)
390
391 def test_jump_forward(self):
392 def testfunc(n):
393 a = 0
394 while a < n:
395 if a < 0:
396 a = -a
397 else:
398 a = +a
399 a += 1
400 return a
401
402 opt = _testinternalcapi.new_uop_optimizer()
403 with temporary_optimizer(opt):
404 testfunc(20)
405
406 ex = get_first_executor(testfunc)
407 self.assertIsNotNone(ex)
408 uops = get_opnames(ex)
409 # Since there is no JUMP_FORWARD instruction,
410 # look for indirect evidence: the += operator
411 self.assertIn("_BINARY_OP_ADD_INT", uops)
412
413 def test_for_iter_range(self):
414 def testfunc(n):
415 total = 0
416 for i in range(n):
417 total += i
418 return total
419
420 opt = _testinternalcapi.new_uop_optimizer()
421 with temporary_optimizer(opt):
422 total = testfunc(20)
423 self.assertEqual(total, 190)
424
425 ex = get_first_executor(testfunc)
426 self.assertIsNotNone(ex)
427 # for i, (opname, oparg) in enumerate(ex):
428 # print(f"{i:4d}: {opname:<20s} {oparg:3d}")
429 uops = get_opnames(ex)
430 self.assertIn("_GUARD_NOT_EXHAUSTED_RANGE", uops)
431 # Verification that the jump goes past END_FOR
432 # is done by manual inspection of the output
433
434 def test_for_iter_list(self):
435 def testfunc(a):
436 total = 0
437 for i in a:
438 total += i
439 return total
440
441 opt = _testinternalcapi.new_uop_optimizer()
442 with temporary_optimizer(opt):
443 a = list(range(20))
444 total = testfunc(a)
445 self.assertEqual(total, 190)
446
447 ex = get_first_executor(testfunc)
448 self.assertIsNotNone(ex)
449 # for i, (opname, oparg) in enumerate(ex):
450 # print(f"{i:4d}: {opname:<20s} {oparg:3d}")
451 uops = get_opnames(ex)
452 self.assertIn("_GUARD_NOT_EXHAUSTED_LIST", uops)
453 # Verification that the jump goes past END_FOR
454 # is done by manual inspection of the output
455
456 def test_for_iter_tuple(self):
457 def testfunc(a):
458 total = 0
459 for i in a:
460 total += i
461 return total
462
463 opt = _testinternalcapi.new_uop_optimizer()
464 with temporary_optimizer(opt):
465 a = tuple(range(20))
466 total = testfunc(a)
467 self.assertEqual(total, 190)
468
469 ex = get_first_executor(testfunc)
470 self.assertIsNotNone(ex)
471 # for i, (opname, oparg) in enumerate(ex):
472 # print(f"{i:4d}: {opname:<20s} {oparg:3d}")
473 uops = get_opnames(ex)
474 self.assertIn("_GUARD_NOT_EXHAUSTED_TUPLE", uops)
475 # Verification that the jump goes past END_FOR
476 # is done by manual inspection of the output
477
478 def test_list_edge_case(self):
479 def testfunc(it):
480 for x in it:
481 pass
482
483 opt = _testinternalcapi.new_uop_optimizer()
484 with temporary_optimizer(opt):
485 a = [1, 2, 3]
486 it = iter(a)
487 testfunc(it)
488 a.append(4)
489 with self.assertRaises(StopIteration):
490 next(it)
491
492 def test_call_py_exact_args(self):
493 def testfunc(n):
494 def dummy(x):
495 return x+1
496 for i in range(n):
497 dummy(i)
498
499 opt = _testinternalcapi.new_uop_optimizer()
500 with temporary_optimizer(opt):
501 testfunc(20)
502
503 ex = get_first_executor(testfunc)
504 self.assertIsNotNone(ex)
505 uops = get_opnames(ex)
506 self.assertIn("_PUSH_FRAME", uops)
507 self.assertIn("_BINARY_OP_ADD_INT", uops)
508
509 def test_branch_taken(self):
510 def testfunc(n):
511 for i in range(n):
512 if i < 0:
513 i = 0
514 else:
515 i = 1
516
517 opt = _testinternalcapi.new_uop_optimizer()
518 with temporary_optimizer(opt):
519 testfunc(20)
520
521 ex = get_first_executor(testfunc)
522 self.assertIsNotNone(ex)
523 uops = get_opnames(ex)
524 self.assertIn("_GUARD_IS_FALSE_POP", uops)
525
526 def test_for_iter_tier_two(self):
527 class MyIter:
528 def __init__(self, n):
529 self.n = n
530 def __iter__(self):
531 return self
532 def __next__(self):
533 self.n -= 1
534 if self.n < 0:
535 raise StopIteration
536 return self.n
537
538 def testfunc(n, m):
539 x = 0
540 for i in range(m):
541 for j in MyIter(n):
542 x += 1000*i + j
543 return x
544
545 opt = _testinternalcapi.new_uop_optimizer()
546 with temporary_optimizer(opt):
547 x = testfunc(10, 10)
548
549 self.assertEqual(x, sum(range(10)) * 10010)
550
551 ex = get_first_executor(testfunc)
552 self.assertIsNotNone(ex)
553 uops = get_opnames(ex)
554 self.assertIn("_FOR_ITER_TIER_TWO", uops)
555
556 def test_confidence_score(self):
557 def testfunc(n):
558 bits = 0
559 for i in range(n):
560 if i & 0x01:
561 bits += 1
562 if i & 0x02:
563 bits += 1
564 if i&0x04:
565 bits += 1
566 if i&0x08:
567 bits += 1
568 if i&0x10:
569 bits += 1
570 if i&0x20:
571 bits += 1
572 return bits
573
574 opt = _testinternalcapi.new_uop_optimizer()
575 with temporary_optimizer(opt):
576 x = testfunc(20)
577
578 self.assertEqual(x, 40)
579 ex = get_first_executor(testfunc)
580 self.assertIsNotNone(ex)
581 ops = list(iter_opnames(ex))
582 #Since branch is 50/50 the trace could go either way.
583 count = ops.count("_GUARD_IS_TRUE_POP") + ops.count("_GUARD_IS_FALSE_POP")
584 self.assertLessEqual(count, 2)
585
586
587 @requires_specialization
588 @unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
589 "Requires optimizer infrastructure")
590 @unittest.skipIf(os.getenv("PYTHON_UOPS_OPTIMIZE") == "0", "Needs uop optimizer to run.")
591 class TestUopsOptimization(unittest.TestCase):
592
593 def _run_with_optimizer(self, testfunc, arg):
594 res = None
595 opt = _testinternalcapi.new_uop_optimizer()
596 with temporary_optimizer(opt):
597 res = testfunc(arg)
598
599 ex = get_first_executor(testfunc)
600 return res, ex
601
602
603 def test_int_type_propagation(self):
604 def testfunc(loops):
605 num = 0
606 for i in range(loops):
607 x = num + num
608 a = x + 1
609 num += 1
610 return a
611
612 res, ex = self._run_with_optimizer(testfunc, 32)
613 self.assertIsNotNone(ex)
614 self.assertEqual(res, 63)
615 binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"]
616 guard_both_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
617 self.assertGreaterEqual(len(binop_count), 3)
618 self.assertLessEqual(len(guard_both_int_count), 1)
619
620 def test_int_type_propagation_through_frame(self):
621 def double(x):
622 return x + x
623 def testfunc(loops):
624 num = 0
625 for i in range(loops):
626 x = num + num
627 a = double(x)
628 num += 1
629 return a
630
631 opt = _testinternalcapi.new_uop_optimizer()
632 res = None
633 with temporary_optimizer(opt):
634 res = testfunc(32)
635
636 ex = get_first_executor(testfunc)
637 self.assertIsNotNone(ex)
638 self.assertEqual(res, 124)
639 binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"]
640 guard_both_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
641 self.assertGreaterEqual(len(binop_count), 3)
642 self.assertLessEqual(len(guard_both_int_count), 1)
643
644 def test_int_type_propagation_from_frame(self):
645 def double(x):
646 return x + x
647 def testfunc(loops):
648 num = 0
649 for i in range(loops):
650 a = double(num)
651 x = a + a
652 num += 1
653 return x
654
655 opt = _testinternalcapi.new_uop_optimizer()
656 res = None
657 with temporary_optimizer(opt):
658 res = testfunc(32)
659
660 ex = get_first_executor(testfunc)
661 self.assertIsNotNone(ex)
662 self.assertEqual(res, 124)
663 binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"]
664 guard_both_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
665 self.assertGreaterEqual(len(binop_count), 3)
666 self.assertLessEqual(len(guard_both_int_count), 1)
667
668 def test_int_impure_region(self):
669 def testfunc(loops):
670 num = 0
671 while num < loops:
672 x = num + num
673 y = 1
674 x // 2
675 a = x + y
676 num += 1
677 return a
678
679 res, ex = self._run_with_optimizer(testfunc, 64)
680 self.assertIsNotNone(ex)
681 binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"]
682 self.assertGreaterEqual(len(binop_count), 3)
683
684 def test_call_py_exact_args(self):
685 def testfunc(n):
686 def dummy(x):
687 return x+1
688 for i in range(n):
689 dummy(i)
690
691 res, ex = self._run_with_optimizer(testfunc, 32)
692 self.assertIsNotNone(ex)
693 uops = get_opnames(ex)
694 self.assertIn("_PUSH_FRAME", uops)
695 self.assertIn("_BINARY_OP_ADD_INT", uops)
696 self.assertNotIn("_CHECK_PEP_523", uops)
697
698 def test_int_type_propagate_through_range(self):
699 def testfunc(n):
700
701 for i in range(n):
702 x = i + i
703 return x
704
705 res, ex = self._run_with_optimizer(testfunc, 32)
706 self.assertEqual(res, 62)
707 self.assertIsNotNone(ex)
708 uops = get_opnames(ex)
709 self.assertNotIn("_GUARD_BOTH_INT", uops)
710
711 def test_int_value_numbering(self):
712 def testfunc(n):
713
714 y = 1
715 for i in range(n):
716 x = y
717 z = x
718 a = z
719 b = a
720 res = x + z + a + b
721 return res
722
723 res, ex = self._run_with_optimizer(testfunc, 32)
724 self.assertEqual(res, 4)
725 self.assertIsNotNone(ex)
726 uops = get_opnames(ex)
727 self.assertIn("_GUARD_BOTH_INT", uops)
728 guard_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
729 self.assertEqual(len(guard_count), 1)
730
731 def test_comprehension(self):
732 def testfunc(n):
733 for _ in range(n):
734 return [i for i in range(n)]
735
736 res, ex = self._run_with_optimizer(testfunc, 32)
737 self.assertEqual(res, list(range(32)))
738 self.assertIsNotNone(ex)
739 uops = get_opnames(ex)
740 self.assertNotIn("_BINARY_OP_ADD_INT", uops)
741
742 def test_call_py_exact_args_disappearing(self):
743 def dummy(x):
744 return x+1
745
746 def testfunc(n):
747 for i in range(n):
748 dummy(i)
749
750 opt = _testinternalcapi.new_uop_optimizer()
751 # Trigger specialization
752 testfunc(8)
753 with temporary_optimizer(opt):
754 del dummy
755 gc.collect()
756
757 def dummy(x):
758 return x + 2
759 testfunc(32)
760
761 ex = get_first_executor(testfunc)
762 # Honestly as long as it doesn't crash it's fine.
763 # Whether we get an executor or not is non-deterministic,
764 # because it's decided by when the function is freed.
765 # This test is a little implementation specific.
766
767 def test_promote_globals_to_constants(self):
768
769 result = script_helper.run_python_until_end('-c', textwrap.dedent("""
770 import _testinternalcapi
771 import opcode
772 import _opcode
773
774 def get_first_executor(func):
775 code = func.__code__
776 co_code = code.co_code
777 for i in range(0, len(co_code), 2):
778 try:
779 return _opcode.get_executor(code, i)
780 except ValueError:
781 pass
782 return None
783
784 def get_opnames(ex):
785 return {item[0] for item in ex}
786
787 def testfunc(n):
788 for i in range(n):
789 x = range(i)
790 return x
791
792 opt = _testinternalcapi.new_uop_optimizer()
793 _testinternalcapi.set_optimizer(opt)
794 testfunc(64)
795
796 ex = get_first_executor(testfunc)
797 assert ex is not None
798 uops = get_opnames(ex)
799 assert "_LOAD_GLOBAL_BUILTINS" not in uops
800 assert "_LOAD_CONST_INLINE_BORROW_WITH_NULL" in uops
801 """))
802 self.assertEqual(result[0].rc, 0, result)
803
804 def test_float_add_constant_propagation(self):
805 def testfunc(n):
806 a = 1.0
807 for _ in range(n):
808 a = a + 0.25
809 a = a + 0.25
810 a = a + 0.25
811 a = a + 0.25
812 return a
813
814 res, ex = self._run_with_optimizer(testfunc, 32)
815 self.assertAlmostEqual(res, 33.0)
816 self.assertIsNotNone(ex)
817 uops = get_opnames(ex)
818 guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
819 self.assertLessEqual(len(guard_both_float_count), 1)
820 # TODO gh-115506: this assertion may change after propagating constants.
821 # We'll also need to verify that propagation actually occurs.
822 self.assertIn("_BINARY_OP_ADD_FLOAT", uops)
823
824 def test_float_subtract_constant_propagation(self):
825 def testfunc(n):
826 a = 1.0
827 for _ in range(n):
828 a = a - 0.25
829 a = a - 0.25
830 a = a - 0.25
831 a = a - 0.25
832 return a
833
834 res, ex = self._run_with_optimizer(testfunc, 32)
835 self.assertAlmostEqual(res, -31.0)
836 self.assertIsNotNone(ex)
837 uops = get_opnames(ex)
838 guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
839 self.assertLessEqual(len(guard_both_float_count), 1)
840 # TODO gh-115506: this assertion may change after propagating constants.
841 # We'll also need to verify that propagation actually occurs.
842 self.assertIn("_BINARY_OP_SUBTRACT_FLOAT", uops)
843
844 def test_float_multiply_constant_propagation(self):
845 def testfunc(n):
846 a = 1.0
847 for _ in range(n):
848 a = a * 1.0
849 a = a * 1.0
850 a = a * 1.0
851 a = a * 1.0
852 return a
853
854 res, ex = self._run_with_optimizer(testfunc, 32)
855 self.assertAlmostEqual(res, 1.0)
856 self.assertIsNotNone(ex)
857 uops = get_opnames(ex)
858 guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
859 self.assertLessEqual(len(guard_both_float_count), 1)
860 # TODO gh-115506: this assertion may change after propagating constants.
861 # We'll also need to verify that propagation actually occurs.
862 self.assertIn("_BINARY_OP_MULTIPLY_FLOAT", uops)
863
864 def test_add_unicode_propagation(self):
865 def testfunc(n):
866 a = ""
867 for _ in range(n):
868 a + a
869 a + a
870 a + a
871 a + a
872 return a
873
874 res, ex = self._run_with_optimizer(testfunc, 32)
875 self.assertEqual(res, "")
876 self.assertIsNotNone(ex)
877 uops = get_opnames(ex)
878 guard_both_unicode_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_UNICODE"]
879 self.assertLessEqual(len(guard_both_unicode_count), 1)
880 self.assertIn("_BINARY_OP_ADD_UNICODE", uops)
881
882 def test_compare_op_type_propagation_float(self):
883 def testfunc(n):
884 a = 1.0
885 for _ in range(n):
886 x = a == a
887 x = a == a
888 x = a == a
889 x = a == a
890 return x
891
892 res, ex = self._run_with_optimizer(testfunc, 32)
893 self.assertTrue(res)
894 self.assertIsNotNone(ex)
895 uops = get_opnames(ex)
896 guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
897 self.assertLessEqual(len(guard_both_float_count), 1)
898 self.assertIn("_COMPARE_OP_FLOAT", uops)
899
900 def test_compare_op_type_propagation_int(self):
901 def testfunc(n):
902 a = 1
903 for _ in range(n):
904 x = a == a
905 x = a == a
906 x = a == a
907 x = a == a
908 return x
909
910 res, ex = self._run_with_optimizer(testfunc, 32)
911 self.assertTrue(res)
912 self.assertIsNotNone(ex)
913 uops = get_opnames(ex)
914 guard_both_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
915 self.assertLessEqual(len(guard_both_int_count), 1)
916 self.assertIn("_COMPARE_OP_INT", uops)
917
918 def test_compare_op_type_propagation_int_partial(self):
919 def testfunc(n):
920 a = 1
921 for _ in range(n):
922 if a > 2:
923 x = 0
924 if a < 2:
925 x = 1
926 return x
927
928 res, ex = self._run_with_optimizer(testfunc, 32)
929 self.assertEqual(res, 1)
930 self.assertIsNotNone(ex)
931 uops = get_opnames(ex)
932 guard_left_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_INT"]
933 guard_both_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
934 self.assertLessEqual(len(guard_left_int_count), 1)
935 self.assertEqual(len(guard_both_int_count), 0)
936 self.assertIn("_COMPARE_OP_INT", uops)
937
938 def test_compare_op_type_propagation_float_partial(self):
939 def testfunc(n):
940 a = 1.0
941 for _ in range(n):
942 if a > 2.0:
943 x = 0
944 if a < 2.0:
945 x = 1
946 return x
947
948 res, ex = self._run_with_optimizer(testfunc, 32)
949 self.assertEqual(res, 1)
950 self.assertIsNotNone(ex)
951 uops = get_opnames(ex)
952 guard_left_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_FLOAT"]
953 guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
954 self.assertLessEqual(len(guard_left_float_count), 1)
955 self.assertEqual(len(guard_both_float_count), 0)
956 self.assertIn("_COMPARE_OP_FLOAT", uops)
957
958 def test_compare_op_type_propagation_unicode(self):
959 def testfunc(n):
960 a = ""
961 for _ in range(n):
962 x = a == a
963 x = a == a
964 x = a == a
965 x = a == a
966 return x
967
968 res, ex = self._run_with_optimizer(testfunc, 32)
969 self.assertTrue(res)
970 self.assertIsNotNone(ex)
971 uops = get_opnames(ex)
972 guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_UNICODE"]
973 self.assertLessEqual(len(guard_both_float_count), 1)
974 self.assertIn("_COMPARE_OP_STR", uops)
975
976 def test_type_inconsistency(self):
977 ns = {}
978 src = textwrap.dedent("""
979 def testfunc(n):
980 for i in range(n):
981 x = _test_global + _test_global
982 """)
983 exec(src, ns, ns)
984 testfunc = ns['testfunc']
985 ns['_test_global'] = 0
986 _, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
987 self.assertIsNone(ex)
988 ns['_test_global'] = 1
989 _, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
990 self.assertIsNotNone(ex)
991 uops = get_opnames(ex)
992 self.assertNotIn("_GUARD_BOTH_INT", uops)
993 self.assertIn("_BINARY_OP_ADD_INT", uops)
994 # Try again, but between the runs, set the global to a float.
995 # This should result in no executor the second time.
996 ns = {}
997 exec(src, ns, ns)
998 testfunc = ns['testfunc']
999 ns['_test_global'] = 0
1000 _, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1001 self.assertIsNone(ex)
1002 ns['_test_global'] = 3.14
1003 _, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1004 self.assertIsNone(ex)
1005
1006 def test_combine_stack_space_checks_sequential(self):
1007 def dummy12(x):
1008 return x - 1
1009 def dummy13(y):
1010 z = y + 2
1011 return y, z
1012 def testfunc(n):
1013 a = 0
1014 for _ in range(n):
1015 b = dummy12(7)
1016 c, d = dummy13(9)
1017 a += b + c + d
1018 return a
1019
1020 res, ex = self._run_with_optimizer(testfunc, 32)
1021 self.assertEqual(res, 832)
1022 self.assertIsNotNone(ex)
1023
1024 uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
1025 uop_names = [uop[0] for uop in uops_and_operands]
1026 self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
1027 self.assertEqual(uop_names.count("_POP_FRAME"), 2)
1028 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
1029 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1030 # sequential calls: max(12, 13) == 13
1031 largest_stack = _testinternalcapi.get_co_framesize(dummy13.__code__)
1032 self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
1033
1034 def test_combine_stack_space_checks_nested(self):
1035 def dummy12(x):
1036 return x + 3
1037 def dummy15(y):
1038 z = dummy12(y)
1039 return y, z
1040 def testfunc(n):
1041 a = 0
1042 for _ in range(n):
1043 b, c = dummy15(2)
1044 a += b + c
1045 return a
1046
1047 res, ex = self._run_with_optimizer(testfunc, 32)
1048 self.assertEqual(res, 224)
1049 self.assertIsNotNone(ex)
1050
1051 uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
1052 uop_names = [uop[0] for uop in uops_and_operands]
1053 self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
1054 self.assertEqual(uop_names.count("_POP_FRAME"), 2)
1055 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
1056 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1057 # nested calls: 15 + 12 == 27
1058 largest_stack = (
1059 _testinternalcapi.get_co_framesize(dummy15.__code__) +
1060 _testinternalcapi.get_co_framesize(dummy12.__code__)
1061 )
1062 self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
1063
1064 def test_combine_stack_space_checks_several_calls(self):
1065 def dummy12(x):
1066 return x + 3
1067 def dummy13(y):
1068 z = y + 2
1069 return y, z
1070 def dummy18(y):
1071 z = dummy12(y)
1072 x, w = dummy13(z)
1073 return z, x, w
1074 def testfunc(n):
1075 a = 0
1076 for _ in range(n):
1077 b = dummy12(5)
1078 c, d, e = dummy18(2)
1079 a += b + c + d + e
1080 return a
1081
1082 res, ex = self._run_with_optimizer(testfunc, 32)
1083 self.assertEqual(res, 800)
1084 self.assertIsNotNone(ex)
1085
1086 uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
1087 uop_names = [uop[0] for uop in uops_and_operands]
1088 self.assertEqual(uop_names.count("_PUSH_FRAME"), 4)
1089 self.assertEqual(uop_names.count("_POP_FRAME"), 4)
1090 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
1091 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1092 # max(12, 18 + max(12, 13)) == 31
1093 largest_stack = (
1094 _testinternalcapi.get_co_framesize(dummy18.__code__) +
1095 _testinternalcapi.get_co_framesize(dummy13.__code__)
1096 )
1097 self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
1098
1099 def test_combine_stack_space_checks_several_calls_different_order(self):
1100 # same as `several_calls` but with top-level calls reversed
1101 def dummy12(x):
1102 return x + 3
1103 def dummy13(y):
1104 z = y + 2
1105 return y, z
1106 def dummy18(y):
1107 z = dummy12(y)
1108 x, w = dummy13(z)
1109 return z, x, w
1110 def testfunc(n):
1111 a = 0
1112 for _ in range(n):
1113 c, d, e = dummy18(2)
1114 b = dummy12(5)
1115 a += b + c + d + e
1116 return a
1117
1118 res, ex = self._run_with_optimizer(testfunc, 32)
1119 self.assertEqual(res, 800)
1120 self.assertIsNotNone(ex)
1121
1122 uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
1123 uop_names = [uop[0] for uop in uops_and_operands]
1124 self.assertEqual(uop_names.count("_PUSH_FRAME"), 4)
1125 self.assertEqual(uop_names.count("_POP_FRAME"), 4)
1126 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
1127 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1128 # max(18 + max(12, 13), 12) == 31
1129 largest_stack = (
1130 _testinternalcapi.get_co_framesize(dummy18.__code__) +
1131 _testinternalcapi.get_co_framesize(dummy13.__code__)
1132 )
1133 self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
1134
1135 def test_combine_stack_space_complex(self):
1136 def dummy0(x):
1137 return x
1138 def dummy1(x):
1139 return dummy0(x)
1140 def dummy2(x):
1141 return dummy1(x)
1142 def dummy3(x):
1143 return dummy0(x)
1144 def dummy4(x):
1145 y = dummy0(x)
1146 return dummy3(y)
1147 def dummy5(x):
1148 return dummy2(x)
1149 def dummy6(x):
1150 y = dummy5(x)
1151 z = dummy0(y)
1152 return dummy4(z)
1153 def testfunc(n):
1154 a = 0;
1155 for _ in range(32):
1156 b = dummy5(1)
1157 c = dummy0(1)
1158 d = dummy6(1)
1159 a += b + c + d
1160 return a
1161
1162 res, ex = self._run_with_optimizer(testfunc, 32)
1163 self.assertEqual(res, 96)
1164 self.assertIsNotNone(ex)
1165
1166 uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
1167 uop_names = [uop[0] for uop in uops_and_operands]
1168 self.assertEqual(uop_names.count("_PUSH_FRAME"), 15)
1169 self.assertEqual(uop_names.count("_POP_FRAME"), 15)
1170
1171 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
1172 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1173 largest_stack = (
1174 _testinternalcapi.get_co_framesize(dummy6.__code__) +
1175 _testinternalcapi.get_co_framesize(dummy5.__code__) +
1176 _testinternalcapi.get_co_framesize(dummy2.__code__) +
1177 _testinternalcapi.get_co_framesize(dummy1.__code__) +
1178 _testinternalcapi.get_co_framesize(dummy0.__code__)
1179 )
1180 self.assertIn(
1181 ("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands
1182 )
1183
1184 def test_combine_stack_space_checks_large_framesize(self):
1185 # Create a function with a large framesize. This ensures _CHECK_STACK_SPACE is
1186 # actually doing its job. Note that the resulting trace hits
1187 # UOP_MAX_TRACE_LENGTH, but since all _CHECK_STACK_SPACEs happen early, this
1188 # test is still meaningful.
1189 repetitions = 10000
1190 ns = {}
1191 header = """
1192 def dummy_large(a0):
1193 """
1194 body = "".join([f"""
1195 a{n+1} = a{n} + 1
1196 """ for n in range(repetitions)])
1197 return_ = f"""
1198 return a{repetitions-1}
1199 """
1200 exec(textwrap.dedent(header + body + return_), ns, ns)
1201 dummy_large = ns['dummy_large']
1202
1203 # this is something like:
1204 #
1205 # def dummy_large(a0):
1206 # a1 = a0 + 1
1207 # a2 = a1 + 1
1208 # ....
1209 # a9999 = a9998 + 1
1210 # return a9999
1211
1212 def dummy15(z):
1213 y = dummy_large(z)
1214 return y + 3
1215
1216 def testfunc(n):
1217 b = 0
1218 for _ in range(n):
1219 b += dummy15(7)
1220 return b
1221
1222 res, ex = self._run_with_optimizer(testfunc, 32)
1223 self.assertEqual(res, 32 * (repetitions + 9))
1224 self.assertIsNotNone(ex)
1225
1226 uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
1227 uop_names = [uop[0] for uop in uops_and_operands]
1228 self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
1229 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1230
1231 # this hits a different case during trace projection in refcount test runs only,
1232 # so we need to account for both possibilities
1233 self.assertIn(uop_names.count("_CHECK_STACK_SPACE"), [0, 1])
1234 if uop_names.count("_CHECK_STACK_SPACE") == 0:
1235 largest_stack = (
1236 _testinternalcapi.get_co_framesize(dummy15.__code__) +
1237 _testinternalcapi.get_co_framesize(dummy_large.__code__)
1238 )
1239 else:
1240 largest_stack = _testinternalcapi.get_co_framesize(dummy15.__code__)
1241 self.assertIn(
1242 ("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands
1243 )
1244
1245 def test_combine_stack_space_checks_recursion(self):
1246 def dummy15(x):
1247 while x > 0:
1248 return dummy15(x - 1)
1249 return 42
1250 def testfunc(n):
1251 a = 0
1252 for _ in range(n):
1253 a += dummy15(n)
1254 return a
1255
1256 res, ex = self._run_with_optimizer(testfunc, 32)
1257 self.assertEqual(res, 42 * 32)
1258 self.assertIsNotNone(ex)
1259
1260 uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
1261 uop_names = [uop[0] for uop in uops_and_operands]
1262 self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
1263 self.assertEqual(uop_names.count("_POP_FRAME"), 0)
1264 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 1)
1265 self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1266 largest_stack = _testinternalcapi.get_co_framesize(dummy15.__code__)
1267 self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
1268
1269 def test_many_nested(self):
1270 # overflow the trace_stack
1271 def dummy_a(x):
1272 return x
1273 def dummy_b(x):
1274 return dummy_a(x)
1275 def dummy_c(x):
1276 return dummy_b(x)
1277 def dummy_d(x):
1278 return dummy_c(x)
1279 def dummy_e(x):
1280 return dummy_d(x)
1281 def dummy_f(x):
1282 return dummy_e(x)
1283 def dummy_g(x):
1284 return dummy_f(x)
1285 def dummy_h(x):
1286 return dummy_g(x)
1287 def testfunc(n):
1288 a = 0
1289 for _ in range(n):
1290 a += dummy_h(n)
1291 return a
1292
1293 res, ex = self._run_with_optimizer(testfunc, 32)
1294 self.assertEqual(res, 32 * 32)
1295 self.assertIsNone(ex)
1296
1297 def test_return_generator(self):
1298 def gen():
1299 yield None
1300 def testfunc(n):
1301 for i in range(n):
1302 gen()
1303 return i
1304 res, ex = self._run_with_optimizer(testfunc, 20)
1305 self.assertEqual(res, 19)
1306 self.assertIsNotNone(ex)
1307 self.assertIn("_RETURN_GENERATOR", get_opnames(ex))
1308
1309 def test_for_iter_gen(self):
1310 def gen(n):
1311 for i in range(n):
1312 yield i
1313 def testfunc(n):
1314 g = gen(n)
1315 s = 0
1316 for i in g:
1317 s += i
1318 return s
1319 res, ex = self._run_with_optimizer(testfunc, 20)
1320 self.assertEqual(res, 190)
1321 self.assertIsNotNone(ex)
1322 self.assertIn("_FOR_ITER_GEN_FRAME", get_opnames(ex))
1323
1324 def test_modified_local_is_seen_by_optimized_code(self):
1325 l = sys._getframe().f_locals
1326 a = 1
1327 s = 0
1328 for j in range(1 << 10):
1329 a + a
1330 l["xa"[j >> 9]] = 1.0
1331 s += a
1332 self.assertIs(type(a), float)
1333 self.assertIs(type(s), float)
1334 self.assertEqual(s, 1024.0)
1335
1336
1337 if __name__ == "__main__":
1338 unittest.main()