[py-dev] Re: [Python-Dev] Getting rid of unbound methods: patch available

Armin Rigo arigo at tunes.org
Mon Jan 17 14:17:21 CET 2005


Hi,

On Mon, Jan 17, 2005 at 01:14:17PM +0000, Armin Rigo wrote:
> For reference, the diff is attached to this message.

Or this one.


Armin
-------------- next part --------------
Index: test/item.py
===================================================================
--- test/item.py	(revision 8327)
+++ test/item.py	(working copy)
@@ -84,13 +84,15 @@
         if x is not None:
             x(function)
 
-    def make_callable(self, method):
+    def resolve_callable(self, extpy):
+        container = extpy.dirpath().resolve()
+        method = extpy.resolve()
         assert callable(method)
-        if not hasattr(method, 'im_class'):
+        if not isclass(container):
             return method
-        if self.state._instance.__class__ != method.im_class:
-            self.state._instance = method.im_class()
-        return method.__get__(self.state._instance, method.im_class)
+        if self.state._instance.__class__ != container:
+            self.state._instance = container()
+        return method.__get__(self.state._instance, container)
 
 class Item(SetupItem):
     """ an Item is responsible for locating and executing
@@ -104,8 +106,8 @@
 
     def run(self, driver):
         self.setup_path(self.extpy)
-        method = self.make_callable(self.extpy.resolve())
-        if hasattr(method, 'im_self'):
+        method = self.resolve_callable(self.extpy)
+        if getattr(method, 'im_self', None) is not None:
             self.setup_method(method)
         else:
             self.setup_function(method)
Index: test/compat.py
===================================================================
--- test/compat.py	(revision 8327)
+++ test/compat.py	(working copy)
@@ -7,7 +7,7 @@
     """
     def execute(self, driver):
         unboundmethod = self.extpy.resolve()
-        cls = unboundmethod.im_class
+        cls = self.extpy.dirpath().resolve()
         instance = cls()
         instance.setUp()
         try:
Index: test/collect.py
===================================================================
--- test/collect.py	(revision 8327)
+++ test/collect.py	(working copy)
@@ -172,11 +172,15 @@
             if extpy.check(genfunc=1):
                 yield Generator(extpy)
             else:
+                container = extpy.dirpath().resolve()
                 func = extpy.resolve()
-                try:
-                    yield getattr(func.im_class, 'Item')(extpy)
-                except AttributeError:
-                    yield self.Item(extpy)
+                Item = self.Item
+                if inspect.isclass(container):
+                    try:
+                        Item = container.Item
+                    except AttributeError:
+                        pass
+                yield Item(extpy)
 
 class Generator(PyCollector):
     def builditem(self, obj):
@@ -197,9 +201,10 @@
             #sm.setup_path(self.extpy)
             #gen, teardown = sm.setup_method(self.extpy)
             #assert not teardown, "%r not processoable in Generator-Collector (XXX)"
+            container = self.extpy.dirpath().resolve()
             gen = self.extpy.resolve()
-            if hasattr(gen, 'im_self')  and not gen.im_self:
-                gen = gen.__get__(gen.im_class(), gen.im_class)
+            if inspect.isclass(container):
+                gen = gen.__get__(container(), container)
             for call in gen():
                 yield self.builditem(call)
         except:


More information about the Pytest-dev mailing list