Mercurial > cpython-lru_cache-weakref
diff Python/fileutils.c @ 75353:c80d9a0bd5a0
Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
the os module.
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Wed, 29 Feb 2012 18:31:31 -0500 |
| parents | 33dea851f918 |
| children | 5db6d9ddf6e8 13dd8199c112 |
line wrap: on
line diff
--- a/Python/fileutils.c Wed Feb 29 17:47:21 2012 +0100 +++ b/Python/fileutils.c Wed Feb 29 18:31:31 2012 -0500 @@ -3,6 +3,40 @@ # include <windows.h> #endif +#ifdef HAVE_LANGINFO_H +#include <langinfo.h> +#endif + +PyObject * +_Py_device_encoding(int fd) +{ +#if defined(MS_WINDOWS) || defined(MS_WIN64) + UINT cp; +#endif + if (!_PyVerify_fd(fd) || !isatty(fd)) { + Py_RETURN_NONE; + } +#if defined(MS_WINDOWS) || defined(MS_WIN64) + if (fd == 0) + cp = GetConsoleCP(); + else if (fd == 1 || fd == 2) + cp = GetConsoleOutputCP(); + else + cp = 0; + /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application + has no console */ + if (cp != 0) + return PyUnicode_FromFormat("cp%u", (unsigned int)cp); +#elif defined(CODESET) + { + char *codeset = nl_langinfo(CODESET); + if (codeset != NULL && codeset[0] != 0) + return PyUnicode_FromString(codeset); + } +#endif + Py_RETURN_NONE; +} + #ifdef HAVE_STAT /* Decode a byte string from the locale encoding with the
