def _py36_windowsconsoleio_workaround(stream):
if not sys.platform.startswith("<STR_LIT:win32>") or sys.version_info[:<NUM_LIT:2>] < (<NUM_LIT:3>, <NUM_LIT:6>):<EOL><INDENT>return<EOL><DEDENT>if not hasattr(stream, "<STR_LIT>"):<EOL><INDENT>return<EOL><DEDENT>buffered = hasattr(stream.buffer, "<STR_LIT>")<EOL>raw_stdout = stream.buffer.raw if buffered else stream.buffer<EOL>if not isinstance(raw_stdout, io._WindowsConsoleIO):<EOL><INDENT>return<EOL><DEDENT>def _reopen_stdio(f, mode):<EOL><INDENT>if not buffered and mode[<NUM_LIT:0>] == "<STR_LIT:w>":<EOL><INDENT>buffering = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>buffering = -<NUM_LIT:1><EOL><DEDENT>return io.TextIOWrapper(<EOL>open(os.dup(f.fileno()), mode, buffering),<EOL>f.encoding,<EOL>f.errors,<EOL>f.newlines,<EOL>f.line_buffering,<EOL>)<EOL><DEDENT>sys.stdin = _reopen_stdio(sys.stdin, "<STR_LIT:rb>")<EOL>sys.stdout = _reopen_stdio(sys.stdout, "<STR_LIT:wb>")<EOL>sys.stderr = _reopen_stdio(sys.stderr, "<STR_LIT:wb>")<EOL>
Python 3.6 implemented unicode console handling for Windows. This works
by reading/writing to the raw console handle using
``{Read,Write}ConsoleW``.

The problem is that we are going to ``dup2`` over the stdio file
descriptors when doing ``FDCapture`` and this will ``CloseHandle`` the
handles used by Python to write to the console. Though there is still some
weirdness and the console handle seems to only be closed randomly and not
on the first call to ``CloseHandle``, or maybe it gets reopened with the
same handle value when we suspend capturing.

The workaround in this case will reopen stdio with a different fd which
also means a different handle by replicating the logic in
"Py_lifecycle.c:initstdio/create_stdio".

:param stream: in practice ``sys.stdout`` or ``sys.stderr``, but given
    here as parameter for unittesting purposes.

See https://github.com/pytest-dev/py/issues/103