[Checkins] SVN: persistent/trunk/ Python3 compat:

Tres Seaver cvs-admin at zope.org
Fri Jun 29 04:02:22 UTC 2012


Log message for revision 127174:
  Python3 compat:
  
  - (Temporary) Disable C extension building under Py3k.
  
  - Work around move of UserDict / UserList.
  
  - Make test of 'cmp' conditional on PYTHON2.
  
  - Make '__setslice__' / '__delslice__' conditional on PYTHON2.
  

Changed:
  _U  persistent/trunk/
  U   persistent/trunk/persistent/_compat.py
  U   persistent/trunk/persistent/list.py
  U   persistent/trunk/persistent/mapping.py
  U   persistent/trunk/persistent/tests/test_list.py
  U   persistent/trunk/persistent/tests/test_mapping.py
  U   persistent/trunk/setup.py

-=-
Modified: persistent/trunk/persistent/_compat.py
===================================================================
--- persistent/trunk/persistent/_compat.py	2012-06-29 04:02:14 UTC (rev 127173)
+++ persistent/trunk/persistent/_compat.py	2012-06-29 04:02:18 UTC (rev 127174)
@@ -16,13 +16,22 @@
 
 if sys.version_info[0] > 2: #pragma NO COVER
     import copyreg as copy_reg
+    from collections import UserDict as IterableUserDict
+    from collections import UserList
 
     def _u(s):
         return s
 
+    PYTHON3 = True
+    PYTHON2 = False
+
 else: #pragma NO COVER
     import copy_reg
+    from UserDict import IterableUserDict
+    from UserList import UserList
 
     def _u(s):
         return unicode(s, 'unicode_escape')
 
+    PYTHON3 = False
+    PYTHON2 = True

Modified: persistent/trunk/persistent/list.py
===================================================================
--- persistent/trunk/persistent/list.py	2012-06-29 04:02:14 UTC (rev 127173)
+++ persistent/trunk/persistent/list.py	2012-06-29 04:02:18 UTC (rev 127174)
@@ -17,13 +17,15 @@
 $Id$"""
 
 import persistent
-from UserList import UserList
+from persistent._compat import UserList
+from persistent._compat import PYTHON2
 
 class PersistentList(UserList, persistent.Persistent):
     __super_setitem = UserList.__setitem__
     __super_delitem = UserList.__delitem__
-    __super_setslice = UserList.__setslice__
-    __super_delslice = UserList.__delslice__
+    if PYTHON2:
+        __super_setslice = UserList.__setslice__
+        __super_delslice = UserList.__delslice__
     __super_iadd = UserList.__iadd__
     __super_imul = UserList.__imul__
     __super_append = UserList.append

Modified: persistent/trunk/persistent/mapping.py
===================================================================
--- persistent/trunk/persistent/mapping.py	2012-06-29 04:02:14 UTC (rev 127173)
+++ persistent/trunk/persistent/mapping.py	2012-06-29 04:02:18 UTC (rev 127174)
@@ -17,7 +17,7 @@
 $Id$"""
 
 import persistent
-import UserDict
+from persistent._compat import IterableUserDict
 
 class default(object):
 
@@ -30,7 +30,7 @@
         return self.func(inst)
 
 
