[pypy-svn] r4929 - in pypy/branch/src-newobjectmodel/pypy: interpreter objspace/std

hpk at codespeak.net hpk at codespeak.net
Fri Jun 4 22:31:46 CEST 2004


Author: hpk
Date: Fri Jun  4 22:31:45 2004
New Revision: 4929

Modified:
   pypy/branch/src-newobjectmodel/pypy/interpreter/gateway.py
   pypy/branch/src-newobjectmodel/pypy/objspace/std/cpythonobject.py
   pypy/branch/src-newobjectmodel/pypy/objspace/std/listobject.py
   pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py
Log:
- introduced special wrappers for cpython- and cpython-builtin functions 
  which inherit from the interpreter Function objects

- print stuff as it gets cpython-wrapped 

- some cleanups here and there    
 


Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/gateway.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/gateway.py	(original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/gateway.py	Fri Jun  4 22:31:45 2004
@@ -184,8 +184,9 @@
                 self.code.func)
         space = obj.space
         fn = self.get_function(space)
+        w_obj = space.wrap(obj)
         return Method(space, space.wrap(fn),
-                      space.wrap(obj), space.wrap(obj.__class__))
+                      w_obj, space.type(w_obj)) 
 
 
 class app2interp(Gateway):

Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/cpythonobject.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/cpythonobject.py	(original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/cpythonobject.py	Fri Jun  4 22:31:45 2004
@@ -1,19 +1,35 @@
 """
-Reviewed 03-06-21
-This is an extremely general object, so the tests are
-a carefully choosen sample, rather than a complete coverage.
-Exception wrapping is tested and ok.
-Inplace operators are untested, but quite obvious by inspection.
-Some operators and functions (both unary and binary) are tested.
-Inspection of table MethodImplementations is necessary and
-sufficient to ensure completeness and correctness of this module.
 """
 
 from pypy.objspace.std.objspace import *
+from pypy.interpreter.function import Function
 from stringobject import W_StringObject
 from intobject import W_IntObject
 import sys, operator, types
 
+class W_BuiltinFunctionObject(Function):
+    """This class wraps cpython-functions much like ordinary 'Function' objects 
+       but it avoids wrapping them into CPythonObject which would go badly 
+       with the descroperations. """ 
+
+    def __init__(self, space, cpyfunc):
+        assert callable(cpyfunc), cpyfunc
+        self.space = space
+        self.cpyfunc = cpyfunc
+
+    def call(self, w_args, w_kwds):
+        space = self.space
+        args = space.unwrap(w_args)
+        kwds = {}
+        keys_w = space.unpackiterable(w_kwds)
+        for w_key in keys_w:
+            kwds[space.unwrap(w_key)] = space.unwrap(space.getitem(w_kwds, w_key))
+        try:
+            result = apply(self.cpyfunc, args, kwds)
+        except:
+            wrap_exception(space)
+        return space.wrap(result)
+
 class W_CPythonObject(W_Object):
     "This class wraps an arbitrary CPython object."
     
@@ -173,23 +189,18 @@
     f = MethodImplementation.get(_name)
     if f:
         if _arity == 1:
-            def cpython_f(space, w_1, f=f, pypymethod='pypy_'+_name):
+            def cpython_f(space, w_1, f=f): 
                 x1 = w_1.cpyobj 
                 type_x1 = type(x1)
-                if hasattr(type_x1, pypymethod):
-                    return getattr(type_x1, pypymethod)(x1)
                 try:
                     y = f(x1)
                 except:
                     wrap_exception(space)
                 return space.wrap(y)
         elif _arity == 2:
-            def cpython_f(space, w_1, w_2, f=f, pypymethod='pypy_'+_name):
+            def cpython_f(space, w_1, w_2, f=f): 
                 x1 = w_1.cpyobj 
                 type_x1 = type(x1)
-                if hasattr(type_x1, pypymethod):
-                    return getattr(type_x1, pypymethod)(x1, w_2)
-
                 # XXX do we really want to unwrap unknown objects here? 
                 x2 = space.unwrap(w_2)
                 try:
@@ -198,12 +209,9 @@
                     wrap_exception(space)
                 return space.wrap(y)
         elif _arity == 3:
-            def cpython_f(space, w_1, w_2, w_3, f=f, pypymethod='pypy_'+_name):
+            def cpython_f(space, w_1, w_2, w_3, f=f): 
                 x1 = w_1.cpyobj 
                 type_x1 = type(x1)
-                if hasattr(type_x1, pypymethod):
-                    return getattr(type_x1, pypymethod)(x1, w_2, w_3)
-
                 x2 = space.unwrap(w_2)
                 x3 = space.unwrap(w_3)
                 try:
@@ -231,7 +239,6 @@
                 return space.wrap(y)
             multimethod.register(cpython_f_rev, W_ANY, W_CPythonObject)
 
-
 def is_true__CPython(space, w_obj):
     obj = space.unwrap(w_obj)
     try:
@@ -293,8 +300,6 @@
 
 def next__CPython(space, w_obj):
     obj = space.unwrap(w_obj)
-    if hasattr(obj, 'pypy_next'):
-        return obj.pypy_next()
     try:
         result = obj.next()
     except:
@@ -304,8 +309,6 @@
 def call__CPython(space, w_obj, w_arguments, w_keywords):
     # XXX temporary hack similar to objspace.trivial.call()
     callable = space.unwrap(w_obj)
-    if hasattr(callable, 'pypy_call'):
-        return callable.pypy_call(w_arguments, w_keywords)
     args = space.unwrap(w_arguments)
     keywords = space.unwrap(w_keywords)
     try:

Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/listobject.py	(original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/listobject.py	Fri Jun  4 22:31:45 2004
@@ -252,14 +252,8 @@
         items[start+i*step] = sequence2[i]
     return space.w_None
 
-def repr__List(space, w_list):
-    w = space.wrap
-    a = space.add
-    reprs_w = map(space.repr, space.unpackiterable(w_list))
-    from pypy.objspace.std.stringtype import W_StringType
-    w_bm = space.getattr(space.wrap(', '), space.wrap('join'))
-    return a(a(w('['), space.call_function(w_bm, space.newlist(reprs_w))), w(']'))
-    return space.newstring([])
+def app_repr__List(l):
+    return "[" + ", ".join([repr(x) for x in l]) + ']'
 
 def hash__List(space,w_list):
     raise OperationError(space.w_TypeError,space.wrap("list objects are unhashable"))
@@ -534,5 +528,7 @@
 };
 """
 
+from pypy.interpreter import gateway
+gateway.importall(globals())
 from pypy.objspace.std import listtype
 register_all(vars(), listtype)

Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py	(original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py	Fri Jun  4 22:31:45 2004
@@ -2,6 +2,7 @@
 from pypy.interpreter.baseobjspace import *
 from pypy.objspace.std.multimethod import *
 from pypy.objspace.descroperation import DescrOperation
+import types
 
 
 class W_Object:
@@ -139,7 +140,7 @@
                         "None" : self.w_None,
                         "NotImplemented": self.w_NotImplemented,
                         "Ellipsis": self.w_Ellipsis,
-                        "long": self.wrap(long),  # XXX temporary
+#                        "long": self.wrap(long),  # XXX temporary
                         }
 
         # types
@@ -204,8 +205,14 @@
             return listobject.W_ListObject(self, wrappeditems)
         if isinstance(x, Wrappable):
             return x.__spacebind__(self)
-        #print "wrapping %r (%s)" % (x, type(x))
         import cpythonobject
+        SlotWrapperType = type(type(None).__repr__)
+        if isinstance(x, (types.FunctionType, types.BuiltinFunctionType, SlotWrapperType)):
+            return cpythonobject.W_BuiltinFunctionObject(self, x) 
+        print "cpython wrapping %r (%s)" % (x, type(x))
+        #if hasattr(x, '__bases__'): 
+        #    print "cpython wrapping a class %r (%s)" % (x, type(x))
+            #raise TypeError, "cannot wrap classes"
         return cpythonobject.W_CPythonObject(self, x)
 
     def newint(self, int_w):
@@ -247,7 +254,11 @@
         return stringobject.W_StringObject(self, ''.join(chars))
 
     def type(self, w_obj):
-        if hasattr(w_obj, 'w__class__'):
+        from pypy.objspace.std.cpythonobject import W_CPythonObject
+        if isinstance(w_obj, W_CPythonObject):
+            #raise TypeError, str(w_obj.cpyobj)
+            return self.wrap(type(w_obj.cpyobj))
+        elif hasattr(w_obj, 'w__class__'):
             return w_obj.w__class__    # user-defined classes
         else:
             assert w_obj.typedef, w_obj



More information about the Pypy-commit mailing list