[pypy-svn] r26121 - in pypy/dist/pypy/tool: . test

arigo at codespeak.net arigo at codespeak.net
Sat Apr 22 11:01:57 CEST 2006


Author: arigo
Date: Sat Apr 22 11:01:56 2006
New Revision: 26121

Added:
   pypy/dist/pypy/tool/test/test_instancemethod.py   (contents, props changed)
Modified:
   pypy/dist/pypy/tool/instancemethod.py
Log:
InstanceMethod: remove the pointless __self trick (see #pypy discussion).
Added test.


Modified: pypy/dist/pypy/tool/instancemethod.py
==============================================================================
--- pypy/dist/pypy/tool/instancemethod.py	(original)
+++ pypy/dist/pypy/tool/instancemethod.py	Sat Apr 22 11:01:56 2006
@@ -7,16 +7,16 @@
         self.im_self = im_self
         self.im_class = im_class
 
-    def __call__(__self, *args, **kwds):
-        firstarg = __self.im_self
+    def __call__(self, *args, **kwds):
+        firstarg = self.im_self
         if firstarg is None:
-            if not args or not isinstance(args[0], __self.im_class):
+            if not args or not isinstance(args[0], self.im_class):
                 raise TypeError(
                     "must be called with %r instance as first argument" % (
-                    __self.im_class,))
+                    self.im_class,))
             firstarg = args[0]
             args = args[1:]
-        return __self.im_func(firstarg, *args, **kwds)
+        return self.im_func(firstarg, *args, **kwds)
 
     def __eq__(self, other):
         return isinstance(other, InstanceMethod) and (

Added: pypy/dist/pypy/tool/test/test_instancemethod.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/tool/test/test_instancemethod.py	Sat Apr 22 11:01:56 2006
@@ -0,0 +1,29 @@
+from pypy.tool.instancemethod import InstanceMethod
+
+class X(object):
+    def f(self, *args, **kwds):
+        return args, kwds
+
+def test_bound():
+    obj = X()
+    obj.x = 12
+    meth = InstanceMethod(X.f.im_func, obj, X)
+    assert meth(1, z=2) == ((1,), {'z': 2})
+
+def test_unbound():
+    obj = X()
+    obj.x = 12
+    meth = InstanceMethod(X.f.im_func, None, X)
+    assert meth(obj, 1, z=2) == ((1,), {'z': 2})
+
+def test_eq_hash():
+    obj1 = X()
+    obj1.x = 12
+    meth1 = InstanceMethod(X.f.im_func, obj1, X)
+    meth1bis = InstanceMethod(X.f.im_func, obj1, X)
+    obj2 = X()
+    obj2.x = 12
+    meth2 = InstanceMethod(X.f.im_func, obj2, X)
+    d = {meth1: 123, meth2: 456}
+    assert len(d) == 2
+    assert d[meth1bis] == 123



More information about the Pypy-commit mailing list