[pypy-svn] r9315 - in pypy/branch/dist-interpapp/pypy: interpreter interpreter/test objspace/std tool

pedronis at codespeak.net pedronis at codespeak.net
Fri Feb 18 18:52:52 CET 2005


Author: pedronis
Date: Fri Feb 18 18:52:52 2005
New Revision: 9315

Modified:
   pypy/branch/dist-interpapp/pypy/interpreter/gateway.py
   pypy/branch/dist-interpapp/pypy/interpreter/pycode.py
   pypy/branch/dist-interpapp/pypy/interpreter/test/test_gateway.py
   pypy/branch/dist-interpapp/pypy/interpreter/typedef.py
   pypy/branch/dist-interpapp/pypy/objspace/std/dicttype.py
   pypy/branch/dist-interpapp/pypy/objspace/std/listtype.py
   pypy/branch/dist-interpapp/pypy/objspace/std/objecttype.py
   pypy/branch/dist-interpapp/pypy/objspace/std/stdtypedef.py
   pypy/branch/dist-interpapp/pypy/objspace/std/typetype.py
   pypy/branch/dist-interpapp/pypy/tool/pytestsupport.py
Log:
now except for the simplest cases inter2app unwrap_spec is mandatory

if an unbound method is used the its class is used for the unwrap_spec of the self argument

when explicitly specifying the unwrap_spec for an unbound method the placeholder 'self' should be used




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 18 18:52:52 2005
@@ -15,7 +15,7 @@
 from pypy.interpreter.error import OperationError 
 from pypy.interpreter import eval
 from pypy.interpreter.function import Function, Method
-from pypy.interpreter.baseobjspace import W_Root,ObjSpace,Wrappable
+from pypy.interpreter.baseobjspace import W_Root,ObjSpace, BaseWrappable, Wrappable
 from pypy.interpreter.argument import Arguments
 from pypy.tool.cache import Cache 
 # internal non-translatable parts: 
@@ -55,7 +55,7 @@
 
 class UnwrapSpecRecipe:
 
-    bases_order = [W_Root, ObjSpace, Arguments, object]
+    bases_order = [BaseWrappable, W_Root, ObjSpace, Arguments, object]
 
     def dispatch(self, meth_family, el, orig_sig, new_sig):
         if isinstance(el,str):
@@ -78,14 +78,19 @@
     # checks for checking interp2app func argument names wrt unwrap_spec
     # and synthetizing an app-level signature
 
-    def check__ObjSpace(self, el, orig_sig, new_sig):
-        orig_sig.next_arg()
-
-    def check_self(self, el, orig_sig, app_sig): # xxx
+    def check__BaseWrappable(self, el, orig_sig, app_sig):
+        name = el.__name__
         argname = orig_sig.next_arg()
+        assert not argname.startswith('w_'), (
+            "unwrapped %s argument %s of built-in function %r should "
+            "not start with 'w_'" % (name, argname, orig_sig.func))
         app_sig.append(argname)
+        
+    def check__ObjSpace(self, el, orig_sig, app_sig):
+        orig_sig.next_arg()
 
     def check__W_Root(self, el, orig_sig, app_sig):
+        assert el is W_Root, "oops"
         argname = orig_sig.next_arg()
         assert argname.startswith('w_'), (
             "argument %s of built-in function %r should "
@@ -138,15 +143,24 @@
 
     # collect code to emit for interp2app builtin frames based on unwrap_spec
 
-    def emit__ObjSpace(self, el, orig_sig, emit_sig):
-        emit_sig.run_args.append('self.space')
-
-    def emit_self(self, el, orig_sig, emit_sig): # xxx
+    def emit__BaseWrappable(self, el, orig_sig, emit_sig):
+        name = el.__name__
+        cur = emit_sig.through_scope_w
         emit_sig.setfastscope.append(
-            "self.self_arg = self.space.interpclass_w(scope_w[%d])" %
-                (emit_sig.through_scope_w))
+            "x = self.space.interpclass_w(scope_w[%d])" % cur)
+        emit_sig.setfastscope.append(
+            "if x is None or not isinstance(x, %s):" % name)
+        emit_sig.setfastscope.append(
+            "    raise OperationError(self.space.w_TypeError,space.wrap('unexpected arg type'))") # xxx
+        emit_sig.miniglobals[name] = el
+        emit_sig.miniglobals['OperationError'] = OperationError
+        emit_sig.setfastscope.append(
+            "self.%s_arg%d = x" % (name,cur))
         emit_sig.through_scope_w += 1
