bpo-14156: Make argparse.FileType work correctly for binary file mode… by MojoVampire · Pull Request #13165 · python/cpython (original) (raw)
For the record, the failures in the Azure Pipelines environment appear to be caused by the tests being run in some manner that causes sys.stdout/sys.stderr to be replaced with io.StringIO before the tests even start. The tests work fine on AppVeyor and Travis CI because this isn't happening, and work fine locally; I'm having a hard time figuring out what the Azure Pipelines are doing that triggers the problem. It does look like a number of the test support packages will replace stdout/stderr with StringIO instances, but it's not obvious what I can do to trigger that behavior locally so I can try and fix it (possibly by using the same TextIOWrapper subclass I changed test_argparse to use precisely to support this use case.
Update: Apparently this only occurs in high verbosity/warning levels, which I hadn't been using when running locally. I've tweaked the tests to fallback to using a unique sentinel value for each buffer when the buffer doesn't exist (it's not really about the buffer existing, it's just a convenient way to confirm that the buffer was used without leaving sys.stdout/sys.stderr patched, so using sentinels is fine). The tests now pass in both verbose and non-verbose modes, and are run in both in different automated tests, so the behavior is definitely being fully tested either way.