Skip to content

Fix unicode printing error on Python 2 without TTY. - #691

Merged
akx merged 2 commits into
python-babel:masterfrom
nh2:testsuite-fix-unicode-error-printing
Jan 28, 2020
Merged

akx merged 2 commits into
python-babel:masterfrom
nh2:testsuite-fix-unicode-error-printing

Conversation

@nh2

@nh2 nh2 commented Jan 27, 2020 •

Copy link
Copy Markdown
Contributor

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  à [email protected]

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.


With th above, python setup.py test | cat passes (e.g. on my Ubuntu 18.04).

This was found as part of NixOS/nixpkgs#75676.

@codecov-io

codecov-io commented Jan 27, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #691 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #691   +/-   ##
=======================================
  Coverage   90.97%   90.97%           
=======================================
  Files          24       24           
  Lines        4175     4175           
=======================================
  Hits         3798     3798           
  Misses        377      377
Impacted Files Coverage Δ
babel/messages/pofile.py 95.51% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4fa749b...da7f311. Read the comment docs.

nh2 added 2 commits January 28, 2020 02:58
This makes debugging and reasoning about the code easier;
otherwise it is surprising that sometimes `line` is a unicode
and sometimes not.

So far, when it was not, it could either be only `""` or
`'Algo esta mal'`; thus this commit makes those two u"" strings.
In all other cases, it was guaranteed that it's unicode,
because all code paths leading to `_invalid_pofile()` went through

    if not isinstance(line, text_type):
        line = line.decode(self.catalog.charset)

before.
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  à [email protected]

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.
@nh2
nh2 force-pushed the testsuite-fix-unicode-error-printing branch from ce2185d to da7f311 Compare January 28, 2020 02:01
@nh2

nh2 commented Jan 28, 2020 •

Copy link
Copy Markdown
Contributor Author

I've improved the approach slightly with the latest force-push (based on feedback from @neilmayhew), adding a commit that makes explicit what encoding guarantees we have where.

@neilmayhew neilmayhew left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@akx akx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you!

@akx
akx merged commit 1d06afc into python-babel:master Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants