Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7cd581a

Browse files
bpo-37269: Correctly optimise conditionals with constant booleans (GH-14071)
Fix a regression introduced byaf8646c that was causing code of the form:if True and False: do_something()to be optimized incorrectly, eliminating the block.(cherry picked from commit05f8318)Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
1 parent886d83e commit7cd581a

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

‎Lib/test/test_peepholer.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,13 @@ def forloop():
414414
pass
415415
self.assertEqual(count_instr_recursively(forloop,'BUILD_LIST'),0)
416416

417+
deftest_condition_with_binop_with_bools(self):
418+
deff():
419+
ifTrueorFalse:
420+
return1
421+
return0
422+
self.assertEqual(f(),1)
423+
417424

418425
classTestBuglets(unittest.TestCase):
419426

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug in the peephole optimizer that was not treating correctly constant
2+
conditions with binary operators. Patch by Pablo Galindo.

‎Python/peephole.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
315315
fill_nops(codestr,op_start,nexti+1);
316316
cumlc=0;
317317
}elseif (is_true==0) {
318+
if (i>1&&
319+
(_Py_OPCODE(codestr[i-1])==POP_JUMP_IF_TRUE||
320+
_Py_OPCODE(codestr[i-1])==POP_JUMP_IF_FALSE)) {
321+
break;
322+
}
318323
h=get_arg(codestr,nexti) /sizeof(_Py_CODEUNIT);
319324
tgt=find_op(codestr,codelen,h);
320325
fill_nops(codestr,op_start,tgt);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp