Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Prevent “unittest --buffer” from crashing
Before this change, if certain tests were failing in certain ways, then running “python -m unittest --buffer” would cause an AttributeError in the unittest module itself. Here’s what unittest does when you use the --buffer argument: 1. It sets sys.stdout and sys.stderr to StringIOs [1]. 2. It runs a test. 3. If the test failed, it runs getvalue() on sys.stdout and sys.stderr to get data from its StringIOs. tests/test_cli.py has its own RunContext class that does something similar. Before this change, here’s what could happen: 1. unittest sets sys.stdout and sys.stderr to StringIOs. 2. unittest runs a test that uses RunContext. 3. A RunContext gets entered. It sets sys.stdout and sys.stderr to its own StringIOs. 4. The RunContext gets exited. It sets sys.stdout and sys.stderr to sys.__stdout__ and sys.__stderr__. 5. The test fails. 6. unittest assumes that sys.stdout is still set to one of its StringIOs, and runs sys.stdout.getvalue(). 7. unittest crashes with this error: AttributeError: '_io.TextIOWrapper' object has no attribute 'getvalue' [1]: <https://github.com/python/cpython/blob/2305ca51448552542b2414186252123a8dc87db7/Lib/unittest/result.py#L65> [2]: <https://github.com/python/cpython/blob/2305ca51448552542b2414186252123a8dc87db7/Lib/unittest/result.py#L87>
- Loading branch information