]> git.ipfire.org Git - thirdparty/babel.git/commit
Fix unicode printing error on Python 2 without TTY. 691/head
authorNiklas Hambüchen <mail@nh2.me>
Mon, 27 Jan 2020 23:37:22 +0000 (00:37 +0100)
committerNiklas Hambüchen <mail@nh2.me>
Tue, 28 Jan 2020 02:00:20 +0000 (03:00 +0100)
commitda7f31143847659b6b74d802618b03438aceb350
treea0e7f79452e75718cb850cfbe30a5ac986bc4e4b
parentf4f6653e6aa053724d2c6dc0ee71dcb928013352
Fix unicode printing error on Python 2 without TTY.

Until now, on Python 2.7, `python setup.py test | cat` crashed in the test
runner with

    ======================================================================
    ERROR: test_abort_invalid_po_file (tests.messages.test_pofile.ReadPoTestCase)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "src/babel/tests/messages/test_pofile.py", line 458, in test_abort_invalid_po_file
        output = pofile.read_po(buf, locale='fr', abort_invalid=False)
      File "src/babel/babel/messages/pofile.py", line 377, in read_po
        parser.parse(fileobj)
      File "src/babel/babel/messages/pofile.py", line 310, in parse
        self._process_message_line(lineno, line)
      File "src/babel/babel/messages/pofile.py", line 210, in _process_message_line
        self._process_keyword_line(lineno, line, obsolete)
      File "src/babel/babel/messages/pofile.py", line 222, in _process_keyword_line
        self._invalid_pofile(line, lineno, "Start of line didn't match any expected keyword.")
      File "src/babel/babel/messages/pofile.py", line 325, in _invalid_pofile
        print(u"WARNING: Problem on line {0}: {1}".format(lineno + 1, line))
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 84: ordinal not in range(128)

The test suite would show this when printing the `à` in the test pofile contents

    Pour toute question, veuillez communiquer avec Fulano  à nadie@blah.com

But this bug is not confined to the test suite only. Any call to `read_po()`
with invalid .po file could trigger it in non-test code when
`sys.stdout.encoding` is `None`, which is the default for Python 2 when
`sys.stdout.isatty()` is false (as induced e.g. by `| cat`).

The fix is to `repr()` the line when printing the WARNING.
babel/messages/pofile.py