def win_get_unicode_stream(stream, excepted_fileno, output_handle, encoding):
"""Returns a unicode-compatible stream.
This function will return a direct-Console writing object only if:
- the file number is the expected console file number
- the handle the expected file handle
- the 'real' handle is in fact a handle to a console.
"""
old_fileno = getattr(stream, 'fileno', lambda: None)()
if old_fileno == excepted_fileno:
# These types are available on linux but not Mac.
# pylint: disable=no-name-in-module,F0401
from ctypes import windll, WINFUNCTYPE
from ctypes.wintypes import DWORD, HANDLE
# <http://msdn.microsoft.com/en-us/library/ms683231.aspx>
GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(('GetStdHandle', windll.kernel32))
real_output_handle = GetStdHandle(DWORD(output_handle))
if win_handle_is_a_console(real_output_handle):
# It's a console.
return WinUnicodeConsoleOutput(
real_output_handle, old_fileno, stream.name, encoding)
# It's something else. Create an auto-encoding stream.
return WinUnicodeOutput(stream, old_fileno, encoding)
评论列表
文章目录