Issue 1421664: [win32] stderr atty encoding not set (original) (raw)

When starting python console application under windows, output of unicode strings to console fails because of ansi encoding. I found that in file Python-2.4.2/Python/sysmodule, _PySys_Init functions, setting encoding on stderr was forgotten:

#ifdef MS_WINDOWS if(isatty(_fileno(stdin))){ sprintf(buf, "cp%d", GetConsoleCP()); if (!PyFile_SetEncoding(sysin, buf)) return NULL; } if(isatty(_fileno(stdout))) { sprintf(buf, "cp%d", GetConsoleOutputCP()); if (!PyFile_SetEncoding(sysout, buf)) return NULL; } #endif

I think the following lines should be added:

if(isatty(_fileno(stderr))) {
    sprintf(buf, "cp%d", GetConsoleOutputCP());
    if (!PyFile_SetEncoding(syserr, buf))
        return NULL;
}

Otherwise it's inconsistant, as if we set it to sysout, why not on syserr? And, for example, this code will not work properly:

#!/usr/bin/env python
# -*- encoding: mbcs -*-
import sys
reload(sys) # bring setdefaultencoding back!
sys.setdefaultencoding("mbcs")
sys.stderr.write(u"<native text>\n")

Instead of native text garbage will be printed (if you change it to sys.stdout, proper text displayed), and there is no way I know to properly determine and set encoding just for stderr (file.encoding is read-only, after all).