Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape unencodable messages #261

Merged
merged 1 commit into from
Feb 11, 2022

Conversation

kri-k
Copy link
Contributor

@kri-k kri-k commented Feb 4, 2022

PR for #260

Problem:
teamcity-messages plugin fails in cases when the sys.stdout encoding is unable to handle passed symbols.
This mostly happens on Windows hosts due to the behavior, described in the documentation:

On Windows, UTF-8 is used for the console device. Non-character devices such as disk files and pipes use the system locale encoding (i.e. the ANSI codepage).

How to reproduce:
Let's say we have a test.py file with the following content:

import sys
import unicodedata
def test_bad_encoding():
    bad_symbol = unicodedata.lookup('REPLACEMENT CHARACTER')
    sys.stdout.write(bad_symbol)

If we run it on Windows

python3 -m pytest test.py --teamcity > test_log

then most probably encoding of the sys.stdout will be cp1252 or something like that.
And test_log will contain the following error:

<...>
PASSED                                        [100%]
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "...\site-packages\_pytest\main.py", line 269, in wrap_session
<...>
INTERNALERROR>   File "...\site-packages\teamcity\messages.py", line 187, in testStdOut
INTERNALERROR>     self.message('testStdOut', name=testName, out=out, flowId=flowId)
INTERNALERROR>   File "...\site-packages\teamcity\messages.py", line 101, in message
INTERNALERROR>     retry_on_EAGAIN(self.output.write)(self.encode(message))
INTERNALERROR>   File "...\site-packages\teamcity\messages.py", line 68, in encode
INTERNALERROR>     value = value.encode(self.encoding)
INTERNALERROR>   File "...\encodings\cp1252.py", line 12, in encode
INTERNALERROR>     return codecs.charmap_encode(input,errors,encoding_table)
INTERNALERROR> UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 132: character maps to <undefined>

What does this pull request do:
Let's look how bad test output is handled by the pytest itself.
Let's add assert False line to the test and look at the result: python3 -m pytest test.py > test_log

test.py F                                                                [100%]

================================== FAILURES ===================================
______________________________ test_bad_encoding ______________________________

    def test_bad_encoding():
        bad_symbol = unicodedata.lookup('REPLACEMENT CHARACTER')
        sys.stdout.write(bad_symbol)
>       assert False
E       assert False

test.py:6: AssertionError
---------------------------- Captured stdout call -----------------------------
\ufffd
=========================== short test summary info ===========================
FAILED test.py::test_bad_encoding - assert False
============================== 1 failed in 0.08s ==============================

Captured stdout is escaped.
This pull request is for the teamcity-plugin to similarly escape unencodable characters.

@kri-k
Copy link
Contributor Author

kri-k commented Feb 9, 2022

Pypy 3 (Linux)
Result: | Tests failed: 14 (14 new), passed: 123, ignored: 26

I'm pretty sure it's not my fault 😃
master branch is also broken, as I can see

@mikekidya
Copy link
Member

I'm pretty sure it's not my fault 😃
master branch is also broken, as I can see

Yes, looking into it. Looks like there are some failures at new PyPy version (

Anyway, changes looks safe to me, let's merge them
Thank you for you change!

@mikekidya mikekidya merged commit 06013f8 into JetBrains:master Feb 11, 2022
@kri-k
Copy link
Contributor Author

kri-k commented Feb 14, 2022

@mikekidya I'm glad I could help with this bugfix.
BTW, are there any deadlines when the new version of package will be released on pypi?
It would be great if a new version released soon :)

@kri-k kri-k deleted the unicode-encode-error/0 branch February 14, 2022 11:54
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.

2 participants