[pypy-commit] pypy translation-cleanup: Refactor direct calls to app-level RPython from flow space

rlamy noreply at buildbot.pypy.org
Thu Sep 27 19:29:18 CEST 2012


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: translation-cleanup
Changeset: r57632:acef92897543
Date: 2012-09-27 18:28 +0100
http://bitbucket.org/pypy/pypy/changeset/acef92897543/

Log:	Refactor direct calls to app-level RPython from flow space

	* Replace sc_applevel with FlowObjSpace.appcall()
	* Simplify PRINT_ITEM and PRINT_NEWLINE
	* Remove flowspace-specific hacks in pypy.interpreter.gateway

diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -943,14 +943,6 @@
         def appcaller(space, *args_w):
             if not isinstance(space, ObjSpace):
                 raise TypeError("first argument must be a space instance.")
-            # redirect if the space handles this specially
-            # XXX can this be factored a bit less flow space dependently?
-            if hasattr(space, 'specialcases'):
-                sc = space.specialcases
-                if ApplevelClass in sc:
-                    ret_w = sc[ApplevelClass](space, self, name, args_w)
-                    if ret_w is not None: # it was RPython
-                        return ret_w
             # the last argument can be an Arguments
             w_func = self.wget(space, name)
             if not args_w:
diff --git a/pypy/objspace/flow/flowcontext.py b/pypy/objspace/flow/flowcontext.py
--- a/pypy/objspace/flow/flowcontext.py
+++ b/pypy/objspace/flow/flowcontext.py
@@ -10,7 +10,8 @@
 from pypy.objspace.flow.framestate import (FrameState, recursively_unflatten,
         recursively_flatten)
 from pypy.objspace.flow.bytecode import HostCode
-from pypy.objspace.flow.specialcase import sc_applevel
+from pypy.objspace.flow.specialcase import (rpython_print_item,
+        rpython_print_newline)
 
 class FlowingError(Exception):
     """ Signals invalid RPython in the function being analysed"""
@@ -588,10 +589,8 @@
 
     def PRINT_ITEM(self, oparg, next_instr):
         w_item = self.popvalue()
-        self.print_item(w_item)
-
-    def print_item(self, *args):
-        return sc_applevel(self.space, 'print_item', args)
+        w_s = self.space.do_operation('str', w_item)
+        self.space.appcall(rpython_print_item, w_s)
 
     def PRINT_NEWLINE_TO(self, oparg, next_instr):
         w_stream = self.popvalue()
@@ -600,10 +599,7 @@
         print_newline_to(self.space, w_stream)
 
     def PRINT_NEWLINE(self, oparg, next_instr):
-        self.print_newline()
-
-    def print_newline(self):
-        return sc_applevel(self.space, 'print_newline', [])
+        self.space.appcall(rpython_print_newline)
 
     def FOR_ITER(self, jumpby, next_instr):
         w_iterator = self.peekvalue()
diff --git a/pypy/objspace/flow/objspace.py b/pypy/objspace/flow/objspace.py
--- a/pypy/objspace/flow/objspace.py
+++ b/pypy/objspace/flow/objspace.py
@@ -451,10 +451,14 @@
         return self.call_function(w_meth, *arg_w)
 
     def call_function(self, w_func, *args_w):
-        nargs = len(args_w)
         args = argument.ArgumentsForTranslation(self, list(args_w))
         return self.call_args(w_func, args)
 
+    def appcall(self, func, *args_w):
+        """Call an app-level RPython function directly"""
+        w_func = self.wrap(func)
+        return self.do_operation('simple_call', w_func, *args_w)
+
     def call_args(self, w_callable, args):
         try:
             fn = self.unwrap(w_callable)
diff --git a/pypy/objspace/flow/specialcase.py b/pypy/objspace/flow/specialcase.py
--- a/pypy/objspace/flow/specialcase.py
+++ b/pypy/objspace/flow/specialcase.py
@@ -1,6 +1,5 @@
 from pypy.objspace.flow.model import Constant, UnwrapException
 from pypy.objspace.flow.operation import OperationName, Arity
-from pypy.interpreter.gateway import ApplevelClass
 from pypy.interpreter.error import OperationError
 from pypy.tool.cache import Cache
 from pypy.rlib.rarithmetic import r_uint
@@ -35,11 +34,13 @@
 class StdOutBuffer:
     linebuf = []
 stdoutbuffer = StdOutBuffer()
+
 def rpython_print_item(s):
     buf = stdoutbuffer.linebuf
     for c in s:
         buf.append(c)
     buf.append(' ')
+
 def rpython_print_newline():
     buf = stdoutbuffer.linebuf
     if buf:
@@ -51,18 +52,6 @@
     import os
     os.write(1, s)
 
-def sc_applevel(space, name, args_w):
-    # special case only for print_item and print_newline
-    if name == 'print_item':
-        w_s = space.do_operation('str', *args_w)
-        args_w = (w_s,)
-    elif name == 'print_newline':
-        pass
-    else:
-        raise Exception("not RPython: calling %r from %r" % (name, app))
-    func = globals()['rpython_' + name]
-    return space.do_operation('simple_call', Constant(func), *args_w)
-
 # _________________________________________________________________________
 
 def sc_r_uint(space, r_uint, args):
@@ -79,8 +68,8 @@
 def sc_we_are_translated(space, we_are_translated, args):
     return Constant(True)
 
-SPECIAL_CASES = {__import__: sc_import, ApplevelClass: sc_applevel,
-        r_uint: sc_r_uint, we_are_translated: sc_we_are_translated}
+SPECIAL_CASES = {__import__: sc_import, r_uint: sc_r_uint,
+        we_are_translated: sc_we_are_translated}
 for fn in OperationName:
     SPECIAL_CASES[fn] = sc_operator
 


More information about the pypy-commit mailing list