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 21 Sep 2002 20:22:32 -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: Lib/site.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/site.py,v retrieving revision 1.45 diff -u -r1.45 site.py --- Lib/site.py 19 Sep 2002 11:11:27 -0000 1.45 +++ Lib/site.py 21 Sep 2002 20:22:32 -0000 @@ -309,6 +309,50 @@ # On Non-Unicode builds this will raise an AttributeError... sys.setdefaultencoding(encoding) # Needs Python Unicode build ! +if sys.stdout.encoding: + import codecs + try: + codecs.lookup(sys.stdout.encoding) + except LookupError: + pass + else: + def _allowed(a): + return a in ('__repr__','__str__') or \ + (not a.startswith("_") and \ + not a in ('write','writelines','closed')) + + _fileslots = [] + for a in dir(sys.stdout): + if a in ('__repr__','__str__') or \ + (not a.startswith("_") and \ + not a in ('write','writelines','closed')): + _fileslots.append(a) + + class StdoutWrapper(object): + __slots__= _fileslots + ['stream'] + + def __init__(self): + self.stream = sys.stdout + for a in _fileslots: + setattr(self, a, getattr(sys.stdout, a)) + + def write(self, data): + if isinstance(data, unicode): + data = data.encode(self.stream.encoding) + return self.stream.write(data) + + def writelines(self, data): + for index, value in enumerate(data): + if isinstance(value, unicode): + data[index] = value.encode(self.stream.encoding) + print self.stream.writelines(data) + + def _getclosed(self): + return self.stream.closed + closed = property(_getclosed, doc="True if the file is closed") + + sys.stdout = StdoutWrapper() + # # Run custom site specific code, if available. # Index: Objects/fileobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v retrieving revision 2.168 diff -u -r2.168 fileobject.c --- Objects/fileobject.c 14 Aug 2002 21:01:41 -0000 2.168 +++ Objects/fileobject.c 21 Sep 2002 20:22:35 -0000 @@ -110,6 +110,7 @@ Py_DECREF(f->f_name); Py_DECREF(f->f_mode); + Py_DECREF(f->f_encoding); f->f_name = PyString_FromString(name); f->f_mode = PyString_FromString(mode); @@ -122,7 +123,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; @@ -286,6 +289,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); } @@ -1565,6 +1569,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 */ }; @@ -1753,6 +1759,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; } Index: Python/sysmodule.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/sysmodule.c,v retrieving revision 2.110 diff -u -r2.110 sysmodule.c --- Python/sysmodule.c 3 Sep 2002 20:10:45 -0000 2.110 +++ Python/sysmodule.c 21 Sep 2002 20:22:36 -0000 @@ -26,6 +26,15 @@ extern const char *PyWin_DLLVersionString; #endif +#ifdef MS_WINDOWS +#include +#endif + +#ifdef HAVE_LANGINFO_H +#include +#include +#endif + PyObject * PySys_GetObject(char *name) { @@ -744,6 +753,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); @@ -753,6 +768,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);