#NEWLINE# def fix_unicode_on_win32( ) : #NEWLINE# #INDENT# from ctypes import WINFUNCTYPE , windll , POINTER , byref , c_int #NEWLINE# from ctypes.wintypes import BOOL , HANDLE , DWORD , LPWSTR , LPCWSTR , LPVOID #NEWLINE# original_stderr = sys . stderr #NEWLINE# def _complain( message ) : #NEWLINE# #INDENT# print ( original_stderr , ( message if isinstance ( message , str ) else repr ( message ) ) ) #NEWLINE# #UNINDENT# codecs . register ( ( lambda name : ( ( ( name == 'cp65001' ) and codecs . lookup ( 'utf-8' ) ) or None ) ) ) #NEWLINE# try : #NEWLINE# #INDENT# GetStdHandle = WINFUNCTYPE ( HANDLE , DWORD ) ( ( 'GetStdHandle' , windll . kernel32 ) ) #NEWLINE# STD_OUTPUT_HANDLE = DWORD ( ( - 11 ) ) #NEWLINE# STD_ERROR_HANDLE = DWORD ( ( - 12 ) ) #NEWLINE# GetFileType = WINFUNCTYPE ( DWORD , DWORD ) ( ( 'GetFileType' , windll . kernel32 ) ) #NEWLINE# FILE_TYPE_CHAR = 2 #NEWLINE# FILE_TYPE_REMOTE = 32768 #NEWLINE# GetConsoleMode = WINFUNCTYPE ( BOOL , HANDLE , POINTER ( DWORD ) ) ( ( 'GetConsoleMode' , windll . kernel32 ) ) #NEWLINE# INVALID_HANDLE_VALUE = DWORD ( ( - 1 ) ) . value #NEWLINE# def not_a_console( handle ) : #NEWLINE# #INDENT# if ( ( handle == INVALID_HANDLE_VALUE ) or ( handle is None ) ) : #NEWLINE# #INDENT# return True #NEWLINE# #UNINDENT# return ( ( ( GetFileType ( handle ) & ( ~ FILE_TYPE_REMOTE ) ) != FILE_TYPE_CHAR ) or ( GetConsoleMode ( handle , byref ( DWORD ( ) ) ) == 0 ) ) #NEWLINE# #UNINDENT# old_stdout_fileno = None #NEWLINE# old_stderr_fileno = None #NEWLINE# if hasattr ( sys . stdout , 'fileno' ) : #NEWLINE# #INDENT# old_stdout_fileno = sys . stdout . fileno ( ) #NEWLINE# #UNINDENT# if hasattr ( sys . stderr , 'fileno' ) : #NEWLINE# #INDENT# old_stderr_fileno = sys . stderr . fileno ( ) #NEWLINE# #UNINDENT# STDOUT_FILENO = 1 #NEWLINE# STDERR_FILENO = 2 #NEWLINE# real_stdout = ( old_stdout_fileno == STDOUT_FILENO ) #NEWLINE# real_stderr = ( old_stderr_fileno == STDERR_FILENO ) #NEWLINE# if real_stdout : #NEWLINE# #INDENT# hStdout = GetStdHandle ( STD_OUTPUT_HANDLE ) #NEWLINE# if not_a_console ( hStdout ) : #NEWLINE# #INDENT# real_stdout = False #NEWLINE# #UNINDENT# #UNINDENT# if real_stderr : #NEWLINE# #INDENT# hStderr = GetStdHandle ( STD_ERROR_HANDLE ) #NEWLINE# if not_a_console ( hStderr ) : #NEWLINE# #INDENT# real_stderr = False #NEWLINE# #UNINDENT# #UNINDENT# if ( real_stdout or real_stderr ) : #NEWLINE# #INDENT# WriteConsoleW = WINFUNCTYPE ( BOOL , HANDLE , LPWSTR , DWORD , POINTER ( DWORD ) , LPVOID ) ( ( 'WriteConsoleW' , windll . kernel32 ) ) #NEWLINE# class UnicodeOutput ( ) : #NEWLINE# #INDENT# def __init__( self , hConsole , stream , fileno , name ) : #NEWLINE# #INDENT# self . _hConsole = hConsole #NEWLINE# self . _stream = stream #NEWLINE# self . _fileno = fileno #NEWLINE# self . closed = False #NEWLINE# self . softspace = False #NEWLINE# self . mode = 'w' #NEWLINE# self . encoding = 'utf-8' #NEWLINE# self . name = name #NEWLINE# self . flush ( ) #NEWLINE# #UNINDENT# def isatty( self ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# def close( self ) : #NEWLINE# #INDENT# self . closed = True #NEWLINE# #UNINDENT# def fileno( self ) : #NEWLINE# #INDENT# return self . _fileno #NEWLINE# #UNINDENT# def flush( self ) : #NEWLINE# #INDENT# if ( self . _hConsole is None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# self . _stream . flush ( ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# _complain ( ( '%s.flush: %r from %r' % ( self . name , e , self . _stream ) ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# def write( self , text ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if ( self . _hConsole is None ) : #NEWLINE# #INDENT# if isinstance ( text , unicode ) : #NEWLINE# #INDENT# text = text . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# self . _stream . write ( text ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not isinstance ( text , unicode ) ) : #NEWLINE# #INDENT# text = str ( text ) . decode ( 'utf-8' ) #NEWLINE# #UNINDENT# remaining = len ( text ) #NEWLINE# while ( remaining > 0 ) : #NEWLINE# #INDENT# n = DWORD ( 0 ) #NEWLINE# retval = WriteConsoleW ( self . _hConsole , text , min ( remaining , 10000 ) , byref ( n ) , None ) #NEWLINE# if ( ( retval == 0 ) or ( n . value == 0 ) ) : #NEWLINE# #INDENT# raise IOError ( ( 'WriteConsoleW returned %r, n.value = %r' % ( retval , n . value ) ) ) #NEWLINE# #UNINDENT# remaining -= n . value #NEWLINE# if ( remaining == 0 ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# text = text [ n . value : ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# _complain ( ( '%s.write: %r' % ( self . name , e ) ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# #UNINDENT# def writelines( self , lines ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# for line in lines : #NEWLINE# #INDENT# self . write ( line ) #NEWLINE# #UNINDENT# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# _complain ( ( '%s.writelines: %r' % ( self . name , e ) ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# #UNINDENT# def __repr__( self ) : #NEWLINE# #INDENT# return self . name #NEWLINE# #UNINDENT# #UNINDENT# sys . stdout = ( UnicodeOutput ( hStdout , None , STDOUT_FILENO , '<Unicode console stdout>' ) if real_stdout else UnicodeOutput ( None , sys . stdout , old_stdout_fileno , '<Unicode redirected stdout>' ) ) #NEWLINE# sys . stderr = ( UnicodeOutput ( hStderr , None , STDERR_FILENO , '<Unicode console stderr>' ) if real_stderr else UnicodeOutput ( None , sys . stderr , old_stderr_fileno , '<Unicode redirected stderr>' ) ) #NEWLINE# #UNINDENT# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# _complain ( ( 'exception %r while fixing up sys.stdout and sys.stderr' % ( e , ) ) ) #NEWLINE# #UNINDENT# if UNMANGLE_CMDLINE_ARGS : #NEWLINE# #INDENT# GetCommandLineW = WINFUNCTYPE ( LPWSTR ) ( ( 'GetCommandLineW' , windll . kernel32 ) ) #NEWLINE# CommandLineToArgvW = WINFUNCTYPE ( POINTER ( LPWSTR ) , LPCWSTR , POINTER ( c_int ) ) ( ( 'CommandLineToArgvW' , windll . shell32 ) ) #NEWLINE# argc = c_int ( 0 ) #NEWLINE# argv_unicode = CommandLineToArgvW ( GetCommandLineW ( ) , byref ( argc ) ) #NEWLINE# argv = [ argv_unicode [ i ] . encode ( 'utf-8' ) for i in xrange ( 0 , argc . value ) ] #NEWLINE# if ( not hasattr ( sys , 'frozen' ) ) : #NEWLINE# #INDENT# argv = argv [ 1 : ] #NEWLINE# while ( len ( argv ) > 0 ) : #NEWLINE# #INDENT# arg = argv [ 0 ] #NEWLINE# if ( ( not arg . startswith ( '-' ) ) or ( arg == '-' ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# argv = argv [ 1 : ] #NEWLINE# if ( arg == '-m' ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# if ( arg == '-c' ) : #NEWLINE# #INDENT# argv [ 0 ] = '-c' #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# sys . argv = argv