pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.94k stars 2.65k forks source link

stdout capturing breaks sys.stdout.encoding #4389

Open mgedmin opened 5 years ago

mgedmin commented 5 years ago

py.test by default replaces sys.stdout with something that captures test output, which is great! But the replacement stdout doesn't have a correct encoding attribute, and that can break the code under test.

(Prior to Python 3.6 sys.stdout.encoding is the only way to figure out the current OEM encoding on Windows, which you need if you have to decode the output of subprocesses. You can see what happens when people try to use py.test on such code here: https://github.com/mgedmin/check-manifest/pull/92)

Here's a reproducible test case for Linux (note that you need to use Python 3.5 or older):

# test.py
import sys

def test_stdout_encoding():
    assert sys.stdout.encoding == 'ANSI_X3.4-1968'
LC_ALL=C py.test test.py -s     # succeeds
LC_ALL=C py.test test.py        # fails, sys.stdout.encoding is 'UTF8'
$ pip list|grep pytest
pytest                        3.9.3        
pytest-catchlog               1.2.2        /home/mg/src/pytest-catchlog
pytest-forked                 0.2          
pytest-xdist                  1.24.0       

This is similar to #2375, except I'm not using the capsys fixture.

blueyed commented 5 years ago

Thanks for the report. https://github.com/pytest-dev/pytest/pull/4398 should fix this.