From 29dcaad6eb47d27fc2d450ae6d81cdf1d740be38 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 15 Mar 2011 14:50:16 -0700 Subject: [PATCH] Issue 11510: Fix BUILD_SET optimizer bug. --- Lib/test/test_peepholer.py | 14 +++++++++++++- Misc/NEWS | 2 ++ Python/peephole.c | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 531b4250775..b7d446fdd07 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -267,11 +267,23 @@ class TestTranforms(unittest.TestCase): asm = disassemble(f) self.assertNotIn('BINARY_ADD', asm) +class TestBuglets(unittest.TestCase): + + def test_bug_11510(self): + # folded constant set optimization was commingled with the tuple + # unpacking optimization which would fail if the set had duplicate + # elements so that the set length was unexpected + def f(): + x, y = {1, 1} + return x, y + with self.assertRaises(ValueError): + f() + def test_main(verbose=None): import sys from test import support - test_classes = (TestTranforms,) + test_classes = (TestTranforms, TestBuglets) support.run_unittest(*test_classes) # verify reference counting diff --git a/Misc/NEWS b/Misc/NEWS index 8b72498f68a..42330ff40da 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,8 @@ What's New in Python 3.2.1? Core and Builtins ----------------- +- Issue #11510: Fixed optimizer bug which turned "a,b={1,1}" into "a,b=(1,1)". + - Issue #11432: A bug was introduced in subprocess.Popen on posix systems with 3.2.0 where the stdout or stderr file descriptor being the same as the stdin file descriptor would raise an exception. webbrowser.open would fail. fixed. diff --git a/Python/peephole.c b/Python/peephole.c index f972e1611e6..69850439171 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -475,7 +475,8 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, } if (codestr[i+3] != UNPACK_SEQUENCE || !ISBASICBLOCK(blocks,i,6) || - j != GETARG(codestr, i+3)) + j != GETARG(codestr, i+3) || + opcode == BUILD_SET) continue; if (j == 1) { memset(codestr+i, NOP, 6);