summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-05-23 16:12:52 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-05-23 16:12:52 (GMT)
commit7870bdff5a16c7440a6db5ef5f91082d27a4c482 (patch)
tree141fd39f8d468ea4d1d3248386d72c56bb43effd /Modules
parenta1ae533ac9e1535003daeaf8928018ab1e63c51a (diff)
downloadcpython-7870bdff5a16c7440a6db5ef5f91082d27a4c482.zip
cpython-7870bdff5a16c7440a6db5ef5f91082d27a4c482.tar.gz
cpython-7870bdff5a16c7440a6db5ef5f91082d27a4c482.tar.bz2
Issue #6501: os.device_encoding() returns None on Windows if the application
has no console.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/posixmodule.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 9c19ed0..add3b35 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -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);