-        emit_sig.run_args.append("self.self_arg")
+        emit_sig.run_args.append("self.%s_arg%d" % (name,cur))
+
+    def emit__ObjSpace(self, el, orig_sig, emit_sig):
+        emit_sig.run_args.append('self.space')
 
     def emit__W_Root(self, el, orig_sig, emit_sig):
         cur = emit_sig.through_scope_w
@@ -267,27 +281,19 @@
     # When a BuiltinCode is stored in a Function object,
     # you get the functionality of CPython's built-in function type.
 
-    def __init__(self, func, ismethod=None, spacearg=None, unwrap_spec = None):
+    def __init__(self, func, ismethod=None, spacearg=None, unwrap_spec = None, self_type = None):
         "NOT_RPYTHON"
         # 'implfunc' is the interpreter-level function.
         # Note that this uses a lot of (construction-time) introspection.
         eval.Code.__init__(self, func.__name__)
         self.docstring = func.__doc__
-        # signature-based hacks if unwrap_spec is not specified:
-        # renaming arguments from w_xyz to xyz.
-        # Currently we enforce the following signature tricks:
-        #  * the first arg must be either 'self' or 'space'
-        #  * 'w_' prefixes for the rest
-        #  * '_w' suffix for the optional '*' argument
-        #  * alternatively a final '__args__' means an Arguments()
-        # Not exactly a clean approach XXX.
-        # --
+
         # unwrap_spec can be passed to interp2app or
         # attached as an attribute to the function.
         # It is a list of types or singleton objects:
         #  baseobjspace.ObjSpace is used to specify the space argument
-        #  'self' is used to specify a self method argument
         #  baseobjspace.W_Root is for wrapped arguments to keep wrapped
+        #  baseobjspace.BaseWrappable subclasses imply interpclass_w and a typecheck
         #  argument.Arguments is for a final rest arguments Arguments object
         # 'args_w' for unpacktuple applied to rest arguments
         # 'w_args' for rest arguments passed as wrapped tuple
@@ -297,49 +303,25 @@
         from pypy.interpreter import pycode
         argnames, varargname, kwargname = pycode.cpython_code_signature(func.func_code)
 
+        assert not ismethod, ("ismethod is not expected anymore")
+        assert not spacearg, ("spacearg is not expected anymore")
+
         if unwrap_spec is None:
             unwrap_spec = getattr(func,'unwrap_spec',None)
-        
-        if unwrap_spec is None:
 
-            unwrap_spec = []
+        if unwrap_spec is None and argnames == ['space', '__args__']: #xxx for geninterp
+            unwrap_spec = [ObjSpace, Arguments]
 
-            argnames = list(argnames)
-            lookslikemethod = argnames[:1] == ['self']
-            if ismethod is None:
-                ismethod = lookslikemethod
-            if spacearg is None:
-                spacearg = not lookslikemethod
-            self.ismethod = ismethod
-            self.spacearg = spacearg
-            assert kwargname is None, (
-                "built-in function %r should not take a ** argument" % func)
-
-            n = len(argnames)
-
-            if self.ismethod:
-                unwrap_spec.append('self')
-                n -= 1
-            if self.spacearg:
-                unwrap_spec.append(ObjSpace)
-                n -= 1
-
-            self.generalargs = argnames[-1:] == ['__args__']
-            self.starargs = varargname is not None
-
-            if self.generalargs:
-                unwrap_spec.extend([W_Root] * (n-1))
-                unwrap_spec.append(Arguments)
-            else:
-                unwrap_spec.extend([W_Root] * n)
+        if unwrap_spec is None:
+            unwrap_spec = [ObjSpace]+ [W_Root] * (len(argnames)-1)
 
-            if self.starargs:
-                unwrap_spec.append('starargs')
-        else:
-            assert not ismethod, ("if unwrap_spec is specified, "
-                                  "ismethod is not expected")
-            assert not spacearg, ("if unwrap_spec is specified, " 
-                                  "spacearg is not expected")
+            if self_type:
+                unwrap_spec = ['self'] + unwrap_spec[1:]
+            
+        if self_type:
+            assert unwrap_spec[0] == 'self',"self_type without 'self' spec element"
+            unwrap_spec = list(unwrap_spec)
+            unwrap_spec[0] = self_type
 
         orig_sig = Signature(func, argnames, varargname, kwargname)
 
@@ -385,6 +367,10 @@
         "NOT_RPYTHON"
         Wrappable.__init__(self)
         # f must be a function whose name does NOT starts with 'app_'
