From 372f4878a649a157359315a1e7d550955132f356 Mon Sep 17 00:00:00 2001 From: Alex Morega Date: Sat, 6 Jul 2013 19:30:11 +0200 Subject: [PATCH] fix some string/bytes issues in catalog --- babel/_compat.py | 3 +++ babel/messages/catalog.py | 2 +- babel/messages/mofile.py | 28 ++++++++++++++-------------- tests/messages/test_mofile.py | 11 ++++++----- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/babel/_compat.py b/babel/_compat.py index b03fc64d..0a26e8e9 100644 --- a/babel/_compat.py +++ b/babel/_compat.py @@ -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,) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 0b49f8ef..1777585a 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -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'] diff --git a/babel/messages/mofile.py b/babel/messages/mofile.py index 313e3b46..f355c732 100644 --- a/babel/messages/mofile.py +++ b/babel/messages/mofile.py @@ -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. diff --git a/tests/messages/test_mofile.py b/tests/messages/test_mofile.py index 835891d4..c381cec1 100644 --- a/tests/messages/test_mofile.py +++ b/tests/messages/test_mofile.py @@ -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) -- 2.47.2