Skip to content

Commit 0661e91

Browse files
committed
Issue 11510: Fix BUILD_SET optimizer bug.
1 parent 31f218b commit 0661e91

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎Lib/test/test_peepholer.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,23 @@ def test_constant_folding(self):
294294
self.assertNotIn('BINARY_', asm, e)
295295
self.assertNotIn('BUILD_', asm, e)
296296

297+
class TestBuglets(unittest.TestCase):
298+
299+
def test_bug_11510(self):
300+
# folded constant set optimization was commingled with the tuple
301+
# unpacking optimization which would fail if the set had duplicate
302+
# elements so that the set length was unexpected
303+
def f():
304+
x, y = {1, 1}
305+
return x, y
306+
with self.assertRaises(ValueError):
307+
f()
308+
297309

298310
def test_main(verbose=None):
299311
import sys
300312
from test import support
301-
test_classes = (TestTranforms,)
313+
test_classes = (TestTranforms, TestBuglets)
302314
support.run_unittest(*test_classes)
303315

304316
# verify reference counting

‎Python/peephole.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
535535
}
536536
if (codestr[i+3] != UNPACK_SEQUENCE ||
537537
!ISBASICBLOCK(blocks,i,6) ||
538-
j != GETARG(codestr, i+3))
538+
j != GETARG(codestr, i+3) ||
539+
opcode == BUILD_SET)
539540
continue;
540541
if (j == 1) {
541542
memset(codestr+i, NOP, 6);

0 commit comments

Comments
 (0)