[Checkins] SVN: Zope3/trunk/src/zope/app/intid/ - ported final fix for intid utility from 3.3 branch

Christian Theune cvs-admin at zope.org
Sat Jun 17 18:00:20 EDT 2006


Log message for revision 68720:
   - ported final fix for intid utility from 3.3 branch
  

Changed:
  U   Zope3/trunk/src/zope/app/intid/__init__.py
  U   Zope3/trunk/src/zope/app/intid/tests.py

-=-
Modified: Zope3/trunk/src/zope/app/intid/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/__init__.py	2006-06-17 21:51:14 UTC (rev 68719)
+++ Zope3/trunk/src/zope/app/intid/__init__.py	2006-06-17 22:00:16 UTC (rev 68720)
@@ -48,10 +48,9 @@
     implements(IIntIds)
 
     _v_nextid = None   
-    
-    # Used for testability of random function
-    __randint__ = random.randint
 
+    _randrange = random.randrange
+
     def __init__(self):
         self.ids = OIBTree.OIBTree()
         self.refs = IOBTree.IOBTree()
@@ -100,16 +99,11 @@
         """
         while True:
             if self._v_nextid is None:
-                self._v_nextid = self.__randint__(0, 2**31)
+                self._v_nextid = self._randrange(0, 2**31)
             uid = self._v_nextid
             self._v_nextid += 1
-            try:
-                if uid not in self.refs:
-                    return uid
-            except TypeError:
-                # uid was a long instead of int and btree complained
-                # we just try again
-                pass
+            if uid not in self.refs:
+                return uid
             self._v_nextid = None
 
     def register(self, ob):

Modified: Zope3/trunk/src/zope/app/intid/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/tests.py	2006-06-17 21:51:14 UTC (rev 68719)
+++ Zope3/trunk/src/zope/app/intid/tests.py	2006-06-17 22:00:16 UTC (rev 68720)
@@ -108,37 +108,21 @@
         self.assertRaises(KeyError, u.getId, obj)
 
     def test_btree_long(self):
+        # This is a somewhat arkward test, that *simulates* the border case
+        # behaviour of the _generateId method
         u = IntIds()
+        u._randrange = lambda x,y:int(2**31-1)
 
-        fake_randint_data = [2**31,20,2**31-1,2**31-2,10]
+        # The chosen int is exactly the largest number possible that is
+        # delivered by the randint call in the code
+        obj = P()
+        obj._p_jar = ConnectionStub()
+        uid = u.register(obj)
+        self.assertEquals(2**31-1, uid)
+        # Make an explicit tuple here to avoid implicit type casts on 2**31-1
+        # by the btree code
+        self.failUnless(2**31-1 in tuple(u.refs.keys()))
 
-        def fake_randint(min, max):
-            return fake_randint_data.pop(0)
-
-        u.__randint__ = fake_randint
-
-        # Check whether something in the area of 2**31 is accepted by the btree
-        try:
-            u.refs[2**31] = True
-            btree_accepts_long = True
-        except TypeError:
-            btree_accepts_long = False
-
-        # One int that is too large 
-        uid1 = u._generateId()
-        if btree_accepts_long:
-            self.assertEquals(2**31, uid1)
-        else:
-            self.assertEquals(20, uid1)
-
-        # Two ints that are too large
-        u._v_nextid = None
-        uid2 = u._generateId()
-        if btree_accepts_long:
-            self.assertEquals(20, uid2)
-        else:
-            self.assertEquals(10, uid2)
-
     def test_len_items(self):
         u = IntIds()
         obj = P()



More information about the Checkins mailing list