Skip to content

Commit 1373bd1

Browse files
committed
fix bug
1 parent 2e3e76e commit 1373bd1

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,19 @@ def testfunc(n):
460460
uops = get_opnames(ex)
461461
self.assertIn(self.guard_is_false, uops)
462462

463+
def test_branch_coincident_targets(self):
464+
# test for gh-144681: https://github.com/python/cpython/issues/144681
465+
def testfunc(n):
466+
for _ in range(n):
467+
r = [x for x in range(10) if [].append(x) or True]
468+
return r
469+
470+
res = testfunc(TIER2_THRESHOLD)
471+
ex = get_first_executor(testfunc)
472+
473+
self.assertEqual(res, list(range(10)))
474+
self.assertIsNotNone(ex)
475+
463476
def test_for_iter_tier_two(self):
464477
class MyIter:
465478
def __init__(self, n):

Python/optimizer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,12 @@ _PyJit_translate_single_bytecode_to_trace(
800800
_Py_CODEUNIT *computed_next_instr = computed_next_instr_without_modifiers + (computed_next_instr_without_modifiers->op.code == NOT_TAKEN);
801801
_Py_CODEUNIT *computed_jump_instr = computed_next_instr_without_modifiers + oparg;
802802
assert(next_instr == computed_next_instr || next_instr == computed_jump_instr);
803-
int jump_happened = computed_jump_instr == next_instr;
803+
int jump_happened;
804+
if (computed_next_instr == computed_jump_instr) {
805+
jump_happened = target_instr[1].cache & 1;
806+
} else {
807+
jump_happened = computed_jump_instr == next_instr;
808+
}
804809
assert(jump_happened == (target_instr[1].cache & 1));
805810
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_happened];
806811
ADD_TO_TRACE(uopcode, 0, 0, INSTR_IP(jump_happened ? computed_next_instr : computed_jump_instr, old_code));

0 commit comments

Comments
 (0)