Commits

Armin Rigo committed 170dfcb

generic wrapper done, TrivialObjectSpace is dead

Comments (0)

Files changed (3)

pypy/objspace/std/cpythonobject.py

 from pypy.objspace.std.objspace import *
 from stringobject import W_StringObject
-import sys, operator
+import sys, operator, types
 
 class W_CPythonObject:
     "This class wraps an arbitrary CPython object."
 StdObjSpace.unwrap.register(cpython_unwrap, W_CPythonObject)
 
 
+# real-to-wrapped exceptions
+def wrap_exception(space):
+    exc, value, tb = sys.exc_info()
+    raise OperationError(space.wrap(exc), space.wrap(value))
+
 # in-place operators
 def inplace_pow(x1, x2):
     x1 **= x2
                 try:
                     y = f(x)
                 except:
-                    raise OperationError(*sys.exc_info()[:2])
+                    wrap_exception(space)
                 return space.wrap(y)
         elif _arity == 2:
             def cpython_f(space, w_1, w_2, f=f):
                 try:
                     y = f(x1, x2)
                 except:
-                    raise OperationError(*sys.exc_info()[:2])
+                    wrap_exception(space)
                 return space.wrap(y)
         elif _arity == 3:
             def cpython_f(space, w_1, w_2, w_3, f=f):
                 try:
                     y = f(x1, x2, x3)
                 except:
-                    raise OperationError(*sys.exc_info()[:2])
+                    wrap_exception(space)
                 return space.wrap(y)
         else:
             raise ValueError, '_arity too large'
     try:
         return operator.truth(obj)
     except:
-        raise OperationError(*sys.exc_info()[:2])
+        wrap_exception(space)
 
 
 # slicing
         else:
             result = operator.getslice(obj, sindex[0], sindex[1])
     except:
-        raise OperationError(*sys.exc_info()[:2])
+        wrap_exception(space)
     return space.wrap(result)
 
 def cpython_setitem(space, w_obj, w_index, w_value):
         else:
             operator.setslice(obj, sindex[0], sindex[1], value)
     except:
-        raise OperationError(*sys.exc_info()[:2])
+        wrap_exception(space)
 
 def cpython_delitem(space, w_obj, w_index):
     obj = space.unwrap(w_obj)
         else:
             operator.delslice(obj, sindex[0], sindex[1])
     except:
-        raise OperationError(*sys.exc_info()[:2])
+        wrap_exception(space)
 
 StdObjSpace.getitem.register(cpython_getitem, W_CPythonObject, W_ANY)
 StdObjSpace.setitem.register(cpython_getitem, W_CPythonObject, W_ANY, W_ANY)
     except StopIteration:
         raise NoValue
     except:
-        raise OperationError(*sys.exc_info()[:2])
+        wrap_exception(space)
     return space.wrap(result)
 
 StdObjSpace.next.register(cpython_next, W_CPythonObject)
         result = apply(callable, args, keywords)
     except:
         import sys
-        raise OperationError(*sys.exc_info()[:2])
+        wrap_exception(space)
     return space.wrap(result)
 
 StdObjSpace.call.register(cpython_call, W_CPythonObject, W_ANY, W_ANY)

pypy/objspace/std/objspace.py

         if isinstance(x, str):
             import stringobject
             return stringobject.W_StringObject(x)
-        if isinstance(x, float):
-            import floatobject
-            return floatobject.W_FloatObject(x)
+        #if isinstance(x, float):
+        #    import floatobject
+        #    return floatobject.W_FloatObject(x)
         if isinstance(x, tuple):
             wrappeditems = [self.wrap(item) for item in x]
             import tupleobject

pypy/objspace/std/test/test_cpythonobject.py

 
     def setUp(self):
         self.space = StdObjSpace()
-        self.stuff = array.array('b')  # arbitrary always-wrapped stuff
+        # arbitrary always-wrapped object
+        self.stuff = array.array('b', [5,-2,77])
 
     def tearDown(self):
         pass
 
     def test_unary(self):
+        w1 = self.space.wrap(self.stuff)
         for op, expected in [
             ('id',     id(self.stuff)),
             ('type',   array.ArrayType),
+            ('len',    3),
+            ('repr',   "array('b', [5, -2, 77])"),
+            ('str',    "array('b', [5, -2, 77])"),
             ]:
-            w1 = self.space.wrap(self.stuff)
             w_result = getattr(self.space, op)(w1)
             self.assertEquals(self.space.unwrap(w_result), expected)
 
+    def test_binary(self):
+        w1 = self.space.wrap(self.stuff)
+        for op, w_arg, expected in [
+            ('getattr',   self.space.wrap('count'),      self.stuff.count),
+            ('getitem',   self.space.wrap(1),            -2),
+            ('getitem',   self.space.wrap(slice(1,2)),   array.array('b', [-2])),
+            ]:
+            w_result = getattr(self.space, op)(w1, w_arg)
+            self.assertEquals(self.space.unwrap(w_result), expected)
+
+    def test_unaryop(self):
+        w1 = self.space.wrap(3+4j)
+        for op, expected in [
+            ('pos',       3+4j),
+            ('neg',      -3-4j),
+            ('not_',     False),
+            ('abs',      5.0),
+            ('hash',     hash(3+4j)),
+            ]:
+            w_result = getattr(self.space, op)(w1)
+            self.assertEquals(self.space.unwrap(w_result), expected)
+
+    def test_binaryop(self):
+        w1 = self.space.wrap(3+4j)
+        w2 = self.space.wrap(1-2j)
+        for op, expected in [
+            ('add',      (3+4j) + (1-2j)),
+            ('sub',      (3+4j) - (1-2j)),
+            ('mul',      (3+4j) * (1-2j)),
+            ('div',      (3+4j) / (1-2j)),
+            ('eq',       False),
+            ('ne',       True),
+            ]:
+            w_result = getattr(self.space, op)(w1, w2)
+            self.assertEquals(self.space.unwrap(w_result), expected)
+
+    def test_hash(self):
+        w1 = self.space.wrap(self.stuff)
+        try:
+            self.space.hash(w1)
+        except OperationError, e:
+            self.assertEquals(e.w_type.cpyobj, TypeError)
+
+    def test_call(self):
+        w1 = self.space.wrap(len)
+        w_result = self.space.call(w1, self.space.wrap(("hello world",)),
+                                       self.space.wrap({}))
+        self.assertEquals(self.space.unwrap(w_result), 11)
+
 if __name__ == '__main__':
     unittest.main()