]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
fix some string/bytes issues in catalog
authorAlex Morega <alex@grep.ro>
Sat, 6 Jul 2013 17:30:11 +0000 (19:30 +0200)
committerAlex Morega <alex@grep.ro>
Sat, 6 Jul 2013 17:30:11 +0000 (19:30 +0200)
babel/_compat.py
babel/messages/catalog.py
babel/messages/mofile.py
tests/messages/test_mofile.py

index b03fc64d6629ad03e8047391cd3c65887950c7e7..0a26e8e9299dd8b2423a720901807845aa6bb804 100644 (file)
@@ -35,6 +35,7 @@ if not PY2:
     imap = map
     range_type = range
 
+    cmp = lambda a, b: (a > b) - (a < b)
 
 else:
     text_type = unicode
@@ -52,5 +53,7 @@ else:
     from itertools import izip, imap
     range_type = xrange
 
+    cmp = cmp
+
 
 number_types = integer_types + (float,)
index 0b49f8ef67f1cbf45eb41f86e88d550902609832..1777585a346cde803d227878ac49f9145ccb3024 100644 (file)
@@ -26,7 +26,7 @@ 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, FixedOffsetTimezone
-from babel._compat import string_types, number_types, PY2
+from babel._compat import string_types, number_types, PY2, cmp
 
 __all__ = ['Message', 'Catalog', 'TranslationError']
 
index 313e3b463173ee8d9a00f7707d63bde59b83df1e..f355c73235fe5e6c03affaf8ab021389e45151f1 100644 (file)
@@ -85,21 +85,21 @@ def read_mo(fileobj):
                 item = item.strip()
                 if not item:
                     continue
-                if ':' in item:
-                    key, value = item.split(':', 1)
+                if b':' in item:
+                    key, value = item.split(b':', 1)
                     lastkey = key = key.strip().lower()
                     headers[key] = value.strip()
                 elif lastkey:
-                    headers[lastkey] += '\n' + item
+                    headers[lastkey] += b'\n' + item
 
-        if '\x04' in msg: # context
-            ctxt, msg = msg.split('\x04')
+        if b'\x04' in msg: # context
+            ctxt, msg = msg.split(b'\x04')
         else:
             ctxt = None
 
-        if '\x00' in msg: # plural forms
-            msg = msg.split('\x00')
-            tmsg = tmsg.split('\x00')
+        if b'\x00' in msg: # plural forms
+            msg = msg.split(b'\x00')
+            tmsg = tmsg.split(b'\x00')
             if catalog.charset:
                 msg = [x.decode(catalog.charset) for x in msg]
                 tmsg = [x.decode(catalog.charset) for x in tmsg]
@@ -165,14 +165,14 @@ def write_mo(fileobj, catalog, use_fuzzy=False):
         messages[1:] = [m for m in messages[1:] if not m.fuzzy]
     messages.sort()
 
-    ids = strs = ''
+    ids = strs = b''
     offsets = []
 
     for message in messages:
         # For each string, we need size and file offset.  Each string is NUL
         # terminated; the NUL does not count into the size.
         if message.pluralizable:
-            msgid = '\x00'.join([
+            msgid = b'\x00'.join([
                 msgid.encode(catalog.charset) for msgid in message.id
             ])
             msgstrs = []
@@ -181,7 +181,7 @@ def write_mo(fileobj, catalog, use_fuzzy=False):
                     msgstrs.append(message.id[min(int(idx), 1)])
                 else:
                     msgstrs.append(string)
-            msgstr = '\x00'.join([
+            msgstr = b'\x00'.join([
                 msgstr.encode(catalog.charset) for msgstr in msgstrs
             ])
         else:
@@ -191,11 +191,11 @@ def write_mo(fileobj, catalog, use_fuzzy=False):
             else:
                 msgstr = message.string.encode(catalog.charset)
         if message.context:
-            msgid = '\x04'.join([message.context.encode(catalog.charset),
+            msgid = b'\x04'.join([message.context.encode(catalog.charset),
                                  msgid])
         offsets.append((len(ids), len(msgid), len(strs), len(msgstr)))
-        ids += msgid + '\x00'
-        strs += msgstr + '\x00'
+        ids += msgid + b'\x00'
+        strs += msgstr + b'\x00'
 
     # The header is 7 32-bit unsigned integers.  We don't use hash tables, so
     # the keys start right after the index tables.
index 835891d42e58925bd6c1de28fe349f8591d67406..c381cec18080c3246337fb95ebdbca61d3386fc3 100644 (file)
@@ -16,7 +16,7 @@ import os
 import unittest
 
 from babel.messages import mofile, Catalog
-from babel._compat import StringIO
+from babel._compat import BytesIO
 
 
 class ReadMoTestCase(unittest.TestCase):
@@ -25,8 +25,9 @@ class ReadMoTestCase(unittest.TestCase):
         self.datadir = os.path.join(os.path.dirname(__file__), 'data')
 
     def test_basics(self):
-        mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de',
-                                    'LC_MESSAGES', 'messages.mo'))
+        mo_path = os.path.join(self.datadir, 'project', 'i18n', 'de',
+                               'LC_MESSAGES', 'messages.mo')
+        mo_file = open(mo_path, 'rb')
         try:
             catalog = mofile.read_mo(mo_file)
             self.assertEqual(2, len(catalog))
@@ -53,7 +54,7 @@ class WriteMoTestCase(unittest.TestCase):
         catalog.add((u'There is', u'There are'), (u'Es gibt', u'Es gibt'))
         catalog.add(u'Fizz', '')
         catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
-        buf = StringIO()
+        buf = BytesIO()
         mofile.write_mo(buf, catalog)
         buf.seek(0)
         translations = gettext.GNUTranslations(fp=buf)
@@ -71,5 +72,5 @@ class WriteMoTestCase(unittest.TestCase):
     def test_more_plural_forms(self):
         catalog2 = Catalog(locale='ru_RU')
         catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
-        buf = StringIO()
+        buf = BytesIO()
         mofile.write_mo(buf, catalog2)