diff options
author | Jun Omae <jun66j5@gmail.com> | 2015-08-04 20:15:25 +0900 |
---|---|---|
committer | Erick Wilder <erickwilder@gmail.com> | 2015-08-24 14:34:48 -0300 |
commit | efd3f6303be28960394f93d16229f6c6088f92ff (patch) | |
tree | 2cb70b13b15099155070b60e341825782b058d8f | |
parent | aa5b28d3c59c2fa811e929cf8158b4f942faf86e (diff) | |
download | babel-efd3f6303be28960394f93d16229f6c6088f92ff.tar.gz |
Removed uses of datetime.date class from *.dat files (#174)
To avoid incompatible *.dat files between Python 2 and 3.
Added unit tests for that *.dat files have only babel classes
Author: Jun Omae <jun66j5@gmail.com>
-rw-r--r-- | babel/numbers.py | 4 | ||||
-rwxr-xr-x | scripts/import_cldr.py | 6 | ||||
-rw-r--r-- | tests/test_core.py | 26 |
3 files changed, 32 insertions, 4 deletions
diff --git a/babel/numbers.py b/babel/numbers.py index 587d640..d7a20d1 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -134,6 +134,10 @@ def get_territory_currencies(territory, start_date=None, end_date=None, result = [] for currency_code, start, end, is_tender in curs: + if start: + start = date_(*start) + if end: + end = date_(*end) if ((is_tender and tender) or \ (not is_tender and non_tender)) and _is_active(start, end): if include_details: diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index 3a2f121..a9350ae 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -21,8 +21,6 @@ try: except ImportError: from xml.etree import ElementTree -from datetime import date - # Make sure we're using Babel source, and not some previously installed version sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..')) @@ -103,12 +101,12 @@ def _parse_currency_date(s): if not s: return None parts = s.split('-', 2) - return date(*map(int, parts + [1] * (3 - len(parts)))) + return tuple(map(int, parts + [1] * (3 - len(parts)))) def _currency_sort_key(tup): code, start, end, tender = tup - return int(not tender), start or date(1, 1, 1) + return int(not tender), start or (1, 1, 1) def main(): diff --git a/tests/test_core.py b/tests/test_core.py index ac2611d..58118aa 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -269,3 +269,29 @@ def test_parse_locale(): assert core.parse_locale('en_US.UTF-8') == ('en', 'US', None, None) assert (core.parse_locale('de_DE.iso885915@euro') == ('de', 'DE', None, None)) + +def test_compatible_classes_in_global_and_localedata(): + # Use pickle module rather than cPickle since cPickle.Unpickler is a method + # on Python 2 + import pickle + + class Unpickler(pickle.Unpickler): + def find_class(self, module, name): + # *.dat files must have compatible classes between Python 2 and 3 + if module.split('.')[0] == 'babel': + return pickle.Unpickler.find_class(self, module, name) + raise pickle.UnpicklingError("global '%s.%s' is forbidden" % + (module, name)) + + def load(filename): + with open(filename, 'rb') as f: + return Unpickler(f).load() + + load('babel/global.dat') + load('babel/localedata/root.dat') + load('babel/localedata/en.dat') + load('babel/localedata/en_US.dat') + load('babel/localedata/en_US_POSIX.dat') + load('babel/localedata/zh_Hans_CN.dat') + load('babel/localedata/zh_Hant_TW.dat') + load('babel/localedata/es_419.dat') |