+        self_type = None
+        if hasattr(f, 'im_func'):
+            self_type = f.im_class
+            f = f.im_func
         if not isinstance(f, types.FunctionType):
             raise TypeError, "function expected, got %r instead" % f
         if app_name is None:
@@ -394,7 +380,8 @@
             app_name = f.func_name
         self._code = BuiltinCode(f, ismethod=ismethod,
                                   spacearg=spacearg,
-                                  unwrap_spec=unwrap_spec)
+                                  unwrap_spec=unwrap_spec,
+                                  self_type = self_type)
         self.__name__ = f.func_name
         self.name = app_name
         self._staticdefs = list(f.func_defaults or ())
@@ -450,7 +437,6 @@
             fn = Function(space, code, None, defs, forcename = self.name)
             cache.content[self] = fn 
             return fn
-
         
 def exportall(d, temporary=False):
     """NOT_RPYTHON: Publish every function from a dict."""

Modified: pypy/branch/dist-interpapp/pypy/interpreter/pycode.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/pycode.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/pycode.py	Fri Feb 18 18:52:52 2005
@@ -159,8 +159,7 @@
         self = space.interpclass_w(w_self)
         return space.newtuple([space.wrap(name) for name in self.co_names])
 
-    def descr_code__eq__(space, w_self, w_other):
-        self = space.interpclass_w(w_self)
+    def descr_code__eq__(self, w_other):
         other = space.interpclass_w(w_other)
         if not isinstance(other, PyCode):
             return space.w_False

Modified: pypy/branch/dist-interpapp/pypy/interpreter/test/test_gateway.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/test/test_gateway.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/test/test_gateway.py	Fri Feb 18 18:52:52 2005
@@ -7,15 +7,22 @@
     def test_signature(self):
         def c(space, w_x, w_y, *hello_w):
             pass
-        code = gateway.BuiltinCode(c)
+        code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace,
+                                                   gateway.W_Root,
+                                                   gateway.W_Root,
+                                                   'starargs'])
         assert code.signature() == (['x', 'y'], 'hello', None)
         def d(self, w_boo):
             pass
-        code = gateway.BuiltinCode(d)
+        code = gateway.BuiltinCode(d, unwrap_spec= ['self',
+                                                   gateway.W_Root], self_type=gateway.BaseWrappable)
         assert code.signature() == (['self', 'boo'], None, None)
         def e(space, w_x, w_y, __args__):
             pass
-        code = gateway.BuiltinCode(e)
+        code = gateway.BuiltinCode(e, unwrap_spec=[gateway.ObjSpace,
+                                                   gateway.W_Root,
+                                                   gateway.W_Root,
+                                                   gateway.Arguments])
         assert code.signature() == (['x', 'y'], 'args', 'keywords')
 
     def test_call(self):
@@ -26,7 +33,10 @@
             assert u(hello_w[0]) == 0
             assert u(hello_w[1]) == True
             return w((u(w_x) - u(w_y) + len(hello_w)))
