Skip to content

Commit 002b033

Browse files
committed
Tier 2 translation, with test
This was remarkably straightforward
1 parent 7e71ddf commit 002b033

File tree

6 files changed

+160
-97
lines changed

6 files changed

+160
-97
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,7 +2443,6 @@ def testfunc(x):
24432443
i += 1
24442444

24452445
opt = _testinternalcapi.get_uop_optimizer()
2446-
24472446
with temporary_optimizer(opt):
24482447
testfunc(1000)
24492448

@@ -2580,13 +2579,33 @@ def testfunc(n):
25802579

25812580
ex = get_first_executor(testfunc)
25822581
self.assertIsNotNone(ex)
2583-
# for i, (opname, oparg) in enumerate(ex):
2584-
# print(f"{i:4d}: {opname:<20s} {oparg:4d}")
25852582
uops = {opname for opname, _ in ex}
25862583
# Since there is no JUMP_FORWARD instruction,
25872584
# look for indirect evidence: the += operator
25882585
self.assertIn("_BINARY_OP_ADD_INT", uops)
25892586

2587+
def test_for_iter_range(self):
2588+
def testfunc(n):
2589+
total = 0
2590+
for i in range(n):
2591+
total += i
2592+
return total
2593+
# import dis; dis.dis(testfunc)
2594+
2595+
opt = _testinternalcapi.get_uop_optimizer()
2596+
with temporary_optimizer(opt):
2597+
total = testfunc(10)
2598+
self.assertEqual(total, 45)
2599+
2600+
ex = get_first_executor(testfunc)
2601+
self.assertIsNotNone(ex)
2602+
# for i, (opname, oparg) in enumerate(ex):
2603+
# print(f"{i:4d}: {opname:<20s} {oparg:3d}")
2604+
uops = {opname for opname, _ in ex}
2605+
self.assertIn("_ITER_EXHAUSTED_RANGE", uops)
2606+
# Verification that the jump goes past END_FOR
2607+
# is done by manual inspection of the output
2608+
25902609

25912610
if __name__ == "__main__":
25922611
unittest.main()

Python/bytecodes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,13 @@ dummy_func(
24502450
}
24512451
}
24522452

2453+
// Only used by Tier 2
2454+
op(_ITER_EXHAUSTED_RANGE, (iter -- iter, exhausted)) {
2455+
_PyRangeIterObject *r = (_PyRangeIterObject *)iter;
2456+
assert(Py_TYPE(r) == &PyRangeIter_Type);
2457+
exhausted = r->len <= 0 ? Py_True : Py_False;
2458+
}
2459+
24532460
op(_ITER_NEXT_RANGE, (iter -- iter, next)) {
24542461
_PyRangeIterObject *r = (_PyRangeIterObject *)iter;
24552462
assert(Py_TYPE(r) == &PyRangeIter_Type);

Python/executor_cases.c.h

Lines changed: 43 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)