~lovesyao/bzr/windows-utf8

« back to all changes in this revision

Viewing changes to bzr

  • Committer: Nazo
  • Date: 2008-11-20 04:39:32 UTC
  • Revision ID: lovesyao@gmail.com-20081120043932-h5bdchn2vbmugd50
Fix mojibake of utf_8 character for stderr on Windows Command Prompt

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
    os.unsetenv(REINVOKE)
53
53
 
54
54
 
55
 
if sys.platform == 'win32' and sys.stdout.isatty():
 
55
if sys.platform == 'win32':
56
56
    from ctypes import windll, Structure, c_short, c_ushort, byref
57
57
    import codecs
58
58
 
80
80
        ]
81
81
 
82
82
    STD_OUTPUT_HANDLE = -11
 
83
    STD_ERROR_HANDLE = -12
83
84
 
84
85
    def consolereplace_errors(exception):
85
86
        if not isinstance(exception, UnicodeDecodeError):
92
93
    FOREGROUND_RED = 4
93
94
 
94
95
    class WindowsConsoleWriter:
95
 
        def __init__(self, errors='strict'):
 
96
        def __init__(self, stdhandle, errors='strict'):
96
97
            self.errors = errors
97
98
            self.encoding = 'utf_16'
98
 
            self.handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
 
99
            self.handle = windll.kernel32.GetStdHandle(stdhandle)
99
100
 
100
101
            csbi = CONSOLE_SCREEN_BUFFER_INFO()
101
102
            windll.kernel32.GetConsoleScreenBufferInfo(self.handle, byref(csbi))
131
132
 
132
133
        def flush(self): pass
133
134
 
134
 
    sys.stdout = WindowsConsoleWriter()
 
135
    if sys.stdout.isatty():
 
136
        sys.stdout = WindowsConsoleWriter(STD_OUTPUT_HANDLE)
 
137
    if sys.stderr.isatty():
 
138
        sys.stderr = WindowsConsoleWriter(STD_ERROR_HANDLE)
135
139
 
136
140
profiling = False
137
141
if '--profile-imports' in sys.argv: