From: Armin Ronacher Date: Sat, 6 Jul 2013 13:49:50 +0000 (+0200) Subject: Started work on cleanups for Python 3 X-Git-Tag: 1.0~97 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e00f5a1a84c98f2fd44752e7aae07852e0aa1b31;p=thirdparty%2Fbabel.git Started work on cleanups for Python 3 --- diff --git a/babel/__init__.py b/babel/__init__.py index 2e34f7c7..ae7d6ca9 100644 --- a/babel/__init__.py +++ b/babel/__init__.py @@ -26,7 +26,8 @@ This package is basically composed of two major parts: :see: http://www.unicode.org/cldr/ """ -from babel.core import * +from babel.core import UnknownLocaleError, Locale, default_locale, \ + negotiate_locale, parse_locale try: diff --git a/babel/_compat.py b/babel/_compat.py new file mode 100644 index 00000000..f7104928 --- /dev/null +++ b/babel/_compat.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2007-2011 Edgewall Software +# All rights reserved. +# +# This software is licensed as described in the file COPYING, which +# you should have received as part of this distribution. The terms +# are also available at http://babel.edgewall.org/wiki/License. +# +# This software consists of voluntary contributions made by many +# individuals. For the exact contribution history, see the revision +# history and logs, available at http://babel.edgewall.org/log/. + + +import sys + +PY2 = sys.version_info[0] == 2 + +_identity = lambda x: x + + +if not PY2: + text_type = str + string_types = (str,) + integer_types = (int, ) + + iterkeys = lambda d: iter(d.keys()) + itervalues = lambda d: iter(d.values()) + iteritems = lambda d: iter(d.items()) + + from io import StringIO + import cPickle as pickle + + def reraise(tp, value, tb=None): + if value.__traceback__ is not tb: + raise value.with_traceback(tb) + raise value + + implements_to_string = _identity + +else: + text_type = unicode + string_types = (str, unicode) + integer_types = (int, long) + + iterkeys = lambda d: d.iterkeys() + itervalues = lambda d: d.itervalues() + iteritems = lambda d: d.iteritems() + + from cStringIO import StringIO + import pickle + + exec('def reraise(tp, value, tb=None):\n raise tp, value, tb') + + def implements_to_string(cls): + cls.__unicode__ = cls.__str__ + cls.__str__ = lambda x: x.__unicode__().encode('utf-8') + return cls diff --git a/babel/compat.py b/babel/compat.py deleted file mode 100644 index c9d407a7..00000000 --- a/babel/compat.py +++ /dev/null @@ -1,18 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2007-2011 Edgewall Software -# All rights reserved. -# -# This software is licensed as described in the file COPYING, which -# you should have received as part of this distribution. The terms -# are also available at http://babel.edgewall.org/wiki/License. -# -# This software consists of voluntary contributions made by many -# individuals. For the exact contribution history, see the revision -# history and logs, available at http://babel.edgewall.org/log/. - -try: - import threading -except ImportError: - import dummy_threading as threading - diff --git a/babel/core.py b/babel/core.py index dc36a06c..4c531259 100644 --- a/babel/core.py +++ b/babel/core.py @@ -14,9 +14,9 @@ """Core locale representation and locale data access.""" import os -import cPickle as pickle from babel import localedata +from babel._compat import pickle __all__ = ['UnknownLocaleError', 'Locale', 'default_locale', 'negotiate_locale', 'parse_locale'] diff --git a/babel/localedata.py b/babel/localedata.py index ee63a07a..7257f6fd 100644 --- a/babel/localedata.py +++ b/babel/localedata.py @@ -18,10 +18,10 @@ """ import os -import cPickle as pickle +import threading from UserDict import DictMixin -from babel.compat import threading +from babel._compat import pickle __all__ = ['exists', 'locale_identifiers', 'load'] diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index 1ebb06be..6586a044 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -12,7 +12,6 @@ # individuals. For the exact contribution history, see the revision # history and logs, available at http://babel.edgewall.org/log/. -import cPickle as pickle from optparse import OptionParser import os import re @@ -25,6 +24,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..')) from babel import dates, numbers from babel.plural import PluralRule from babel.localedata import Alias +from babel._compat import pickle parse = ElementTree.parse weekdays = {'mon': 0, 'tue': 1, 'wed': 2, 'thu': 3, 'fri': 4, 'sat': 5, @@ -421,7 +421,7 @@ def main(): try: date_formats[elem.attrib.get('type')] = \ dates.parse_pattern(unicode(elem.findtext('dateFormat/pattern'))) - except ValueError, e: + except ValueError as e: error(e) elif elem.tag == 'alias': date_formats = Alias(_translate_alias( @@ -438,7 +438,7 @@ def main(): try: time_formats[elem.attrib.get('type')] = \ dates.parse_pattern(unicode(elem.findtext('timeFormat/pattern'))) - except ValueError, e: + except ValueError as e: error(e) elif elem.tag == 'alias': time_formats = Alias(_translate_alias( @@ -455,7 +455,7 @@ def main(): try: datetime_formats[elem.attrib.get('type')] = \ unicode(elem.findtext('dateTimeFormat/pattern')) - except ValueError, e: + except ValueError as e: error(e) elif elem.tag == 'alias': datetime_formats = Alias(_translate_alias(