]> granicus.if.org Git - python/commitdiff
Issue #6501: os.device_encoding() returns None on Windows if the application
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 23 May 2011 16:12:52 +0000 (18:12 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 23 May 2011 16:12:52 +0000 (18:12 +0200)
has no console.

Misc/NEWS
Modules/posixmodule.c

index 6ebb33a8f99ca399732791d936d5300239ddcb63..9e2c14df2a769cefb8f20989ffa3df7e83adfc70 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -153,6 +153,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #6501: os.device_encoding() returns None on Windows if the application
+  has no console.
+
 - Issue #12132: Skip test_build_ext in case the xxmodule is not found.
 
 - Issue #12105: Add O_CLOEXEC to the os module.
index 9c19ed0433aa6e64a7f30c249080d10d97506b93..add3b35c9fce6b6d8a8f9d8023cd4adad7b36b7e 100644 (file)
@@ -8495,6 +8495,9 @@ static PyObject *
 device_encoding(PyObject *self, PyObject *args)
 {
     int fd;
+#if defined(MS_WINDOWS) || defined(MS_WIN64)
+    UINT cp;
+#endif
     if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
         return NULL;
     if (!_PyVerify_fd(fd) || !isatty(fd)) {
@@ -8502,16 +8505,16 @@ device_encoding(PyObject *self, PyObject *args)
         return Py_None;
     }
 #if defined(MS_WINDOWS) || defined(MS_WIN64)
-    if (fd == 0) {
-        char buf[100];
-        sprintf(buf, "cp%d", GetConsoleCP());
-        return PyUnicode_FromString(buf);
-    }
-    if (fd == 1 || fd == 2) {
-        char buf[100];
-        sprintf(buf, "cp%d", GetConsoleOutputCP());
-        return PyUnicode_FromString(buf);
-    }
+    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);