Pytest: `sys.stdout.reconfigure` raises AttributeError: '_io.FileIO' object has no attribute 'reconfigure'

Created on 27 Feb 2019  路  3Comments  路  Source: pytest-dev/pytest

Given python file test_1.py

import sys
sys.stdout.reconfigure(encoding='utf-8')

class Test1(object):
    def test_1(self):
        return True

then run command

python -m pytest -v test_1.py

results in

================================ test session starts ================================
platform win32 -- Python 3.7.1, pytest-4.2.0, py-1.7.0, pluggy-0.8.1 -- C:\Users\user1\.virtualenvs\project1-rAPKj25Z\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\user1\Projects\project1, inifile:
plugins: dependency-0.4.0, cov-2.6.1
collected 0 items / 1 errors

====================================== ERRORS =======================================
_______________________ ERROR collecting pytestbug1/test_1.py _______________________
test_1.py:2: in <module>
    sys.stdout.reconfigure(encoding='utf-8')
..\..\..\..\.virtualenvs\project1-rAPKj25Z\lib\site-packages\_pytest\capture.py:427: in __getattr__
    return getattr(object.__getattribute__(self, "buffer"), name)
E   AttributeError: '_io.FileIO' object has no attribute 'reconfigure'
!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.18 seconds ==============================


Similar AttributeError as in Issue #4389 but caused by the io.TextIOWrapper.requires function (which is new in Python 3.7).

capture bug

Most helpful comment

For anyone that needs it, a workaround that I used is

if not 'pytest' in sys.modules:  # workaround for https://github.com/pytest-dev/pytest/issues/4843
    sys.stdout.reconfigure(encoding='utf-8', errors='namereplace')
    sys.stderr.reconfigure(encoding='utf-8', errors='namereplace')

All 3 comments

For anyone that needs it, a workaround that I used is

if not 'pytest' in sys.modules:  # workaround for https://github.com/pytest-dev/pytest/issues/4843
    sys.stdout.reconfigure(encoding='utf-8', errors='namereplace')
    sys.stderr.reconfigure(encoding='utf-8', errors='namereplace')

Hi @jtmoon79,

That happens because sys.stdout is replaced by our own buffer in order to capture output, but that buffer does not support a few attributes which were added in Python 3.7:

  • write_through
  • reconfigure

Thanks for posting a workaround, btw!

Was this page helpful?
0 / 5 - 0 ratings