[pypy-svn] r9166 - in pypy/branch/dist-interpapp/pypy: interpreter objspace/flow tool

arigo at codespeak.net arigo at codespeak.net
Sat Feb 12 16:39:37 CET 2005


Author: arigo
Date: Sat Feb 12 16:39:37 2005
New Revision: 9166

Modified:
   pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py
   pypy/branch/dist-interpapp/pypy/interpreter/gateway.py
   pypy/branch/dist-interpapp/pypy/interpreter/module.py
   pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py
   pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py
   pypy/branch/dist-interpapp/pypy/objspace/flow/objspace.py
   pypy/branch/dist-interpapp/pypy/objspace/flow/specialcase.py
   pypy/branch/dist-interpapp/pypy/tool/pydis.py
Log:
* slight additions to the interface to class applevel
* rewrite normalize_exception as an applevel
* these changes allow the flow objspace to special-case it again

* rewrite the app-level helpers of pyopcode.py as a single big
    applevel(), which looks very nice now (see e.g. the global 'import sys')

* random fixes in tool/pydis.py to make the test pass in this new world



Modified: pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py	Sat Feb 12 16:39:37 2005
@@ -280,7 +280,7 @@
 
     def appexec(self, posargs_w, source): 
         """ return value from executing given source at applevel.
-            The source must look like
+            EXPERIMENTAL. The source must look like
                '''(x, y):
                        do_stuff...
                        return result

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	Sat Feb 12 16:39:37 2005
@@ -606,6 +606,10 @@
         return space.loadfromcache(self, applevel.builddict,
                                    space._gatewaycache)
 
+    def buildmodule(self, space, name='applevel'):
+        from pypy.interpreter.module import Module
+        return Module(space, space.wrap(name), self.getdict(space))
+
     def builddict(self, space):
         "NOT_RPYTHON"
         w_glob = space.newdict([])
@@ -619,7 +623,13 @@
             w_func = space.getitem(w_glob, space.wrap(name))
             args = Arguments(space, args_w)
             return space.call_args(w_func, args)
-        return hack.func_with_new_name(appcaller, name)
+        def get_function(space):
+            w_glob = self.getdict(space)
+            w_func = space.getitem(w_glob, space.wrap(name))
+            return space.unwrap(w_func)
+        appcaller = hack.func_with_new_name(appcaller, name)
+        appcaller.get_function = get_function
+        return appcaller
 
 
 def appdef(source, applevel=applevel):

Modified: pypy/branch/dist-interpapp/pypy/interpreter/module.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/module.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/module.py	Sat Feb 12 16:39:37 2005
@@ -18,6 +18,9 @@
         if not space.is_true(space.contains(w_dict, space.wrap('__doc__'))):
             space.setitem(w_dict, space.wrap('__doc__'), space.w_None)
 
+    def get(self, name):
+        return self.space.getitem(self.w_dict, self.space.wrap(name))
+
     def getdict(self):
         return self.w_dict
 

Modified: pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py	Sat Feb 12 16:39:37 2005
@@ -216,44 +216,46 @@
             return True  # stop unrolling
         return False
 
-def app_normalize_exception(etype, value, tb):
-    """Normalize an (exc_type, exc_value) pair:
-    exc_value will be an exception instance and exc_type its class.
-    """
-    # mistakes here usually show up as infinite recursion, which is fun.
-    while isinstance(etype, tuple):
-        etype = etype[0]
-    if isinstance(etype, (type, _classobj)):
-        if not isinstance(value, etype):
-            if value is None:
-                # raise Type: we assume we have to instantiate Type
-                value = etype()
-            elif isinstance(value, tuple):
-                # raise Type, Tuple: assume Tuple contains the constructor args
-                value = etype(*value)
-            else:
-                # raise Type, X: assume X is the constructor argument
-                value = etype(value)
-        # raise Type, Instance: let etype be the exact type of value
-        etype = value.__class__
-    elif type(etype) is str:
-        # XXX warn -- deprecated
-        if value is not None and type(value) is not str:
-            raise TypeError("string exceptions can only have a string value")
-    else:
-        # raise X: we assume that X is an already-built instance
-        if value is not None:
-            raise TypeError("instance exception may not have a separate value")
-        value = etype
-        etype = value.__class__
-        # for the sake of language consistency we should not allow
-        # things like 'raise 1', but it's probably fine (i.e.
-        # not ambiguous) to allow them in the explicit form 'raise int, 1'
-        if not hasattr(value, '__dict__') and not hasattr(value, '__slots__'):
-            raise TypeError("raising built-in objects can be ambiguous, "
-                            "use 'raise type, value' instead")
-    return etype, value, tb
-normalize_exception = gateway.app2interp(app_normalize_exception)
+app = gateway.applevel('''
+    def normalize_exception(etype, value, tb):
+        """Normalize an (exc_type, exc_value) pair:
+        exc_value will be an exception instance and exc_type its class.
+        """
+        # mistakes here usually show up as infinite recursion, which is fun.
+        while isinstance(etype, tuple):
+            etype = etype[0]
+        if isinstance(etype, (type, _classobj)):
+            if not isinstance(value, etype):
+                if value is None:
+                    # raise Type: we assume we have to instantiate Type
+                    value = etype()
+                elif isinstance(value, tuple):
+                    # raise Type, Tuple: assume Tuple contains the constructor args
+                    value = etype(*value)
+                else:
+                    # raise Type, X: assume X is the constructor argument
+                    value = etype(value)
+            # raise Type, Instance: let etype be the exact type of value
+            etype = value.__class__
+        elif type(etype) is str:
+            # XXX warn -- deprecated
+            if value is not None and type(value) is not str:
+                raise TypeError("string exceptions can only have a string value")
+        else:
+            # raise X: we assume that X is an already-built instance
+            if value is not None:
+                raise TypeError("instance exception may not have a separate value")
+            value = etype
+            etype = value.__class__
+            # for the sake of language consistency we should not allow
+            # things like 'raise 1', but it is probably fine (i.e.
+            # not ambiguous) to allow them in the explicit form 'raise int, 1'
+            if not hasattr(value, '__dict__') and not hasattr(value, '__slots__'):
+                raise TypeError("raising built-in objects can be ambiguous, "
+                                "use 'raise type, value' instead")
+        return etype, value, tb
+''')
+normalize_exception = app.interphook("normalize_exception")
 
 
 class FinallyBlock(FrameBlock):

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	Sat Feb 12 16:39:37 2005
@@ -744,7 +744,38 @@
 # There are also a couple of helpers that are methods, defined in the
 # class above.
 
-app = gateway.applevel('''
+app = gateway.applevel(r'''
+
+    import sys
+
+    def sys_stdout(): 
+        try: 
+            return sys.stdout
+        except AttributeError:
+            raise RuntimeError("lost sys.stdout")
+
+    def print_expr(obj):
+        try:
+            displayhook = sys.displayhook
+        except AttributeError:
+            raise RuntimeError("lost sys.displayhook")
+        displayhook(obj)
+
+    def 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 x and x[-1].isspace() and x[-1]!=' ':
+            return 
+        # XXX add unicode handling
+        file_softspace(stream, True)
+
+    def print_newline_to(stream):
+        stream.write("\n")
+        file_softspace(stream, False)
 
     def file_softspace(file, newflag):
         try:
@@ -831,44 +862,11 @@
             return (co, globals, locals)
 ''')
 
+sys_stdout      = app.interphook('sys_stdout')
+print_expr      = app.interphook('print_expr')
+print_item_to   = app.interphook('print_item_to')
+print_newline_to= app.interphook('print_newline_to')
 file_softspace  = app.interphook('file_softspace')
 find_metaclass  = app.interphook('find_metaclass')
 import_all_from = app.interphook('import_all_from')
 prepare_exec    = app.interphook('prepare_exec')
-
-def print_expr(space, w_x): 
-    try:
-        w_displayhook = space.getattr(space.w_sys, space.wrap('displayhook'))
-    except OperationError, e:
-        if not e.match(space, space.w_AttributeError):
-            raise
-        raise OperationError(space.w_RuntimeError, "lost sys.displayhook")
-    space.call_function(w_displayhook, w_x)
-
-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))
-
-    # add a softspace unless we just printed a string which ends in a '\t'
-    # or '\n' -- or more generally any whitespace character but ' '
-    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(space, w_stream, space.w_True)
-
-def print_newline_to(space, w_stream):
-    space.call_method(w_stream, 'write', space.wrap("\n"))
-    file_softspace(space, w_stream, space.w_False)

Modified: pypy/branch/dist-interpapp/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/objspace/flow/objspace.py	(original)
+++ pypy/branch/dist-interpapp/pypy/objspace/flow/objspace.py	Sat Feb 12 16:39:37 2005
@@ -302,17 +302,6 @@
         self.handle_implicit_exceptions(exceptions)
         return w_res
 
-    def appexec(self, posargs_w, source):
-        name_end = source.find('(')
-        appname = source[:name_end].strip()
-        #print appname
-        try:
-            sc = self.specialcases[appname]
-        except KeyError:
-            return ObjSpace.appexec(self, posargs_w, source)
-        else:
-            return sc(self, posargs_w)
-
     def handle_implicit_exceptions(self, exceptions):
         if exceptions:
             # catch possible exceptions implicitly.  If the OperationError

Modified: pypy/branch/dist-interpapp/pypy/objspace/flow/specialcase.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/objspace/flow/specialcase.py	(original)
+++ pypy/branch/dist-interpapp/pypy/objspace/flow/specialcase.py	Sat Feb 12 16:39:37 2005
@@ -16,7 +16,7 @@
     return None
 
 
-def sc_normalize_exception(space, args):
+def sc_normalize_exception(space, fn, args):
     """Special-case for 'raise' statements.  Case-by-case analysis:
 
     * raise Class
