summaryrefslogtreecommitdiffstats
path: root/Python/fileutils.c
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-02-29 23:31:31 (GMT)
committerBrett Cannon <brett@python.org>2012-02-29 23:31:31 (GMT)
commitefb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf (patch)
tree22dc683979e0dd42d049416797b756a5c6a053cd /Python/fileutils.c
parent54c32032aa28bdfead50714bf7861c98a9843597 (diff)
downloadcpython-efb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf.zip
cpython-efb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf.tar.gz
cpython-efb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf.tar.bz2
Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
the os module.
Diffstat (limited to 'Python/fileutils.c')
-rw-r--r--Python/fileutils.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 8993c8c..501cb8c 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -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