web/request.py
changeset 9897 fa44db7da2dc
parent 9821 2077c8da1893
parent 9884 5ad1c3a9c4a1
child 10001 1245357b3b3e
--- a/web/request.py	Thu Jul 17 11:08:56 2014 +0200
+++ b/web/request.py	Fri Jul 18 17:35:25 2014 +0200
@@ -30,7 +30,6 @@
 from datetime import date, datetime
 from urlparse import urlsplit
 import httplib
-from itertools import count
 from warnings import warn
 
 from rql.utils import rqlvar_maker
@@ -82,6 +81,24 @@
     return [v for v in value if v != INTERNAL_FIELD_VALUE]
 
 
+class Counter(object):
+    """A picklable counter object, usable for e.g. page tab index count"""
+    __slots__ = ('value',)
+
+    def __init__(self, initialvalue=0):
+        self.value = initialvalue
+
+    def __call__(self):
+        value = self.value
+        self.value += 1
+        return value
+
+    def __getstate__(self):
+        return {'value': self.value}
+
+    def __setstate__(self, state):
+        self.value = state['value']
+
 
 class _CubicWebRequestBase(RequestSessionBase):
     """abstract HTTP request, should be extended according to the HTTP backend
@@ -201,7 +218,7 @@
     def next_tabindex(self):
         nextfunc = self.get_page_data('nexttabfunc')
         if nextfunc is None:
-            nextfunc = count(1).next
+            nextfunc = Counter(1)
             self.set_page_data('nexttabfunc', nextfunc)
         return nextfunc()