-class PersistentMapping(UserDict.IterableUserDict, persistent.Persistent):
+class PersistentMapping(IterableUserDict, persistent.Persistent):
     """A persistent wrapper for mapping objects.
 
     This class allows wrapping of mapping objects so that object
@@ -47,13 +47,13 @@
     # state as changed when a method actually changes the state.  At
     # the mapping API evolves, we may need to add more methods here.
 
-    __super_delitem = UserDict.IterableUserDict.__delitem__
-    __super_setitem = UserDict.IterableUserDict.__setitem__
-    __super_clear = UserDict.IterableUserDict.clear
-    __super_update = UserDict.IterableUserDict.update
-    __super_setdefault = UserDict.IterableUserDict.setdefault
-    __super_pop = UserDict.IterableUserDict.pop
-    __super_popitem = UserDict.IterableUserDict.popitem
+    __super_delitem = IterableUserDict.__delitem__
+    __super_setitem = IterableUserDict.__setitem__
+    __super_clear = IterableUserDict.clear
+    __super_update = IterableUserDict.update
+    __super_setdefault = IterableUserDict.setdefault
+    __super_pop = IterableUserDict.pop
+    __super_popitem = IterableUserDict.popitem
 
     def __delitem__(self, key):
         self.__super_delitem(key)
@@ -75,7 +75,7 @@
         # We could inline all of UserDict's implementation into the
         # method here, but I'd rather not depend at all on the
         # implementation in UserDict (simple as it is).
-        if not self.has_key(key):
+        if not key in self.data:
             self._p_changed = 1
         return self.__super_setdefault(key, failobj)
 

Modified: persistent/trunk/persistent/tests/test_list.py
===================================================================
--- persistent/trunk/persistent/tests/test_list.py	2012-06-29 04:02:14 UTC (rev 127173)
+++ persistent/trunk/persistent/tests/test_list.py	2012-06-29 04:02:18 UTC (rev 127174)
@@ -44,6 +44,7 @@
         self.failIf('_v_baz' in state)
 
     def testTheWorld(self):
+        from persistent._compat import PYTHON2
         # Test constructors
         pl = self._getTargetClass()
         u = pl()
@@ -68,17 +69,18 @@
 
         # Test __cmp__ and __len__
 
-        def mycmp(a, b):
-            r = cmp(a, b)
-            if r < 0: return -1
-            if r > 0: return 1
-            return r
+        if PYTHON2:
+            def mycmp(a, b):
+                r = cmp(a, b)
+                if r < 0: return -1
+                if r > 0: return 1
+                return r
 
-        all = [l0, l1, l2, u, u0, u1, u2, uu, uu0, uu1, uu2]
-        for a in all:
-            for b in all:
-                eq(mycmp(a, b), mycmp(len(a), len(b)),
-                      "mycmp(a, b) == mycmp(len(a), len(b))")
+            all = [l0, l1, l2, u, u0, u1, u2, uu, uu0, uu1, uu2]
+            for a in all:
+                for b in all:
+                    eq(mycmp(a, b), mycmp(len(a), len(b)),
+                        "mycmp(a, b) == mycmp(len(a), len(b))")
 
         # Test __getitem__
 
@@ -218,8 +220,9 @@
         eq(u, u2, "u == u2")
 
         # Test keyword arguments to sort
-        u.sort(cmp=lambda x,y: cmp(y, x))
-        eq(u, [1, 0], "u == [1, 0]")
+        if PYTHON2:
+            u.sort(cmp=lambda x,y: cmp(y, x))
+            eq(u, [1, 0], "u == [1, 0]")
 
         u.sort(key=lambda x:-x)
         eq(u, [1, 0], "u == [1, 0]")

Modified: persistent/trunk/persistent/tests/test_mapping.py
===================================================================
--- persistent/trunk/persistent/tests/test_mapping.py	2012-06-29 04:02:14 UTC (rev 127173)
+++ persistent/trunk/persistent/tests/test_mapping.py	2012-06-29 04:02:18 UTC (rev 127174)
@@ -67,6 +67,7 @@
         self.failIf('_v_baz' in state)
 
     def testTheWorld(self):
+        from persistent._compat import PYTHON2
         # Test constructors
         l0 = {}
         l1 = {0:0}
@@ -81,7 +82,7 @@
         uu1 = self._makeOne(u1)
         uu2 = self._makeOne(u2)
 
-        class OtherMapping:
+        class OtherMapping(dict):
             def __init__(self, initmapping):
                 self.__data = initmapping
             def items(self):
@@ -97,17 +98,18 @@
 
         # Test __cmp__ and __len__
 
-        def mycmp(a, b):
-            r = cmp(a, b)
-            if r < 0: return -1
-            if r > 0: return 1
-            return r
+        if PYTHON2:
+            def mycmp(a, b):
+                r = cmp(a, b)
+                if r < 0: return -1
+                if r > 0: return 1
+                return r
 
-        all = [l0, l1, l2, u, u0, u1, u2, uu, uu0, uu1, uu2]
-        for a in all:
-            for b in all:
-                eq(mycmp(a, b), mycmp(len(a), len(b)),
-                      "mycmp(a, b) == mycmp(len(a), len(b))")
+            all = [l0, l1, l2, u, u0, u1, u2, uu, uu0, uu1, uu2]
+            for a in all:
+                for b in all:
+                    eq(mycmp(a, b), mycmp(len(a), len(b)),
+                        "mycmp(a, b) == mycmp(len(a), len(b))")
 
         # Test __getitem__
 

Modified: persistent/trunk/setup.py
===================================================================
--- persistent/trunk/setup.py	2012-06-29 04:02:14 UTC (rev 127173)
+++ persistent/trunk/setup.py	2012-06-29 04:02:18 UTC (rev 127174)
@@ -18,8 +18,6 @@
 import platform
 import sys
 
-from ez_setup import use_setuptools
-use_setuptools()
 
 from setuptools import Extension
 from setuptools import find_packages
@@ -40,7 +38,7 @@
 # Jython cannot build the C optimizations, while on PyPy they are
 # anti-optimizations (the C extension compatibility layer is known-slow,
 # and defeats JIT opportunities).
-if is_pypy or is_jython:
+if is_pypy or is_jython or sys.version_info[0] > 2:
     ext_modules = headers = []
 else:
     ext_modules = [Extension(name = 'persistent.cPersistence',



More information about the checkins mailing list