Index: Include/fileobject.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/fileobject.h,v retrieving revision 2.31 diff -u -r2.31 fileobject.h --- Include/fileobject.h 12 Aug 2002 07:21:56 -0000 2.31 +++ Include/fileobject.h 26 Oct 2002 17:43:17 -0000 @@ -24,6 +24,7 @@ int f_newlinetypes; /* Types of newlines seen */ int f_skipnextlf; /* Skip next \n */ #endif + PyObject *f_encoding; } PyFileObject; PyAPI_DATA(PyTypeObject) PyFile_Type; Index: Objects/fileobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v retrieving revision 2.169 diff -u -r2.169 fileobject.c --- Objects/fileobject.c 3 Oct 2002 05:10:39 -0000 2.169 +++ Objects/fileobject.c 26 Oct 2002 17:43:17 -0000 @@ -116,6 +116,7 @@ Py_DECREF(f->f_name); Py_DECREF(f->f_mode); + Py_DECREF(f->f_encoding); if (wname) f->f_name = PyUnicode_FromObject(wname); else @@ -131,7 +132,9 @@ f->f_newlinetypes = NEWLINE_UNKNOWN; f->f_skipnextlf = 0; #endif - + Py_INCREF(Py_None); + f->f_encoding = Py_None; + if (f->f_name == NULL || f->f_mode == NULL) return NULL; f->f_fp = fp; @@ -321,6 +324,7 @@ } Py_XDECREF(f->f_name); Py_XDECREF(f->f_mode); + Py_XDECREF(f->f_encoding); drop_readahead(f); f->ob_type->tp_free((PyObject *)f); } @@ -1613,6 +1617,8 @@ "file mode ('r', 'w', 'a', possibly with 'b' or '+' added)"}, {"name", T_OBJECT, OFF(f_name), RO, "file name"}, + {"encoding", T_OBJECT, OFF(f_encoding), RO, + "file encoding"}, /* getattr(f, "closed") is implemented without this table */ {NULL} /* Sentinel */ }; @@ -1801,6 +1807,8 @@ ((PyFileObject *)self)->f_name = not_yet_string; Py_INCREF(not_yet_string); ((PyFileObject *)self)->f_mode = not_yet_string; + Py_INCREF(Py_None); + ((PyFileObject *)self)->f_encoding = Py_None; } return self; } @@ -1983,11 +1991,28 @@ } else if (PyFile_Check(f)) { FILE *fp = PyFile_AsFile(f); + PyObject *enc = ((PyFileObject*)f)->f_encoding; + int result; if (fp == NULL) { err_closed(); return -1; } +#ifdef Py_USING_UNICODE + if (PyUnicode_Check(v) && enc != Py_None) { + char *cenc = PyString_AS_STRING(enc); + value = PyUnicode_AsEncodedString(v, cenc, "strict"); + if (value == NULL) + return -1; + } else { + value = v; + Py_INCREF(value); + } + result = PyObject_Print(value, fp, flags); + Py_DECREF(value); + return result; +#else return PyObject_Print(v, fp, flags); +#endif } writer = PyObject_GetAttrString(f, "write"); if (writer == NULL) Index: Python/sysmodule.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/sysmodule.c,v retrieving revision 2.111 diff -u -r2.111 sysmodule.c --- Python/sysmodule.c 8 Oct 2002 02:44:28 -0000 2.111 +++ Python/sysmodule.c 26 Oct 2002 17:43:17 -0000 @@ -31,6 +31,15 @@ extern const char *PyWin_DLLVersionString; #endif +#ifdef MS_WINDOWS +#include +#endif + +#ifdef HAVE_LANGINFO_H +#include +#include +#endif + PyObject * PySys_GetObject(char *name) { @@ -781,6 +790,12 @@ 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); @@ -790,6 +805,38 @@ syserr = PyFile_FromFile(stderr, "", "w", NULL); if (PyErr_Occurred()) return NULL; +#ifdef MS_WINDOWS + if(isatty(_fileno(stdin))){ + sprintf(buf, "cp%d", GetConsoleCP()); + Py_DECREF(((PyFileObject*)sysin)->f_encoding); + if((((PyFileObject*)sysin)->f_encoding = PyString_FromString(buf)) == NULL) + return NULL; + } + if(isatty(_fileno(stdout))) { + sprintf(buf, "cp%d", GetConsoleOutputCP()); + Py_DECREF(((PyFileObject*)sysout)->f_encoding); + if((((PyFileObject*)sysout)->f_encoding = PyString_FromString(buf)) == NULL) + 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))){ + Py_DECREF(((PyFileObject*)sysin)->f_encoding); + if((((PyFileObject*)sysin)->f_encoding = PyString_FromString(codeset)) == NULL) + return NULL; + } + if(codeset && isatty(fileno(stdout))) { + Py_DECREF(((PyFileObject*)sysout)->f_encoding); + if((((PyFileObject*)sysout)->f_encoding = PyString_FromString(codeset)) == NULL) + return NULL; + } +#endif + PyDict_SetItemString(sysdict, "stdin", sysin); PyDict_SetItemString(sysdict, "stdout", sysout); PyDict_SetItemString(sysdict, "stderr", syserr);