[pypy-svn] rev 794 - pypy/trunk/src/pypy/objspace

mwh at codespeak.net mwh at codespeak.net
Mon Jun 9 14:11:56 CEST 2003


Author: mwh
Date: Mon Jun  9 14:11:56 2003
New Revision: 794

Modified:
   pypy/trunk/src/pypy/objspace/trivial.py
Log:
Bunch of changes to get the trivial object space working again.

The deepest: our builtins know nothing of old style classes.
Unfortunately, the trivial object space exported the Exceptions from
the underlying interpreter, which were old style classes.

So, like the std object space, clone the interpreters exception
heirachy as new-style classes.  This means we need to convert the
exceptions raised by the wrapped operations into our exceptions --
this is the point of the 'reraise' function.

Also, add __get__ to nufun, add a few object space methods that have
appeared recently and a check that we get them all and a few cosmetic
bits.

With these changes, the only failure in the test suite is due to 2.2.X
not supporting extended slices.


Modified: pypy/trunk/src/pypy/objspace/trivial.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/trivial.py	(original)
+++ pypy/trunk/src/pypy/objspace/trivial.py	Mon Jun  9 14:11:56 2003
@@ -11,6 +11,57 @@
 
 class TrivialObjSpace(ObjSpace):
 
+    def clone_exception_heirachy(self):
+        from pypy.interpreter.pycode import PyByteCode
+        def __init__(self, *args):
+            self.args = args
+        def __str__(self):
+            l = len(self.args)
+            if l == 0:
+                return ''
+            elif l == 1:
+                return str(self.args[0])
+            else:
+                return str(self.args)
+        import exceptions
+
+        # to create types, we should call the standard type object;
+        # but being able to do that depends on the existence of some
+        # of the exceptions...
+        
+        self.w_Exception = type('Exception', (),
+                                {'__init__':__init__, '__str__': __str__})
+        
+        done = {'Exception': self.w_Exception}
+
+        # some of the complexity of the following is due to the fact
+        # that we need to create the tree root first, but the only
+        # connections we have go in the inconvenient direction...
+        
+        for k in dir(exceptions):
+            if k not in done:
+                v = getattr(exceptions, k)
+                if isinstance(v, str):
+                    continue
+                stack = [k]
+                while stack:
+                    next = stack[-1]
+                    if next not in done:
+                        v = getattr(exceptions, next)
+                        b = v.__bases__[0]
+                        if b.__name__ not in done:
+                            stack.append(b.__name__)
+                            continue
+                        else:
+                            base = done[b.__name__]
+                            newtype = type(k, (base,), {})
+                            setattr(self, 'w_' + next, newtype)
+                            done[next] = newtype
+                            stack.pop()
+                    else:
+                        stack.pop()
+        return done
+
     def initialize(self):
         self.w_None = None
         self.w_True = True
@@ -21,11 +72,10 @@
                     "None" : self.w_None,
                     }
         for n, c in __builtin__.__dict__.iteritems():
-            if ((isinstance(c, types.ClassType) and issubclass(c, Exception)) or
-                isinstance(c, types.TypeType)):
-                w_c = c
-                setattr(self, 'w_' + c.__name__, w_c)
-                newstuff[c.__name__] = w_c
+            if isinstance(c, types.TypeType):
+                setattr(self, 'w_' + c.__name__, c)
+                newstuff[c.__name__] = c
+        newstuff.update(self.clone_exception_heirachy())
         self.make_builtins()
         self.make_sys()
         # insert these into the newly-made builtins
@@ -39,6 +89,23 @@
     def unwrap(self, w):
         return w
 
+    def reraise(self):
+        etype, evalue = sys.exc_info()[:2]
+        name = etype.__name__
+        if hasattr(self, 'w_' + name):
+            nt = getattr(self, 'w_' + name)
+            nv = object.__new__(nt)
+            if isinstance(evalue, etype):
+                nv.args = evalue.args
+            else:
+                print [etype, evalue, nt, nv], 
+                print '!!!!!!!!'
+                nv.args = (evalue,)
+        else:
+            nt = etype
+            nv = evalue
+        raise OperationError(nt, nv)
+
     # from the built-ins
     id        = id
     type      = type