@@ -34,7 +34,7 @@
        - assumes that Arg is the value you want for the exception, and
          that Class is exactly the exception class.  No check or normalization.
     """
-    w_arg1, w_arg2, w_tb = args
+    w_arg1, w_arg2, w_tb = args.fixedunpack(3)
 
     # w_arg3 (the traceback) is ignored and replaced with None
     # if it is a Variable, because pyopcode.py tries to unwrap it.
@@ -82,17 +82,10 @@
     opname = fn.__name__.replace('__', '')
     return space.do_operation(opname, *args_w)
 
-def skip_space(w_x):
-    pass
-
-def sc_skip_space(space, args):
-    return space.do_operation('simple_call', space.wrap("skip_space"), args[0])
-
 
 def setup(space):
-    fn = pyframe.normalize_exception
-    space.specialcases["app_normalize_exception"] = sc_normalize_exception
-    space.specialcases["app_skip_space"] = sc_skip_space
+    fn = pyframe.normalize_exception.get_function(space)
+    space.specialcases[fn] = sc_normalize_exception
     space.specialcases[__import__] = sc_import
     for opname in ['lt', 'le', 'eq', 'ne', 'gt', 'ge', 'is_']:
         space.specialcases[getattr(operator, opname)] = sc_operator

Modified: pypy/branch/dist-interpapp/pypy/tool/pydis.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/tool/pydis.py	(original)
+++ pypy/branch/dist-interpapp/pypy/tool/pydis.py	Sat Feb 12 16:39:37 2005
@@ -28,8 +28,7 @@
                 self.index == other.index and
                 self.op == other.op and
                 self.name == other.name and
-                self.oparg == other.oparg and
-                self.lineno == other.lineno)
+                self.oparg == other.oparg)
 
     def __ne__(self, other):
         return not (self == other)
@@ -44,7 +43,12 @@
         
         s = repr(oparg).rjust(5) + " "
         if op in hasconst:
-            s += '(' + `co.co_consts_w[oparg]` + ')'
+            # support both real code objects and PyCode objects
+            try:
+                consts = co.co_consts
+            except AttributeError:
+                consts = co.co_consts_w
+            s += '(' + `consts[oparg]` + ')'
         elif op in hasname:
             s +=  '(' + co.co_names[oparg] + ')'
         elif op in hasjrel:



More information about the Pypy-commit mailing list