[pypy-svn] r34169 - in pypy/branch/transparent-proxy/pypy: interpreter objspace/std/test

fijal at codespeak.net fijal at codespeak.net
Sat Nov 4 15:23:36 CET 2006


Author: fijal
Date: Sat Nov  4 15:23:33 2006
New Revision: 34169

Modified:
   pypy/branch/transparent-proxy/pypy/interpreter/argument.py
   pypy/branch/transparent-proxy/pypy/interpreter/typedef.py
   pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy_function.py
Log:
(samuele, fijal) - Implementation of function.


Modified: pypy/branch/transparent-proxy/pypy/interpreter/argument.py
==============================================================================
--- pypy/branch/transparent-proxy/pypy/interpreter/argument.py	(original)
+++ pypy/branch/transparent-proxy/pypy/interpreter/argument.py	Sat Nov  4 15:23:33 2006
@@ -350,6 +350,11 @@
         "Return a ([w1,w2...], {'kw':w3...}) pair."
         self._unpack()
         return self.arguments_w, self.kwds_w
+    
+    def popfirst(self):
+        self._unpack()
+        return self.arguments_w[0], Arguments(self.space, self.arguments_w[1:],
+            kwds_w = self.kwds_w)
 
     def _unpack(self):
         "unpack the *arg and **kwd into w_arguments and kwds_w"
@@ -418,7 +423,7 @@
             if not e.match(self.space, self.space.w_StopIteration):
                 raise
             return None
-
+        
     ###  Parsing for function calls  ###
 
     def _match_signature(self, scope_w, argnames, has_vararg=False,

Modified: pypy/branch/transparent-proxy/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/transparent-proxy/pypy/interpreter/typedef.py	(original)
+++ pypy/branch/transparent-proxy/pypy/interpreter/typedef.py	Sat Nov  4 15:23:33 2006
@@ -5,7 +5,8 @@
 import py
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.argument import Arguments
-from pypy.interpreter.baseobjspace import Wrappable, W_Root, ObjSpace
+from pypy.interpreter.baseobjspace import Wrappable, W_Root, ObjSpace, \
+    DescrMismatch
 from pypy.interpreter.error import OperationError
 from pypy.tool.sourcetools import compile2, func_with_new_name
 from pypy.rlib.objectmodel import instantiate
@@ -242,7 +243,7 @@
         assert issubclass(cls, Wrappable)
         source = """
         def descr_typecheck_%(name)s(space, w_obj, %(extra)s):
-            obj = space.interp_w(%(cls_name)s, w_obj)
+            obj = space.descr_self_interp_w(%(cls_name)s, w_obj)
             return %(name)s(space, obj, %(extra)s)
         """
         miniglobals[cls_name] = cls
@@ -295,6 +296,7 @@
         self.fset = fset
         self.fdel = fdel
         self.doc = doc
+        self.reqcls = cls
         self.name = '<generic property>'
         self.objclass_getter = objclass_getter
     
@@ -307,7 +309,12 @@
             #print property, w_obj, w_cls
             return space.wrap(property)
         else:
-            return property.fget(space, w_obj)
+            try:
+                return property.fget(space, w_obj)
+            except DescrMismatch, e:
+                return w_obj.descr_call_mismatch(space, '__getattribute__',\
+                    property.reqcls, Arguments(space, [w_obj,
+                                           space.wrap(property.name)]))
     
     def descr_property_set(space, property, w_obj, w_value):
         """property.__set__(obj, value)
@@ -316,7 +323,12 @@
         if fset is None:
             raise OperationError(space.w_TypeError,
                                  space.wrap("readonly attribute"))
-        fset(space, w_obj, w_value)
+        try:
+            fset(space, w_obj, w_value)
+        except DescrMismatch, e:
+            w_obj.descr_call_mismatch(space, '__setattr__',\
+                property.reqcls, Arguments(space, [w_obj,
+                space.wrap(property.name), w_value]))
     
     def descr_property_del(space, property, w_obj):
         """property.__delete__(obj)
@@ -325,7 +337,12 @@
         if fdel is None:
             raise OperationError(space.w_AttributeError,
                                  space.wrap("cannot delete attribute"))
-        fdel(space, w_obj)
+        try:
+            fdel(space, w_obj)
+        except DescrMismatch, e:
+            w_obj.descr_call_mismatch(space, '__delattr__',\
+                property.reqcls, Arguments(space, [w_obj,
+                space.wrap(property.name)]))
     
     def descr_get_objclass(space, property):
         return property.objclass_getter(space)
@@ -586,7 +603,7 @@
                           unwrap_spec=['self', Arguments],
                           descrmismatch='__call__'),
     __get__ = interp2app(descr_function_get),
-    __repr__ = interp2app(Function.descr_function_repr),
+    __repr__ = interp2app(Function.descr_function_repr, descrmismatch='__repr__'),
     __reduce__ = interp2app(Function.descr_function__reduce__,
                             unwrap_spec=['self', ObjSpace]),
     __setstate__ = interp2app(Function.descr_function__setstate__,

Modified: pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy_function.py
==============================================================================
--- pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy_function.py	(original)
+++ pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy_function.py	Sat Nov  4 15:23:33 2006
@@ -4,21 +4,74 @@
 
 from pypy.objspace.std.test.test_proxy import AppProxyBasic
 
-class AppTestProxyFunction(AppProxyBasic):
+class AppTestProxyFunction(object):
+    def setup_method(self, meth):
+        self.w_get_proxy = self.space.appexec([], """():
+        class Controller(object):
+            def __init__(self, obj):
+                self.obj = obj
+    
+            def perform(self, name, *args, **kwargs):
+                return getattr(self.obj, name)(*args, **kwargs)
+        def get_proxy(f):
+            import types
+            return proxy(types.FunctionType, Controller(f).perform)
+        return get_proxy
+        """)
+    
     def test_function_noargs(self):
         def f():
             return 3
         
-        import types
-        c = self.Controller(f)
-        fun = proxy(types.FunctionType, c.perform)
+        fun = self.get_proxy(f)
         assert fun() == f()
     
     def test_simple_function(self):
         def f(x):
             return x
-        
-        import types
-        c = self.Controller(f)
-        fun = proxy(types.FunctionType, c.perform)
+
+        fun = self.get_proxy(f)
         assert fun(3) == f(3)
+
+    def test_function_args(self):
+        def f(x, y):
+            return x
+        
+        fun = self.get_proxy(f)
+        raises(TypeError, "fun(3)")
+        assert fun(1,2) == 1
+
+    def test_method_bind(self):
+        def f(self):
+            return 3
+        
+        class A:
+            pass
+            
+        fun = self.get_proxy(f)
+        assert fun.__get__(A())() == 3
+
+    def test_function_repr(self):
+        def f():
+            pass
+        
+        fun = self.get_proxy(f)
+        assert repr(fun).startswith("<function f")
+
+    def test_func_code(self):
+        def f():
+            pass
+        
+        fun = self.get_proxy(f)
+        assert fun.func_code is f.func_code
+
+    def test_funct_prop_setter_del(self):
+        def f():
+            pass
+        
+        fun = self.get_proxy(f)
+        fun.__doc__ = "aaa"
+        assert f.__doc__ == 'aaa'
+        del fun.__doc__
+        assert f.__doc__ is None
+



More information about the Pypy-commit mailing list