1. edgewall
  2. babel

Commits

Felix Schwarz  committed d010c09

Fix Python 2.3 compatibility for 0.9 branch (closes #233)

  • Participants
  • Parent commits 99a51a3
  • Branches stable-0.9.x

Comments (0)

Files changed (7)

File ChangeLog

View file
  * 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).

File babel/messages/tests/catalog.py

View file
 
 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

File babel/messages/tests/frontend.py

View file
             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:

File babel/messages/tests/pofile.py

View file
 import doctest
 from StringIO import StringIO
 import unittest
+import sys
 
 from babel.messages.catalog import Catalog, Message
 from babel.messages import pofile
 
 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

File babel/numbers.py

View file
     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',
             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 = ''

File babel/support.py

View file
 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'

File babel/util.py

View file
         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)