# The CPython Peephole Optimizer and You

DevFeed: [The CPython Peephole Optimizer and You](<https://devfeed.tech/articles/the-cpython-peephole-optimizer-and-you-29442.md>)

Original publisher: [Read original article](<http://akaptur.github.com/blog/2014/08/02/the-cpython-peephole-optimizer-and-you/>)

Published: 2014-08-02T18:25:00Z

Content type: tutorial

Language: en

Sources: [Allison Kaptur](<https://devfeed.tech/sources/allison-kaptur.md>)

Topics: [Compiler](<https://devfeed.tech/topics/compiler.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [test-coverage](<https://devfeed.tech/topics/test-coverage.md>)

Tags: [compiler](<https://devfeed.tech/tags/compiler.md>), [compiler-optimization](<https://devfeed.tech/tags/compiler-optimization.md>), [python](<https://devfeed.tech/tags/python.md>), [test-coverage](<https://devfeed.tech/tags/test-coverage.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

This article explains a surprising side effect of the CPython peephole compiler optimization through a small Python test coverage tool built with sys.settrace. It demonstrates tracing executed lines while running a simple test framework and identifies lines that were not executed.

## Source excerpt

Last Thursday I gave a lightning talk at Hacker School about the peephole optimizer in Python. A "peephole optimization" is a compiler optimization that looks at a small chunk of code at a time and optimizes in that little spot. This post explains one surprising side-effect of an optimization in CPython. Writing a test coverage tool Suppose that we're setting out to write a test coverage tool. Python provides an easy way to trace execution using sys.settrace, so a simple version of a coverage analyzer isn't too hard. Our code to test is one simple function: example.py 1 2 3 4 5 def iffer(condition): if condition: return 3 else: return 10 Then we'll write the world's simplest testing framework: tests.py 1 2 3 4 5 6 7 8 from example import iffer def test_iffer(): assert iffer(True) == 3 assert iffer(False) == 10 def run_tests(): test_iffer() Now for the simplest possible coverage tool. We can pass sys.settrace any tracing function, and it'll be called with the arguments frame, event, and arg every time an event happens in the execution. Lines of code being executed, function calls, function returns, and exceptions are all events. We'll filter out everything but line and call events, then keep track of what line of code was executing.1 Then we run the tests while the trace function is tracing, and finally report which (non-empty lines) failed to execute. coverage.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 import sys import tests import inspect class TinyCoverage(object): def __init__(self, file_to_watch): self.source_file = file_to_watch self.source_code = open(file_to_watch).readlines() self.executed_code = [] def trace(self, frame, event, arg): current_file = inspect.getframeinfo(frame).filename if self.source_file in current_file and \ (event == "line" or event == "call"): self.executed_code.append(frame.f_lineno) return self.trace def unexecuted_code(self): skipped = [] for line_n