This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [win32] stderr atty encoding not set
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: loewis Nosy List: loewis, snaury
Priority: high Keywords:

Created on 2006-02-01 17:25 by snaury, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg27395 - (view) Author: Alexey Borzenkov (snaury) Date: 2006-02-01 17:25
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).
msg27396 - (view) Author: Alexey Borzenkov (snaury) Date: 2006-02-01 17:29
Logged In: YES 
user_id=1197042

Ooops, sorry, in the example it should be:

print >>sys.stderr, u"<native text>"
msg27397 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2006-04-03 10:57
Logged In: YES 
user_id=21627

Thanks for the report. This is now fixed in #43581.
History
Date User Action Args
2022-04-11 14:56:15adminsetgithub: 42853
2006-02-01 17:25:29snaurycreate