The issue seems to be that CPython is optimizing the locals access for the exec_code_object_and_return_x by using indices into the array, instead of the normal lookup by name (which, by incident, is what the compiled code uses).<p>If I understand <a href="http://bugs.python.org/issue4831" rel="nofollow">http://bugs.python.org/issue4831</a> correctly, CPython 2.7 should not be optimizing the locals access in this way with an exec statement present.
Speaking of CPython optimizations<p>> x = 124236<p>> y = 124237<p>> y -= 1<p>> id(x)<p>140474467900408<p>> id(y)<p>140474467900360<p>> x is y<p>False<p>> x = 1<p>> y = 2<p>> y -= 1<p>> id(x)<p>140558102245816<p>> id(y)<p>140558102245816<p>> x is y<p>True