Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29dcaad

Browse files
committed
Issue 11510: Fix BUILD_SET optimizer bug.
1 parent 729c5e2 commit 29dcaad

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

Lib/test/test_peepholer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,23 @@ def g()->1+1:
267267
asm = disassemble(f)
268268
self.assertNotIn('BINARY_ADD', asm)
269269

270+
class TestBuglets(unittest.TestCase):
271+
272+
def test_bug_11510(self):
273+
# folded constant set optimization was commingled with the tuple
274+
# unpacking optimization which would fail if the set had duplicate
275+
# elements so that the set length was unexpected
276+
def f():
277+
x, y = {1, 1}
278+
return x, y
279+
with self.assertRaises(ValueError):
280+
f()
281+
270282

271283
def test_main(verbose=None):
272284
import sys
273285
from test import support
274-
test_classes = (TestTranforms,)
286+
test_classes = (TestTranforms, TestBuglets)
275287
support.run_unittest(*test_classes)
276288

277289
# verify reference counting

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.2.1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #11510: Fixed optimizer bug which turned "a,b={1,1}" into "a,b=(1,1)".
14+
1315
- Issue #11432: A bug was introduced in subprocess.Popen on posix systems with
1416
3.2.0 where the stdout or stderr file descriptor being the same as the stdin
1517
file descriptor would raise an exception. webbrowser.open would fail. fixed.

Python/peephole.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
475475
}
476476
if (codestr[i+3] != UNPACK_SEQUENCE ||
477477
!ISBASICBLOCK(blocks,i,6) ||
478-
j != GETARG(codestr, i+3))
478+
j != GETARG(codestr, i+3) ||
479+
opcode == BUILD_SET)
479480
continue;
480481
if (j == 1) {
481482
memset(codestr+i, NOP, 6);

0 commit comments

Comments
 (0)