]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Removed uses of datetime.date class from *.dat files (#174)
authorJun Omae <jun66j5@gmail.com>
Tue, 4 Aug 2015 11:15:25 +0000 (20:15 +0900)
committerErick Wilder <erickwilder@gmail.com>
Mon, 24 Aug 2015 17:34:48 +0000 (14:34 -0300)
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>

babel/numbers.py
scripts/import_cldr.py
tests/test_core.py

index 587d640706f5170f089f93ffb8e375a18d19ac1d..d7a20d1a7acb06fb71b4469a58c74fc6bdc040ef 100644 (file)
@@ -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:
index 3a2f1217ce3072e74b405c06849e6dbc93410c97..a9350ae50c26adafd17f2044ea18f393edc0f21d 100755 (executable)
@@ -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():
index ac2611dced3a6a7358ce436583e3c7befaffd98b..58118aa291a8e937671b5d0e2233604a16cbcc61 100644 (file)
@@ -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')