[Zope-Checkins] SVN: Zope/trunk/ - Collector #2116: sequence.sort() did not work properly

Andreas Jung andreas at andreas-jung.com
Mon Jun 5 05:46:05 EDT 2006


Log message for revision 68490:
        - Collector #2116: sequence.sort() did not work properly
          locale related comparison methods
  

Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/lib/python/DocumentTemplate/sequence/SortEx.py

-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2006-06-05 03:34:01 UTC (rev 68489)
+++ Zope/trunk/doc/CHANGES.txt	2006-06-05 09:46:04 UTC (rev 68490)
@@ -20,6 +20,9 @@
 
       - Acquisition wrappers now correctly proxy __contains__.
 
+      - Collector #2116: sequence.sort() did not work properly
+        locale related comparison methods
+
   Zope 2.10.0 beta 1 (2006/05/30)
 
     Restructuring

Modified: Zope/trunk/lib/python/DocumentTemplate/sequence/SortEx.py
===================================================================
--- Zope/trunk/lib/python/DocumentTemplate/sequence/SortEx.py	2006-06-05 03:34:01 UTC (rev 68489)
+++ Zope/trunk/lib/python/DocumentTemplate/sequence/SortEx.py	2006-06-05 09:46:04 UTC (rev 68490)
@@ -17,8 +17,9 @@
 $Id$
 """
 
-from types import TupleType
+from App.config import getConfiguration
 
+
 def sort(sequence, sort=(), _=None, mapping=0):
     """
     - sequence is a sequence of objects to be sorted
@@ -82,7 +83,7 @@
     s=[]
     for client in sequence:
         k = None
-        if type(client)==TupleType and len(client)==2:
+        if isinstance(client, tuple) and len(client)==2:
             if isort: k=client[0]
             v=client[1]
         else:
@@ -133,14 +134,34 @@
 def nocase(str1, str2):
     return cmp(str1.lower(), str2.lower())
 
-import sys
-if sys.modules.has_key("locale"): # only if locale is already imported
-    from locale import strcoll
 
-    def strcoll_nocase(str1, str2):
-        return strcoll(str1.lower(), str2.lower())
+def getStrcoll():
+    """ check the Zope configuration for the 'locale' configuration
+        and return a suitable implementation.
+    """
 
+    if getConfiguration().locale:
+        from locale import strcoll
+        return strcoll
+    else:
+        raise RuntimeError("strcoll() is only available for a proper 'locale' configuration in zope.conf")
 
+def getStrcoll_nocase():
+    """ check the Zope configuration for the 'locale' configuration
+        and return a suitable implementation.
+    """
+
+    if getConfiguration().locale:
+        from locale import strcoll
+    
+        def strcoll_nocase(str1, str2):
+            return strcoll(str1.lower(), str2.lower())
+        return strcoll_nocase
+
+    else:
+        raise RuntimeError("strcoll_nocase() is only available for a proper 'locale' configuration in zope.conf")
+
+
 def make_sortfunctions(sortfields, _):
     """Accepts a list of sort fields; splits every field, finds comparison
     function. Returns a list of 3-tuples (field, cmp_function, asc_multplier)"""
@@ -168,9 +189,9 @@
         elif f_name == "nocase":
             func = nocase
         elif f_name in ("locale", "strcoll"):
-            func = strcoll
+            func = getStrcoll()
         elif f_name in ("locale_nocase", "strcoll_nocase"):
-            func = strcoll_nocase
+            func = getStrcoll_nocase()
         else: # no - look it up in the namespace
             func = _.getitem(f_name, 0)
 



More information about the Zope-Checkins mailing list