-        code = gateway.BuiltinCode(c)
+        code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace,
+                                                   gateway.W_Root,
+                                                   gateway.W_Root,
+                                                   'starargs'])
         w = self.space.wrap
         w_dict = self.space.newdict([
             (w('x'), w(123)),
@@ -43,7 +53,10 @@
             w = space.wrap
             return w((u(w_x) - u(w_y) + len(args_w))
                      * u(kwds_w['boo']))
-        code = gateway.BuiltinCode(c)
+        code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace,
+                                                   gateway.W_Root,
+                                                   gateway.W_Root,
+                                                   gateway.Arguments])
         w = self.space.wrap
         w_dict = self.space.newdict([
             (w('x'), w(123)),

Modified: pypy/branch/dist-interpapp/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/typedef.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/typedef.py	Fri Feb 18 18:52:52 2005
@@ -2,7 +2,7 @@
 
 
 """
-from pypy.interpreter.gateway import interp2app 
+from pypy.interpreter.gateway import interp2app, ObjSpace, Arguments, W_Root 
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.error import OperationError
 from pypy.tool.cache import Cache
@@ -247,7 +247,7 @@
 
 PyCode.typedef = TypeDef('code',
     __new__ = interp2app(PyCode.descr_code__new__.im_func),
-    __eq__ = interp2app(PyCode.descr_code__eq__.im_func),
+    __eq__ = interp2app(PyCode.descr_code__eq__),
     co_argcount = interp_attrproperty('co_argcount'),
     co_nlocals = interp_attrproperty('co_nlocals'),
     co_stacksize = interp_attrproperty('co_stacksize'),
@@ -270,8 +270,9 @@
     **Frame.typedef.rawdict)
 
 Module.typedef = TypeDef("module",
-    __new__ = interp2app(Module.descr_module__new__.im_func),
-    __init__ = interp2app(Module.descr_module__init__.im_func),
+    __new__ = interp2app(Module.descr_module__new__.im_func,
+                         unwrap_spec=[ObjSpace, W_Root, Arguments]),
+    __init__ = interp2app(Module.descr_module__init__),
     __dict__ = GetSetProperty(descr_get_dict), # module dictionaries are readonly attributes
     )
 
@@ -297,8 +298,9 @@
 getset_func_dict = GetSetProperty(descr_get_dict, descr_set_dict)
 
 Function.typedef = TypeDef("function",
-    __call__ = interp2app(Function.descr_function_call.im_func),
-    __get__ = interp2app(Function.descr_function_get.im_func),
+    __call__ = interp2app(Function.descr_function_call,
+                          unwrap_spec=['self', Arguments]),
+    __get__ = interp2app(Function.descr_function_get),
     func_code = getset_func_code, 
     func_doc = getset_func_doc,
     func_name = interp_attrproperty('name'), 
@@ -315,17 +317,18 @@
 
 Method.typedef = TypeDef("method",
     __new__ = interp2app(Method.descr_method__new__.im_func),
-    __call__ = interp2app(Method.descr_method_call.im_func),
-    __get__ = interp2app(Method.descr_method_get.im_func),
+    __call__ = interp2app(Method.descr_method_call,
+                          unwrap_spec=['self', Arguments]),
+    __get__ = interp2app(Method.descr_method_get),
     im_func  = interp_attrproperty_w('w_function'), 
     im_self  = interp_attrproperty_w('w_instance'), 
     im_class = interp_attrproperty_w('w_class'),
-    __getattribute__ = interp2app(Method.descr_method_getattribute.im_func),
+    __getattribute__ = interp2app(Method.descr_method_getattribute),
     # XXX getattribute/setattribute etc.pp 
     )
 
 StaticMethod.typedef = TypeDef("staticmethod",
-    __get__ = interp2app(StaticMethod.descr_staticmethod_get.im_func),
+    __get__ = interp2app(StaticMethod.descr_staticmethod_get),
     # XXX getattribute etc.pp
     )
 
@@ -337,8 +340,8 @@
     )
 
 GeneratorIterator.typedef = TypeDef("generator",
-    next       = interp2app(GeneratorIterator.descr_next.im_func),
-    __iter__   = interp2app(GeneratorIterator.descr__iter__.im_func),
+    next       = interp2app(GeneratorIterator.descr_next),
+    __iter__   = interp2app(GeneratorIterator.descr__iter__),
     gi_running = interp_attrproperty('running'), 
     gi_frame   = interp_attrproperty('frame'), 
 )
@@ -346,11 +349,11 @@
 Cell.typedef = TypeDef("cell")
 
 Ellipsis.typedef = TypeDef("Ellipsis", 
-    __repr__   = interp2app(Ellipsis.descr__repr__.im_func),
+    __repr__   = interp2app(Ellipsis.descr__repr__),
 )
 
 NotImplemented.typedef = TypeDef("NotImplemented", 
-    __repr__   = interp2app(NotImplemented.descr__repr__.im_func), 
+    __repr__   = interp2app(NotImplemented.descr__repr__), 
 )
 
 ControlFlowException.typedef = TypeDef("ControlFlowException")

Modified: pypy/branch/dist-interpapp/pypy/objspace/std/dicttype.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/objspace/std/dicttype.py	(original)
+++ pypy/branch/dist-interpapp/pypy/objspace/std/dicttype.py	Fri Feb 18 18:52:52 2005
@@ -96,6 +96,7 @@
 # ____________________________________________________________
 
 dict_typedef = StdTypeDef("dict",
-    __new__ = newmethod(descr__new__),
+    __new__ = newmethod(descr__new__,
+                        unwrap_spec=[gateway.ObjSpace,gateway.W_Root,gateway.Arguments]),
     )
 dict_typedef.registermethods(globals())

Modified: pypy/branch/dist-interpapp/pypy/objspace/std/listtype.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/objspace/std/listtype.py	(original)
+++ pypy/branch/dist-interpapp/pypy/objspace/std/listtype.py	Fri Feb 18 18:52:52 2005
@@ -37,6 +37,8 @@
 # ____________________________________________________________
 
 list_typedef = StdTypeDef("list",
-    __new__ = newmethod(descr__new__),
+    __new__ = newmethod(descr__new__, unwrap_spec=[gateway.ObjSpace,
+                                                   gateway.W_Root,
+                                                   gateway.Arguments]),
     )
 list_typedef.registermethods(globals())

Modified: pypy/branch/dist-interpapp/pypy/objspace/std/objecttype.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/objspace/std/objecttype.py	(original)
+++ pypy/branch/dist-interpapp/pypy/objspace/std/objecttype.py	Fri Feb 18 18:52:52 2005
@@ -47,6 +47,8 @@
     __str__ = gateway.interp2app(descr__str__),
     __repr__ = gateway.interp2app(descr__repr__),
     __class__ = GetSetProperty(descr__class__),
-    __new__ = newmethod(descr__new__),
-    __init__ = gateway.interp2app(descr__init__),
+    __new__ = newmethod(descr__new__,
+                        unwrap_spec = [gateway.ObjSpace,gateway.W_Root,gateway.Arguments]),
+    __init__ = gateway.interp2app(descr__init__,
+                                  unwrap_spec=[gateway.ObjSpace,gateway.W_Root,gateway.Arguments]),
     )

Modified: pypy/branch/dist-interpapp/pypy/objspace/std/stdtypedef.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/objspace/std/stdtypedef.py	(original)
+++ pypy/branch/dist-interpapp/pypy/objspace/std/stdtypedef.py	Fri Feb 18 18:52:52 2005
@@ -55,10 +55,10 @@
 
 std_dict_descr = GetSetProperty(descr_get_dict, descr_set_dict)
 
-def newmethod(descr_new):
+def newmethod(descr_new, unwrap_spec=None):
     "NOT_RPYTHON: initialization-time only."
     # this is turned into a static method by the constructor of W_TypeObject.
-    return gateway.interp2app(descr_new)
+    return gateway.interp2app(descr_new, unwrap_spec=unwrap_spec)
 
 # ____________________________________________________________
 #

Modified: pypy/branch/dist-interpapp/pypy/objspace/std/typetype.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/objspace/std/typetype.py	(original)
+++ pypy/branch/dist-interpapp/pypy/objspace/std/typetype.py	Fri Feb 18 18:52:52 2005
@@ -78,5 +78,5 @@
     __mro__ = GetSetProperty(descr_get__mro__),
     __dict__ = dictproxy_descr,
     __doc__ = GetSetProperty(descr__doc),
-    mro = newmethod(descr_mro),
+    mro = gateway.interp2app(descr_mro),
     )

Modified: pypy/branch/dist-interpapp/pypy/tool/pytestsupport.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/tool/pytestsupport.py	(original)
+++ pypy/branch/dist-interpapp/pypy/tool/pytestsupport.py	Fri Feb 18 18:52:52 2005
@@ -2,7 +2,7 @@
 import autopath
 import py
 from py.__impl__.magic import exprinfo
-from pypy.interpreter.gateway import interp2app_temp
+from pypy.interpreter import gateway
 from pypy.interpreter.error import OperationError
 
 # ____________________________________________________________
@@ -107,7 +107,10 @@
     w_BuiltinAssertionError = space.getitem(space.builtin.w_dict, 
                                             space.wrap('AssertionError'))
     w_metaclass = space.type(w_BuiltinAssertionError)
-    w_init = space.wrap(interp2app_temp(my_init))
+    w_init = space.wrap(gateway.interp2app_temp(my_init,
+                                                unwrap_spec=[gateway.ObjSpace,
+                                                             gateway.W_Root,
+                                                             gateway.Arguments]))
     w_dict = space.newdict([])
     space.setitem(w_dict, space.wrap('__init__'), w_init)
     return space.call_function(w_metaclass,
@@ -149,14 +152,18 @@
     raise OperationError(space.w_AssertionError,
                          space.wrap("DID NOT RAISE"))
 
-app_raises = interp2app_temp(pypyraises)
+app_raises = gateway.interp2app_temp(pypyraises,
+                                     unwrap_spec=[gateway.ObjSpace,
+                                                  gateway.W_Root,
+                                                  gateway.W_Root,
+                                                  gateway.Arguments])
 
 def pypyskip(space, w_message): 
     """skip a test at app-level. """ 
     msg = space.unwrap(w_message) 
     py.test.skip(msg)
 
-app_skip = interp2app_temp(pypyskip)
+app_skip = gateway.interp2app_temp(pypyskip)
 
 def raises_w(space, w_ExpectedException, *args, **kwds):
     try:



More information about the Pypy-commit mailing list