Commit 5dec096e authored by Raymond Hettinger's avatar Raymond Hettinger

Maintain peepholer's cumlc invariant by updating the running total

everytime a LOAD_CONSTANT is encountered, created, or overwritten.

Added two tests to cover cases affected by the patch.
parent 7d112df9
...@@ -75,9 +75,11 @@ class TestTranforms(unittest.TestCase): ...@@ -75,9 +75,11 @@ class TestTranforms(unittest.TestCase):
def test_folding_of_tuples_of_constants(self): def test_folding_of_tuples_of_constants(self):
for line, elem in ( for line, elem in (
('a = 1,2,3', '((1, 2, 3))',), ('a = 1,2,3', '((1, 2, 3))'),
('("a","b","c")', "(('a', 'b', 'c'))",), ('("a","b","c")', "(('a', 'b', 'c'))"),
('a,b,c = 1,2,3', '((1, 2, 3))',), ('a,b,c = 1,2,3', '((1, 2, 3))'),
('(None, 1, None)', '((None, 1, None))'),
('((1, 2), 3, 4)', '(((1, 2), 3, 4))'),
): ):
asm = dis_single(line) asm = dis_single(line)
self.assert_(elem in asm) self.assert_(elem in asm)
......
...@@ -586,6 +586,7 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen ...@@ -586,6 +586,7 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
if (PyList_GET_ITEM(consts, j) == Py_None) { if (PyList_GET_ITEM(consts, j) == Py_None) {
codestr[i] = LOAD_CONST; codestr[i] = LOAD_CONST;
SETARG(codestr, i, j); SETARG(codestr, i, j);
cumlc = lastlc + 1;
break; break;
} }
} }
...@@ -601,6 +602,7 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen ...@@ -601,6 +602,7 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
!PyObject_IsTrue(PyList_GET_ITEM(consts, j))) !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
continue; continue;
memset(codestr+i, NOP, 7); memset(codestr+i, NOP, 7);
cumlc = 0;
break; break;
/* Try to fold tuples of constants. /* Try to fold tuples of constants.
...@@ -615,6 +617,8 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen ...@@ -615,6 +617,8 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
codestr[h] == LOAD_CONST && codestr[h] == LOAD_CONST &&
ISBASICBLOCK(blocks, h, 3*(j+1)) && ISBASICBLOCK(blocks, h, 3*(j+1)) &&
tuple_of_constants(&codestr[h], j, consts)) { tuple_of_constants(&codestr[h], j, consts)) {
assert(codestr[i] == LOAD_CONST);
cumlc = 1;
break; break;
} }
/* Intentional fallthrough */ /* Intentional fallthrough */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment