diff options
author | Raymond Hettinger <python@rcn.com> | 2005-02-20 12:46:54 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2005-02-20 12:46:54 (GMT) |
commit | afd842f5b262bfe5e2e2e53762d748b5da6caf2c (patch) | |
tree | 9f9a7b567735efff9ea61825e4fb875e47c89c8b | |
parent | 80121491e083a3e80d482f73d201590083285d57 (diff) | |
download | cpython-afd842f5b262bfe5e2e2e53762d748b5da6caf2c.zip cpython-afd842f5b262bfe5e2e2e53762d748b5da6caf2c.tar.gz cpython-afd842f5b262bfe5e2e2e53762d748b5da6caf2c.tar.bz2 |
Teach the peepholer to fold unary operations on constants.
Afterwards, -0.5 loads in a single step and no longer requires a runtime
UNARY_NEGATIVE operation.
-rw-r--r-- | Lib/test/test_peepholer.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 34bd99f..4385a84 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -133,6 +133,25 @@ class TestTranforms(unittest.TestCase): asm = dis_single('a="x"*1000') self.assert_('(1000)' in asm) + def test_folding_of_unaryops_on_constants(self): + for line, elem in ( + ('`1`', "('1')"), # unary convert + ('-0.5', '(-0.5)'), # unary negative + ('~-2', '(1)'), # unary invert + ): + asm = dis_single(line) + self.assert_(elem in asm, asm) + self.assert_('UNARY_' not in asm) + + # Verify that unfoldables are skipped + for line, elem in ( + ('-"abc"', "('abc')"), # unary negative + ('~"abc"', "('abc')"), # unary invert + ): + asm = dis_single(line) + self.assert_(elem in asm, asm) + self.assert_('UNARY_' in asm) + def test_elim_extra_return(self): # RETURN LOAD_CONST None RETURN --> RETURN def f(x): |