[pypy-svn] r9104 - pypy/branch/dist-interpapp/pypy/interpreter

hpk at codespeak.net hpk at codespeak.net
Fri Feb 11 13:25:44 CET 2005


Author: hpk
Date: Fri Feb 11 13:25:44 2005
New Revision: 9104

Modified:
   pypy/branch/dist-interpapp/pypy/interpreter/gateway.py
   pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py
Log:
- move printing code mostly to interp-level (with a few 
  appexec()s

- now gateway.appdef is used by default instead of gateway.app2interp! 
  which gives much nicer tracebacks among other things, 
  tests in interpreter pass, those in objspace/std not 
  because we can't share globals and slicetype.py 
  is very heavy in using that




Modified: pypy/branch/dist-interpapp/pypy/interpreter/gateway.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/gateway.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/gateway.py	Fri Feb 11 13:25:44 2005
@@ -676,7 +676,7 @@
     wfuncdecl, wfastscope, defaulthandlingsource = specialargparse(decl) 
     source = py.code.Source("""\
         def %s(space, %s):
-            # HERE we inject the defhandlingsource below 
+            # HERE we inject the defaultargs-handling below 
             pypyco = PyCode(space)._from_code(newco) 
             w_glob = space.newdict([])
             frame = pypyco.create_frame(space, w_glob) 
@@ -684,6 +684,7 @@
             return frame.run() 
     """ % (funcname, wfuncdecl, wfastscope))
     source.lines[1:2] = defaulthandlingsource.indent().lines 
+    print str(source)
     glob = {
         'newco' : newco, 
         'PyCode': PyCode, 
@@ -722,3 +723,5 @@
     wfuncdecl = ", ".join(wfuncargs) 
     wfastdecl = ", ".join(wfastnames)
     return wfuncdecl, wfastdecl, defaulthandlingsource 
+
+app2interp = appdef 

Modified: pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py	Fri Feb 11 13:25:44 2005
@@ -345,7 +345,7 @@
         w_globals = f.valuestack.pop()
         w_prog    = f.valuestack.pop()
         w_compile_flags = f.space.wrap(f.get_compile_flags())
-        w_resulttuple = f.prepare_exec(w_prog, w_globals, w_locals,
+        w_resulttuple = prepare_exec(f.space, f.space.wrap(f), w_prog, w_globals, w_locals,
                                        w_compile_flags)
         w_prog, w_globals, w_locals = f.space.unpacktuple(w_resulttuple, 3)
 
@@ -357,44 +357,6 @@
         if plain:
             f.setdictscope(w_locals)
 
-    def app_prepare_exec(f, prog, globals, locals, compile_flags):
-        """Manipulate parameters to exec statement to (codeobject, dict, dict).
-        """
-        # XXX INCOMPLETE
-        if (globals is None and locals is None and
-            isinstance(prog, tuple) and
-            (len(prog) == 2 or len(prog) == 3)):
-            globals = prog[1]
-            if len(prog) == 3:
-                locals = prog[2]
-            prog = prog[0]
-        if globals is None:
-            globals = f.f_globals
-            if locals is None:
-                locals = f.f_locals
-        if locals is None:
-            locals = globals
-        if not isinstance(globals, dict):
-            raise TypeError("exec: arg 2 must be a dictionary or None")
-        elif not globals.has_key('__builtins__'):
-            globals['__builtins__'] = f.f_builtins
-        if not isinstance(locals, dict):
-            raise TypeError("exec: arg 3 must be a dictionary or None")
-        # XXX - HACK to check for code object
-        co = compile('1','<string>','eval')
-        if isinstance(prog, type(co)):
-            return (prog, globals, locals)
-        if not isinstance(prog, str):
-    ##     if not (isinstance(prog, types.StringTypes) or
-    ##             isinstance(prog, types.FileType)):
-            raise TypeError("exec: arg 1 must be a string, file, or code object")
-    ##     if isinstance(prog, types.FileType):
-    ##         co = compile(prog.read(),prog.name,'exec',comple_flags,1)
-    ##         return (co,globals,locals)
-        else: # prog is a string
-            co = compile(prog,'<string>','exec', compile_flags, 1)
-            return (co, globals, locals)
-
     def POP_BLOCK(f):
         block = f.blockstack.pop()
         block.cleanup(f)  # the block knows how to clean up the value stack
@@ -785,44 +747,58 @@
 # There are also a couple of helpers that are methods, defined in the
 # class above.
 
-def app_print_expr(x):
-    try:
-        displayhook = sys.displayhook
-    except AttributeError:
-        raise RuntimeError("lost sys.displayhook")
-    displayhook(x)
-
-def app_file_softspace(file, newflag):
-    try:
-        softspace = file.softspace
-    except AttributeError:
-        softspace = 0
-    try:
-        file.softspace = newflag
-    except AttributeError:
-        pass
-    return softspace
-
-def app_sys_stdout():
-    try:
-        return sys.stdout
-    except AttributeError:
-        raise RuntimeError("lost sys.stdout")
+def print_expr(space, w_x): 
+    space.appexec([w_x], """
+        (x): 
+            try:
+                displayhook = sys.displayhook
+            except AttributeError:
+                raise RuntimeError("lost sys.displayhook")
+            displayhook(x)
+    """)
+
+def file_softspace(space, w_file, w_newflag): 
+    return space.appexec([w_file, w_newflag], """
+        (file, newflag):
+            try:
+                softspace = file.softspace
+            except AttributeError:
+                softspace = 0
+            try:
+                file.softspace = newflag
+            except AttributeError:
+                pass
+            return softspace
+    """)
+
+def sys_stdout(space): 
+    try: 
+        return space.getattr(space.w_sys, space.wrap('stdout'))
+    except OperationError, e: 
+        if not e.match(space, space.w_AttributeError): 
+            raise 
+        raise OperationError(space.w_RuntimeError, "lost sys.stdout")
+
+def print_item_to(space, w_x, w_stream):
+    if space.is_true(file_softspace(space, w_stream, space.w_False)): 
+       space.call_method(w_stream, 'write', space.wrap(" "))
+    space.call_method(w_stream, 'write', space.str(w_x))
 
-def app_print_item_to(x, stream):
-    if file_softspace(stream, False):
-        stream.write(" ")
-    stream.write(str(x))
     # add a softspace unless we just printed a string which ends in a '\t'
     # or '\n' -- or more generally any whitespace character but ' '
-    if isinstance(x, str) and len(x) and x[-1].isspace() and x[-1]!=' ':
-        return
+    w_skip = space.appexec([w_x], """ 
+        (x): 
+            return isinstance(x, str) and len(x) and \
+                   x[-1].isspace() and x[-1]!=' ' 
+    """) 
+    if space.is_true(w_skip): 
+        return 
     # XXX add unicode handling
-    file_softspace(stream, True)
+    file_softspace(space, w_stream, space.w_True)
 
-def app_print_newline_to(stream):
-    stream.write("\n")
-    file_softspace(stream, False)
+def print_newline_to(space, w_stream):
+    space.call_method(w_stream, 'write', space.wrap("\n"))
+    file_softspace(space, w_stream, space.w_False)
 
 def app_find_metaclass(bases, namespace, globals, builtins):
     if '__metaclass__' in namespace:
@@ -859,4 +835,42 @@
         into_locals[name] = getattr(module, name)
 
 
+def app_prepare_exec(f, prog, globals, locals, compile_flags):
+    """Manipulate parameters to exec statement to (codeobject, dict, dict).
+    """
+    # XXX INCOMPLETE
+    if (globals is None and locals is None and
+        isinstance(prog, tuple) and
+        (len(prog) == 2 or len(prog) == 3)):
+        globals = prog[1]
+        if len(prog) == 3:
+            locals = prog[2]
+        prog = prog[0]
+    if globals is None:
+        globals = f.f_globals
+        if locals is None:
+            locals = f.f_locals
+    if locals is None:
+        locals = globals
+    if not isinstance(globals, dict):
+        raise TypeError("exec: arg 2 must be a dictionary or None")
+    elif not globals.has_key('__builtins__'):
+        globals['__builtins__'] = f.f_builtins
+    if not isinstance(locals, dict):
+        raise TypeError("exec: arg 3 must be a dictionary or None")
+    # XXX - HACK to check for code object
+    co = compile('1','<string>','eval')
+    if isinstance(prog, type(co)):
+        return (prog, globals, locals)
+    if not isinstance(prog, str):
+##     if not (isinstance(prog, types.StringTypes) or
+##             isinstance(prog, types.FileType)):
+        raise TypeError("exec: arg 1 must be a string, file, or code object")
+##     if isinstance(prog, types.FileType):
+##         co = compile(prog.read(),prog.name,'exec',comple_flags,1)
+##         return (co,globals,locals)
+    else: # prog is a string
+        co = compile(prog,'<string>','exec', compile_flags, 1)
+        return (co, globals, locals)
+
 gateway.importall(globals())   # app_xxx() -> xxx()



More information about the Pypy-commit mailing list