@@ -66,18 +133,18 @@
         try:
             return getattr(obj, name)
         except:
-            raise OperationError(*sys.exc_info()[:2])
+            self.reraise()
 
     for _name in ('pos', 'neg', 'not_', 'abs', 'invert',
-                 'mul', 'truediv', 'floordiv', 'div', 'mod',
-                 'add', 'sub', 'lshift', 'rshift', 'and_', 'xor', 'or_',
+                  'mul', 'truediv', 'floordiv', 'div', 'mod',
+                  'add', 'sub', 'lshift', 'rshift', 'and_', 'xor', 'or_',
                   'lt', 'le', 'eq', 'ne', 'gt', 'ge', 'contains'):
         exec """
 def %(_name)s(self, *args):
     try:
         return operator.%(_name)s(*args)
     except:
-        raise OperationError(*sys.exc_info()[:2])
+        self.reraise()
 """ % locals()
 
     # in-place operators
@@ -146,7 +213,7 @@
             else:
                 return operator.getslice(obj, sindex[0], sindex[1])
         except:
-            raise OperationError(*sys.exc_info()[:2])
+            self.reraise()
 
     def setitem(self, w_obj, w_index, w_value):
         obj = self.unwrap(w_obj)
@@ -159,7 +226,7 @@
             else:
                 return operator.setslice(obj, sindex[0], sindex[1], value)
         except:
-            raise OperationError(*sys.exc_info()[:2])
+            self.reraise()
 
     def delitem(self, w_obj, w_index):
         obj = self.unwrap(w_obj)
@@ -171,7 +238,7 @@
             else:
                 operator.delslice(obj, sindex[0], sindex[1])
         except:
-            raise OperationError(*sys.exc_info()[:2])
+            self.reraise()
 
     # misc
     def next(self, w):
@@ -197,13 +264,16 @@
                     w_defaults = self.defaultarguments,
                     w_closure = self.closure)
                 return self.code.eval_code(self.space, self.globals, locals)
+            def __get__(self, ob, cls=None):
+                import new
+                return new.instancemethod(self, ob, cls)
         return nufun(self, code, globals, defaultarguments, closure)
 
     def newstring(self, asciilist):
         try:
             return ''.join([chr(ascii) for ascii in asciilist])
         except:
-            raise OperationError(*sys.exc_info()[:2])            
+            self.reraise()            
 
     def call(self, callable, args, kwds):
         if isinstance(callable, types.ClassType):
@@ -211,7 +281,7 @@
             try:
                 r = new.instance(callable)
             except:
-                raise OperationError(*sys.exc_info()[:2])
+                self.reraise()
             if hasattr(r, '__init__'):
                 self.call(r.__init__, args, kwds)
             return r
@@ -232,3 +302,38 @@
             return ec.eval_frame(frame)
         else:
             return apply(callable, args, kwds or {})
+
+    def hex(self, ob):
+        try:
+            return hex(ob)
+        except:
+            self.reraise()
+
+    def oct(self, ob):
+        try:
+            return oct(ob)
+        except:
+            self.reraise()
+
+    def ord(self, ob):
+        try:
+            return ord(ob)
+        except:
+            self.reraise()
+
+    def get(self, descr, ob, cls):
+        try:
+            return descr.__get__(ob, cls)
+        except:
+            self.reraise()
+
+    def new(self, type, args, kw):
+        return type(args, kw)
+
+    def init(self, type, args, kw):
+        pass
+
+
+for m in ObjSpace.MethodTable:
+    if not hasattr(TrivialObjSpace, m[0]):
+        print m[0]


More information about the Pypy-commit mailing list