Repository navigation
Fix unicode printing error on Python 2 without TTY. - #691
Merged
akx merged 2 commits intoJan 28, 2020
Merged
Conversation
4 of 10 tasks
Codecov Report
@@ Coverage Diff @@
## master #691 +/- ##
=======================================
Coverage 90.97% 90.97%
=======================================
Files 24 24
Lines 4175 4175
=======================================
Hits 3798 3798
Misses 377 377
Continue to review full report at Codecov.
|
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
force-pushed
the
testsuite-fix-unicode-error-printing
branch
from
January 28, 2020 02:01
ce2185d to
da7f311
Compare
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Until now, on Python 2.7,
python setup.py test | catcrashed in the testrunner with
The test suite would show this when printing the
àin the test pofile contentsBut 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.encodingisNone, which is the default for Python 2 whensys.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 | catpasses (e.g. on my Ubuntu 18.04).This was found as part of NixOS/nixpkgs#75676.