summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2020-01-28 08:42:24 +0200
committerGitHub <noreply@github.com>2020-01-28 08:42:24 +0200
commit1d06afcdfe0159160e131f69cfd79b9af2a87186 (patch)
treea0e7f79452e75718cb850cfbe30a5ac986bc4e4b
parent4fa749b918810b52a63b312d82e4003e24db0406 (diff)
parentda7f31143847659b6b74d802618b03438aceb350 (diff)
downloadbabel-1d06afcdfe0159160e131f69cfd79b9af2a87186.tar.gz
Merge pull request #691 from nh2/testsuite-fix-unicode-error-printing
Fix unicode printing error on Python 2 without TTY.
-rw-r--r--babel/messages/pofile.py8
-rw-r--r--tests/messages/test_pofile.py2
2 files changed, 7 insertions, 3 deletions
diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py
index 93b0697..b86dd40 100644
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -178,7 +178,7 @@ class PoFileParser(object):
string = ['' for _ in range(self.catalog.num_plurals)]
for idx, translation in self.translations:
if idx >= self.catalog.num_plurals:
- self._invalid_pofile("", self.offset, "msg has more translations than num_plurals of catalog")
+ self._invalid_pofile(u"", self.offset, "msg has more translations than num_plurals of catalog")
continue
string[idx] = translation.denormalize()
string = tuple(string)
@@ -319,10 +319,14 @@ class PoFileParser(object):
self._add_message()
def _invalid_pofile(self, line, lineno, msg):
+ assert isinstance(line, text_type)
if self.abort_invalid:
raise PoFileError(msg, self.catalog, line, lineno)
print("WARNING:", msg)
- print(u"WARNING: Problem on line {0}: {1}".format(lineno + 1, line))
+ # `line` is guaranteed to be unicode so u"{}"-interpolating would always
+ # succeed, but on Python < 2 if not in a TTY, `sys.stdout.encoding`
+ # is `None`, unicode may not be printable so we `repr()` to ASCII.
+ print(u"WARNING: Problem on line {0}: {1}".format(lineno + 1, repr(line)))
def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=None, abort_invalid=False):
diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py
index e77fa6e..214ddf5 100644
--- a/tests/messages/test_pofile.py
+++ b/tests/messages/test_pofile.py
@@ -480,7 +480,7 @@ msgstr[2] "Vohs [text]"
def test_invalid_pofile_with_abort_flag(self):
parser = pofile.PoFileParser(None, abort_invalid=True)
lineno = 10
- line = 'Algo esta mal'
+ line = u'Algo esta mal'
msg = 'invalid file'
with self.assertRaises(pofile.PoFileError) as e:
parser._invalid_pofile(line, lineno, msg)