]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Made another pass on the Python 3 support
authorArmin Ronacher <armin.ronacher@active-4.com>
Sat, 6 Jul 2013 14:21:12 +0000 (16:21 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sat, 6 Jul 2013 14:21:12 +0000 (16:21 +0200)
babel/_compat.py
babel/core.py
babel/dates.py
babel/messages/catalog.py
babel/messages/checkers.py
babel/messages/frontend.py
babel/util.py
tests/messages/test_catalog.py

index c4e044784e3dd80bc8b79dbf79ca017c78bea0d3..f8248c5f1bc5c3f3c2d9fda6f591c2195cd3d3ca 100644 (file)
@@ -31,13 +31,6 @@ if not PY2:
     from io import StringIO
     import pickle
 
-    def reraise(tp, value, tb=None):
-        if value.__traceback__ is not tb:
-            raise value.with_traceback(tb)
-        raise value
-
-    implements_to_string = _identity
-
     izip = zip
     imap = map
 
@@ -53,11 +46,7 @@ else:
     from cStringIO import StringIO
     import cPickle as pickle
 
-    exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
+    from itertools import izip, imap
 
-    def implements_to_string(cls):
-        cls.__unicode__ = cls.__str__
-        cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
-        return cls
 
-    from itertools import izip, imap
+number_types = integer_types + (float,)
index 4c531259ba1775b0ad9a73325343f86ea9db7193..87e4921731f9f4a917ee0265b00fbdae8d824db7 100644 (file)
@@ -16,7 +16,7 @@
 import os
 
 from babel import localedata
-from babel._compat import pickle
+from babel._compat import pickle, string_types
 
 __all__ = ['UnknownLocaleError', 'Locale', 'default_locale', 'negotiate_locale',
            'parse_locale']
@@ -210,7 +210,7 @@ class Locale(object):
                                      requested locale
         :see: `parse_locale`
         """
-        if isinstance(identifier, basestring):
+        if isinstance(identifier, string_types):
             return cls(*parse_locale(identifier, sep=sep))
         return identifier
 
index ef1d169d1a5d2752aa139c9b69b57b66b0491c81..a69adc84bbdee27fc0f7e02458f69bb6d48c9fc7 100644 (file)
@@ -29,6 +29,7 @@ import pytz as _pytz
 
 from babel.core import default_locale, get_global, Locale
 from babel.util import UTC, LOCALTZ
+from babel._compat import string_types, integer_types, number_types
 
 __all__ = ['format_date', 'format_datetime', 'format_time', 'format_timedelta',
            'get_timezone_name', 'parse_date', 'parse_datetime', 'parse_time']
@@ -51,7 +52,7 @@ def get_timezone(zone=None):
     """
     if zone is None:
         return LOCALTZ
-    if not isinstance(zone, basestring):
+    if not isinstance(zone, string_types):
         return zone
     try:
         return _pytz.timezone(zone)
@@ -293,7 +294,7 @@ def get_timezone_gmt(datetime=None, width='long', locale=LC_TIME):
     """
     if datetime is None:
         datetime = datetime_.utcnow()
-    elif isinstance(datetime, (int, long)):
+    elif isinstance(datetime, integer_types):
         datetime = datetime_.utcfromtimestamp(datetime).time()
     if datetime.tzinfo is None:
         datetime = datetime.replace(tzinfo=UTC)
@@ -339,10 +340,10 @@ def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME):
     if dt_or_tzinfo is None:
         dt = datetime.now()
         tzinfo = LOCALTZ
-    elif isinstance(dt_or_tzinfo, basestring):
+    elif isinstance(dt_or_tzinfo, string_types):
         dt = None
         tzinfo = get_timezone(dt_or_tzinfo)
-    elif isinstance(dt_or_tzinfo, (int, long)):
+    elif isinstance(dt_or_tzinfo, integer_types):
         dt = None
         tzinfo = UTC
     elif isinstance(dt_or_tzinfo, (datetime, time)):
@@ -451,10 +452,10 @@ def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False,
     if dt_or_tzinfo is None:
         dt = datetime.now()
         tzinfo = LOCALTZ
-    elif isinstance(dt_or_tzinfo, basestring):
+    elif isinstance(dt_or_tzinfo, string_types):
         dt = None
         tzinfo = get_timezone(dt_or_tzinfo)
-    elif isinstance(dt_or_tzinfo, (int, long)):
+    elif isinstance(dt_or_tzinfo, integer_types):
         dt = None
         tzinfo = UTC
     elif isinstance(dt_or_tzinfo, (datetime, time)):
@@ -576,7 +577,7 @@ def format_datetime(datetime=None, format='medium', tzinfo=None,
     """
     if datetime is None:
         datetime = datetime_.utcnow()
-    elif isinstance(datetime, (int, long, float)):
+    elif isinstance(datetime, number_types):
         datetime = datetime_.utcfromtimestamp(datetime)
     elif isinstance(datetime, time):
         datetime = datetime_.combine(date.today(), datetime)
@@ -660,7 +661,7 @@ def format_time(time=None, format='medium', tzinfo=None, locale=LC_TIME):
     """
     if time is None:
         time = datetime.utcnow()
-    elif isinstance(time, (int, long, float)):
+    elif isinstance(time, number_types):
         time = datetime.utcfromtimestamp(time)
     if time.tzinfo is None:
         time = time.replace(tzinfo=UTC)
index 0d454e5765f642dc0ef9e57c3b55485275479253..25fe92c91b7c117c4c3df4098627b1894be04c48 100644 (file)
@@ -25,7 +25,8 @@ from babel import __version__ as VERSION
 from babel.core import Locale
 from babel.dates import format_datetime
 from babel.messages.plurals import get_plural
-from babel.util import odict, distinct, LOCALTZ, UTC, FixedOffsetTimezone
+from babel.util import odict, distinct, LOCALTZ, FixedOffsetTimezone
+from babel._compat import string_types, number_types
 
 __all__ = ['Message', 'Catalog', 'TranslationError']
 
@@ -76,7 +77,7 @@ class Message(object):
             self.flags.discard('python-format')
         self.auto_comments = list(distinct(auto_comments))
         self.user_comments = list(distinct(user_comments))
-        if isinstance(previous_id, basestring):
+        if isinstance(previous_id, string_types):
             self.previous_id = [previous_id]
         else:
             self.previous_id = list(previous_id)
@@ -142,7 +143,7 @@ class Message(object):
         for checker in checkers:
             try:
                 checker(catalog, self)
-            except TranslationError, e:
+            except TranslationError as e:
                 errors.append(e)
         return errors
 
@@ -323,7 +324,7 @@ class Catalog(object):
         headers.append(('POT-Creation-Date',
                         format_datetime(self.creation_date, 'yyyy-MM-dd HH:mmZ',
                                         locale='en')))
-        if isinstance(self.revision_date, (datetime, time_, int, long, float)):
+        if isinstance(self.revision_date, (datetime, time_) + number_types):
             headers.append(('PO-Revision-Date',
                             format_datetime(self.revision_date,
                                             'yyyy-MM-dd HH:mmZ', locale='en')))
@@ -434,6 +435,7 @@ class Catalog(object):
 
     Here's an example of the output for such a catalog template:
 
+    >>> from babel.dates import UTC
     >>> created = datetime(1990, 4, 1, 15, 30, tzinfo=UTC)
     >>> catalog = Catalog(project='Foobar', version='1.0',
     ...                   creation_date=created)
@@ -500,7 +502,7 @@ class Catalog(object):
         >>> Catalog(locale='ga').plural_expr
         '(n==1 ? 0 : n==2 ? 1 : 2)'
 
-        :type: `basestring`"""
+        :type: `string_types`"""
         if self._plural_expr is None:
             expr = '(n != 1)'
             if self.locale:
@@ -767,7 +769,7 @@ class Catalog(object):
                 fuzzy = True
                 fuzzy_matches.add(oldkey)
                 oldmsg = messages.get(oldkey)
-                if isinstance(oldmsg.id, basestring):
+                if isinstance(oldmsg.id, string_types):
                     message.previous_id = [oldmsg.id]
                 else:
                     message.previous_id = list(oldmsg.id)
index 2b8fa4fa12a726946e2703f5abbee4ec3601624c..86dc5bb7084bc1cb3c9b334648fc7aa9a4270659 100644 (file)
@@ -18,6 +18,7 @@
 
 from itertools import izip
 from babel.messages.catalog import TranslationError, PYTHON_FORMAT
+from babel._compat import string_types
 
 #: list of format chars that are compatible to each other
 _string_format_compatibilities = [
@@ -30,7 +31,7 @@ _string_format_compatibilities = [
 def num_plurals(catalog, message):
     """Verify the number of plurals in the translation."""
     if not message.pluralizable:
-        if not isinstance(message.string, basestring):
+        if not isinstance(message.string, string_types):
             raise TranslationError("Found plural forms for non-pluralizable "
                                    "message")
         return
index 753151ebacfe0514ce65f2c49fb4f5d506d60ff5..a0640eba45146e4661e3310b28848814262b5ac9 100755 (executable)
@@ -38,6 +38,7 @@ from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \
 from babel.messages.mofile import write_mo
 from babel.messages.pofile import read_po, write_po
 from babel.util import odict, LOCALTZ
+from babel._compat import string_types
 
 __all__ = ['CommandLineInterface', 'compile_catalog', 'extract_messages',
            'init_catalog', 'check_message_extractors', 'update_catalog']
@@ -342,7 +343,7 @@ class extract_messages(Command):
         elif getattr(self.distribution, 'message_extractors', None):
             message_extractors = self.distribution.message_extractors
             for dirname, mapping in message_extractors.items():
-                if isinstance(mapping, basestring):
+                if isinstance(mapping, string_types):
                     method_map, options_map = parse_mapping(StringIO(mapping))
                 else:
                     method_map, options_map = [], {}
index 2004f8468b104057a3206b2edb55a2c98111d9e9..50f999bdd338e9e0aea5339108e3cd7798d60aa2 100644 (file)
@@ -179,7 +179,7 @@ class odict(dict):
     """
     def __init__(self, data=None):
         dict.__init__(self, data or {})
-        self._keys = dict.keys(self)
+        self._keys = list(dict.keys(self))
 
     def __delitem__(self, key):
         dict.__delitem__(self, key)
@@ -254,7 +254,6 @@ except AttributeError:
         '../foo/bar.txt'
 
         :return: the relative path
-        :rtype: `basestring`
         """
         start_list = os.path.abspath(start).split(os.sep)
         path_list = os.path.abspath(path).split(os.sep)
index b5ba358871c6296fa4a12f1b2dc07a588b379cd1..fcac34d3da4c744647b560f272ba8c00686f31b8 100644 (file)
 
 import copy
 import datetime
-import doctest
 import unittest
 
-from babel.dates import format_datetime
+from babel.dates import format_datetime, UTC
 from babel.messages import catalog
 from babel.util import FixedOffsetTimezone
 
@@ -351,7 +350,7 @@ def test_catalog():
 
 
 def test_catalog_mime_headers():
-    created = datetime.datetime(1990, 4, 1, 15, 30, tzinfo=catalog.UTC)
+    created = datetime.datetime(1990, 4, 1, 15, 30, tzinfo=UTC)
     cat = catalog.Catalog(project='Foobar', version='1.0',
                           creation_date=created)
     assert cat.mime_headers == [
@@ -369,8 +368,8 @@ def test_catalog_mime_headers():
 
 
 def test_catalog_mime_headers_set_locale():
-    created = datetime.datetime(1990, 4, 1, 15, 30, tzinfo=catalog.UTC)
-    revised = datetime.datetime(1990, 8, 3, 12, 0, tzinfo=catalog.UTC)
+    created = datetime.datetime(1990, 4, 1, 15, 30, tzinfo=UTC)
+    revised = datetime.datetime(1990, 8, 3, 12, 0, tzinfo=UTC)
     cat = catalog.Catalog(locale='de_DE', project='Foobar', version='1.0',
                           creation_date=created, revision_date=revised,
                           last_translator='John Doe <jd@example.com>',
@@ -454,4 +453,4 @@ def test_catalog_update():
     assert msg3.locations == [('util.py', 42)]
 
     assert not 'head' in cat
-    assert cat.obsolete.values()[0].id == 'head'
+    assert list(cat.obsolete.values())[0].id == 'head'