From: Armin Ronacher Date: Sat, 6 Jul 2013 14:21:12 +0000 (+0200) Subject: Made another pass on the Python 3 support X-Git-Tag: 1.0~95 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a72331abd904d9080c3e3fd25eb730baed55c270;p=thirdparty%2Fbabel.git Made another pass on the Python 3 support --- diff --git a/babel/_compat.py b/babel/_compat.py index c4e04478..f8248c5f 100644 --- a/babel/_compat.py +++ b/babel/_compat.py @@ -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,) diff --git a/babel/core.py b/babel/core.py index 4c531259..87e49217 100644 --- a/babel/core.py +++ b/babel/core.py @@ -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 diff --git a/babel/dates.py b/babel/dates.py index ef1d169d..a69adc84 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -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) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 0d454e57..25fe92c9 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -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) diff --git a/babel/messages/checkers.py b/babel/messages/checkers.py index 2b8fa4fa..86dc5bb7 100644 --- a/babel/messages/checkers.py +++ b/babel/messages/checkers.py @@ -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 diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index 753151eb..a0640eba 100755 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -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 = [], {} diff --git a/babel/util.py b/babel/util.py index 2004f846..50f999bd 100644 --- a/babel/util.py +++ b/babel/util.py @@ -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) diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index b5ba3588..fcac34d3 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -13,10 +13,9 @@ 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 ', @@ -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'