Index: Python/peephole.c =================================================================== --- Python/peephole.c (revision 61983) +++ Python/peephole.c (working copy) @@ -179,7 +179,7 @@ { PyObject *newconst=NULL, *v; Py_ssize_t len_consts; - int opcode; + int opcode, err; /* Pre-conditions */ assert(PyList_CheckExact(consts)); @@ -194,12 +194,30 @@ if (PyObject_IsTrue(v) == 1) newconst = PyNumber_Negative(v); break; + case UNARY_POSITIVE: + newconst = PyNumber_Positive(v); + break; case UNARY_CONVERT: newconst = PyObject_Repr(v); break; case UNARY_INVERT: newconst = PyNumber_Invert(v); break; + case UNARY_NOT: + err = PyObject_IsTrue(v); + switch (err) { + case 1: + Py_INCREF(Py_False); + newconst = Py_False; + break; + case 0: + Py_INCREF(Py_True); + newconst = Py_True; + break; + default: + newconst = NULL; + } + break; default: /* Called with an unknown opcode */ PyErr_Format(PyExc_SystemError, @@ -351,10 +369,10 @@ if (codestr[i+1] != JUMP_IF_FALSE || codestr[i+4] != POP_TOP || !ISBASICBLOCK(blocks,i,5)) - continue; + goto fold_unary_op; tgt = GETJUMPTGT(codestr, (i+1)); if (codestr[tgt] != POP_TOP) - continue; + goto fold_unary_op; j = GETARG(codestr, i+1) + 1; codestr[i] = JUMP_IF_TRUE; SETARG(codestr, i, j); @@ -479,8 +497,10 @@ /* Fold unary ops on constants. LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */ case UNARY_NEGATIVE: + case UNARY_POSITIVE: case UNARY_CONVERT: case UNARY_INVERT: + fold_unary_op: if (lastlc >= 1 && ISBASICBLOCK(blocks, i-3, 4) && fold_unaryops_on_constants(&codestr[i-3], consts)) { Index: Lib/test/test_peepholer.py =================================================================== --- Lib/test/test_peepholer.py (revision 61983) +++ Lib/test/test_peepholer.py (working copy) @@ -142,7 +142,9 @@ for line, elem in ( ('`1`', "('1')"), # unary convert ('-0.5', '(-0.5)'), # unary negative + ('+0.5', '(0.5)'), # unary positive ('~-2', '(1)'), # unary invert + ('not 2', '(False)'), # unary not ): asm = dis_single(line) self.assert_(elem in asm, asm)