summaryrefslogtreecommitdiffstats
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2003-05-10 07:10:12 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2003-05-10 07:10:12 (GMT)
commit5467d4c0e31e9db305a4899a44d7978f83e96649 (patch)
treecf52a41492d6c1271a4f32ace0a62237daceb63a /Python/sysmodule.c
parentb7b4ce27f74901258f0b3af1fb9483d8f38feab8 (diff)
downloadcpython-5467d4c0e31e9db305a4899a44d7978f83e96649.zip
cpython-5467d4c0e31e9db305a4899a44d7978f83e96649.tar.gz
cpython-5467d4c0e31e9db305a4899a44d7978f83e96649.tar.bz2
Patch #612627: Add encoding attribute to file objects, and determine
the terminal encoding on Windows and Unix.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index d06d18a..edbc2bf 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -36,6 +36,15 @@ extern const char *PyWin_DLLVersionString;
#include <unixlib.h>
#endif
+#ifdef MS_WINDOWS
+#include <windows.h>
+#endif
+
+#ifdef HAVE_LANGINFO_H
+#include <locale.h>
+#include <langinfo.h>
+#endif
+
PyObject *
PySys_GetObject(char *name)
{
@@ -881,6 +890,12 @@ _PySys_Init(void)
PyObject *m, *v, *sysdict;
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);
sysdict = PyModule_GetDict(m);
@@ -890,6 +905,34 @@ _PySys_Init(void)
syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
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);
PyDict_SetItemString(sysdict, "stderr", syserr);