changeset 510:4c473bedd528 stable

Fix Python 2.3 compatibility for 0.9 branch (closes #233)
author fschwarz
date Fri, 04 Mar 2011 14:16:15 +0000
parents cd2dec0823c9
children 4f39b6e38540
files 0.9.x/ChangeLog 0.9.x/babel/messages/tests/catalog.py 0.9.x/babel/messages/tests/frontend.py 0.9.x/babel/messages/tests/pofile.py 0.9.x/babel/numbers.py 0.9.x/babel/support.py 0.9.x/babel/util.py
diffstat 7 files changed, 44 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/0.9.x/ChangeLog
+++ b/0.9.x/ChangeLog
@@ -5,7 +5,7 @@
  * Backport r493-494: documentation typo fixes.
  * Make the CLDR import script work with Python 2.7.
  * Fix various typos.
- * Fixed Python 2.3 compatibility (ticket #146).
+ * Fixed Python 2.3 compatibility (ticket #146, #233).
  * Sort output of list-locales.
  * Make the POT-Creation-Date of the catalog being updated equal to
    POT-Creation-Date of the template used to update (ticket #148).
--- a/0.9.x/babel/messages/tests/catalog.py
+++ b/0.9.x/babel/messages/tests/catalog.py
@@ -251,7 +251,11 @@
 
 def suite():
     suite = unittest.TestSuite()
-    suite.addTest(doctest.DocTestSuite(catalog, optionflags=doctest.ELLIPSIS))
+    if hasattr(doctest, 'ELLIPSIS'):
+        suite.addTest(doctest.DocTestSuite(catalog, optionflags=doctest.ELLIPSIS))
+    else:
+        # Python 2.3 has no doctest.ELLIPSIS option, it's implicit
+        suite.addTest(doctest.DocTestSuite(catalog))
     suite.addTest(unittest.makeSuite(MessageTestCase))
     suite.addTest(unittest.makeSuite(CatalogTestCase))
     return suite
--- a/0.9.x/babel/messages/tests/frontend.py
+++ b/0.9.x/babel/messages/tests/frontend.py
@@ -525,12 +525,19 @@
             self.cli.run(sys.argv)
             self.fail('Expected SystemExit')
         except SystemExit, e:
-            self.assertEqual(2, e.code)
+            stderr = sys.stderr.getvalue()
+            if isinstance(e.code, int):
+                self.assertEqual(2, e.code)
+            else:
+                # OptionParser in Python 2.3 does not set the exit code.
+                # Instead the 'code' contains the custom error message from the 
+                # frontend
+                stderr = stderr + e.code + '\n'
             self.assertEqual("""\
 usage: pybabel command [options] [args]
 
 pybabel: error: no valid command or option passed. try the -h/--help option for more information.
-""", sys.stderr.getvalue().lower())
+""", stderr.lower())
 
     def test_help(self):
         try:
--- a/0.9.x/babel/messages/tests/pofile.py
+++ b/0.9.x/babel/messages/tests/pofile.py
@@ -15,6 +15,7 @@
 import doctest
 from StringIO import StringIO
 import unittest
+import sys
 
 from babel.messages.catalog import Catalog, Message
 from babel.messages import pofile
@@ -451,7 +452,9 @@
 
 def suite():
     suite = unittest.TestSuite()
-    suite.addTest(doctest.DocTestSuite(pofile))
+    is_py23 = sys.version_info[0:2] == (2, 3)
+    if not is_py23:
+        suite.addTest(doctest.DocTestSuite(pofile))
     suite.addTest(unittest.makeSuite(ReadPoTestCase))
     suite.addTest(unittest.makeSuite(WritePoTestCase))
     return suite
--- a/0.9.x/babel/numbers.py
+++ b/0.9.x/babel/numbers.py
@@ -32,6 +32,7 @@
     have_decimal = False
 
 from babel.core import default_locale, Locale
+from babel.util import rsplit
 
 __all__ = ['format_number', 'format_decimal', 'format_currency',
            'format_percent', 'format_scientific', 'parse_number',
@@ -391,7 +392,7 @@
             raise ValueError('Significant digit patterns can not contain '
                              '"@" or "0"')
     if '.' in number:
-        integer, fraction = number.rsplit('.', 1)
+        integer, fraction = rsplit(number, '.', 1)
     else:
         integer = number
         fraction = ''
--- a/0.9.x/babel/support.py
+++ b/0.9.x/babel/support.py
@@ -20,16 +20,11 @@
 from datetime import date, datetime, time
 import gettext
 
-try:
-    set
-except NameError:
-    from sets import set
-
 from babel.core import Locale
 from babel.dates import format_date, format_datetime, format_time, LC_TIME
 from babel.numbers import format_number, format_decimal, format_currency, \
                           format_percent, format_scientific, LC_NUMERIC
-from babel.util import UTC
+from babel.util import set, UTC
 
 __all__ = ['Format', 'LazyProxy', 'Translations']
 __docformat__ = 'restructuredtext en'
--- a/0.9.x/babel/util.py
+++ b/0.9.x/babel/util.py
@@ -269,6 +269,28 @@
         rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:]
         return os.path.join(*rel_list)
 
+try:
+    from operator import attrgetter, itemgetter
+except ImportError:
+    def itemgetter(name):
+        def _getitem(obj):
+            return obj[name]
+        return _getitem
+
+try:
+    ''.rsplit
+    def rsplit(a_string, sep=None, maxsplit=None):
+        return a_string.rsplit(sep, maxsplit)
+except AttributeError:
+    def rsplit(a_string, sep=None, maxsplit=None):
+        parts = a_string.split(sep)
+        if maxsplit is None or len(parts) <= maxsplit:
+            return parts
+        maxsplit_index = len(parts) - maxsplit
+        non_splitted_part = sep.join(parts[:maxsplit_index])
+        splitted = parts[maxsplit_index:]
+        return [non_splitted_part] + splitted
+
 ZERO = timedelta(0)
 
 
Copyright (C) 2012-2017 Edgewall Software