Commits

Tyler Wade committed 5e679b2

Implement skipping asserts when -O option is set

Comments (0)

Files changed (9)

lib-python/2.7/opcode.py

 hasname.append(201)
 def_op('CALL_METHOD', 202)            # #args not including 'self'
 def_op('BUILD_LIST_FROM_ARG', 203)
+jabs_op('JUMP_IF_NOT_DEBUG', 204)     # Target address
 
 del def_op, name_op, jrel_op, jabs_op

pypy/bin/pyinteractive.py

     BoolOption("completer", "use readline commandline completer",
                default=False, cmdline="-C"),
     BoolOption("optimize",
-               "remove docstrings when importing modules (like CPython -OO)",
+               "skip assert statements and remove docstrings when importing modules",
                default=False, cmdline="-O"),
     BoolOption("no_site_import", "do not 'import site' on initialization",
                default=False, cmdline="-S"),
     space = option.make_objspace(config)
 
     if interactiveconfig.optimize:
-        flags = space.sys.get('flags').getitems_copy()
+        flags_w = space.sys.get('flags').getitems_copy()
         #change optimize flag's value
-        flags[6] = space.wrap(2)
-        flags = type(space.sys.get('flags'))(flags)
-        flags.user_setup(space, space.sys.get('flags').w__class__)
-        space.sys.w_dict.setitem(space.wrap('flags'), flags)
+        import pdb; pdb.set_trace()
+        flags_w[6] = space.wrap(2)
+        w_flags = type(space.sys.get('flags'))(flags_w)
+        w_flags.user_setup(space, space.sys.get('flags').w__class__)
+        space.sys.w_dict.setitem(space.wrap('flags'), w_flags)
 
     space._starttime = starttime
     space.setitem(space.sys.w_dict, space.wrap('executable'),

pypy/doc/man/pypy.1.rst

     Inspect interactively after running script.
 
 -O
-    Dummy optimization flag for compatibility with C Python.
+    Skip assert statements.
 
 -OO
-    Remove docstrings when importing modules (like CPython -OO).
+    Remove docstrings when importing modules in addition to -O.
 
 -c *cmd*
     Program passed in as CMD (terminates option list).

pypy/interpreter/app_main.py

 """
 options:
   -i             inspect interactively after running script
-  -O             dummy optimization flag for compatibility with C Python
-  -OO            remove docstrings when importing modules (like CPython -OO)
+  -O             skip assert statements
+  -OO            remove docstrings when importing modules in addition to -O
   -c cmd         program passed in as CMD (terminates option list)
   -S             do not 'import site' on initialization
   -u             unbuffered binary stdout and stderr

pypy/interpreter/astcompiler/assemble.py

     ops.JUMP_IF_FALSE_OR_POP : 0,
     ops.POP_JUMP_IF_TRUE : -1,
     ops.POP_JUMP_IF_FALSE : -1,
+    ops.JUMP_IF_NOT_DEBUG : 0,
 
     ops.BUILD_LIST_FROM_ARG: 1,
 }

pypy/interpreter/astcompiler/codegen.py

     def visit_Assert(self, asrt):
         self.update_position(asrt.lineno)
         end = self.new_block()
+        self.emit_jump(ops.JUMP_IF_NOT_DEBUG, end, True)
         asrt.test.accept_jump_if(self, True, end)
         self.emit_op_name(ops.LOAD_GLOBAL, self.names, "AssertionError")
         if asrt.msg:

pypy/interpreter/astcompiler/test/test_compiler.py

         yield self.check, dict_w, "C4.__doc__", 'docstring'
         yield self.check, dict_w, "__doc__", None
 
+    def test_assert_skipping(self):
+        space = self.space
+        source = """if 1:
+        assert False
+        """
+        w_saved_flags = space.sys.get('flags')
+        flags_w = space.sys.get('flags').getitems_copy()
+        flags_w[6] = space.wrap(1)
+        w_flags = type(space.sys.get('flags'))(flags_w)
+        w_flags.user_setup(space, w_saved_flags.w__class__)
+        space.sys.w_dict.setitem(space.wrap('flags'), w_flags)
+        try:
+            self.run(source)
+        finally:
+            space.sys.w_dict.setitem(space.wrap('flags'), w_saved_flags)
+
+
 class AppTestCompiler:
 
     def test_docstring_not_loaded(self):

pypy/interpreter/pyopcode.py

         self.popvalue()
         return next_instr
 
+    def JUMP_IF_NOT_DEBUG(self, target, next_instr):
+        try:
+            optimize = self.space.sys.get_flag('optimize')
+        except:
+            optimize = 0
+        if optimize >= 1:
+            return target
+        return next_instr
+
     def GET_ITER(self, oparg, next_instr):
         w_iterable = self.popvalue()
         w_iterator = self.space.iter(w_iterable)

rpython/flowspace/flowcontext.py

         self.popvalue()
         return next_instr
 
+    def JUMP_IF_NOT_DEBUG(self, target, next_instr):
+        return next_instr
+
     def GET_ITER(self, oparg, next_instr):
         w_iterable = self.popvalue()
         w_iterator = self.space.iter(w_iterable)