[tableview] deepcopy of colrenderer was actually broken with py < 2.7
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 07 Nov 2011 18:16:53 +0100
changeset 8065 8a933961938e
parent 8064 6d8eb873256d
child 8066 1d1b3735ac1b
[tableview] deepcopy of colrenderer was actually broken with py < 2.7
web/views/tableview.py
--- a/web/views/tableview.py	Mon Nov 07 18:09:13 2011 +0100
+++ b/web/views/tableview.py	Mon Nov 07 18:16:53 2011 +0100
@@ -64,6 +64,8 @@
 _ = unicode
 
 from warnings import warn
+from copy import copy
+from types import MethodType
 
 from logilab.mtconverter import xml_escape
 from logilab.common.decorators import cachedproperty
@@ -366,6 +368,10 @@
         self._cw = view._cw
         self.colid = colid
 
+    def copy(self):
+        assert self.view is None
+        return copy(self)
+
     def default_header(self):
         """Return header for this column if one has not been specified."""
         return self._cw._(self.colid)
@@ -698,6 +704,23 @@
         self.renderfunc = renderfunc
         self.sortfunc = sortfunc
 
+    def copy(self):
+        assert self.view is None
+        # copy of attribute referencing a method doesn't work with python < 2.7
+        renderfunc = self.__dict__.pop('renderfunc')
+        sortfunc = self.__dict__.pop('sortfunc')
+        try:
+            acopy =  copy(self)
+            for aname, member in[('renderfunc', renderfunc),
+                                 ('sortfunc', sortfunc)]:
+                if isinstance(member, MethodType):
+                    member = MethodType(member.im_func, acopy, acopy.__class__)
+                setattr(acopy, aname, member)
+            return acopy
+        finally:
+            self.renderfunc = renderfunc
+            self.sortfunc = sortfunc
+
     def render_cell(self, w, rownum):
         entity = self.entity(rownum)
         if entity is None: