[Python-checkins] python/dist/src/Python sysmodule.c,2.118,2.119
loewis@users.sourceforge.net
loewis@users.sourceforge.net
Sat, 10 May 2003 00:08:52 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv10809/Python
Modified Files:
sysmodule.c
Log Message:
Patch #612627: Add encoding attribute to file objects, and determine
the terminal encoding on Windows and Unix.
Index: sysmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/sysmodule.c,v
retrieving revision 2.118
retrieving revision 2.119
diff -C2 -d -r2.118 -r2.119
*** sysmodule.c 3 May 2003 09:14:54 -0000 2.118
--- sysmodule.c 10 May 2003 07:08:50 -0000 2.119
***************
*** 37,40 ****
--- 37,49 ----
#endif
+ #ifdef MS_WINDOWS
+ #include <windows.h>
+ #endif
+
+ #ifdef HAVE_LANGINFO_H
+ #include <locale.h>
+ #include <langinfo.h>
+ #endif
+
PyObject *
PySys_GetObject(char *name)
***************
*** 882,885 ****
--- 891,900 ----
PyObject *sysin, *sysout, *syserr;
char *s;
+ #ifdef MS_WINDOWS
+ char buf[10];
+ #endif
+ #if defined(HAVE_LANGINFO_H) && defined(CODESET)
+ char *oldloc, *codeset;
+ #endif
m = Py_InitModule3("sys", sys_methods, sys_doc);
***************
*** 891,894 ****
--- 906,937 ----
if (PyErr_Occurred())
return NULL;
+ #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
+
+ #if defined(HAVE_LANGINFO_H) && defined(CODESET)
+ oldloc = setlocale(LC_CTYPE, 0);
+ setlocale(LC_CTYPE, "");
+ codeset = nl_langinfo(CODESET);
+ setlocale(LC_CTYPE, oldloc);
+ if(codeset && isatty(fileno(stdin))){
+ if (!PyFile_SetEncoding(sysin, codeset))
+ return NULL;
+ }
+ if(codeset && isatty(fileno(stdout))) {
+ if (!PyFile_SetEncoding(sysout, codeset))
+ return NULL;
+ }
+ #endif
+
PyDict_SetItemString(sysdict, "stdin", sysin);
PyDict_SetItemString(sysdict, "stdout", sysout);