From 8d9d4b2b3a2745c4748e87af41849dc7df63363d Mon Sep 17 00:00:00 2001
From: Victor Stinner
Date: Wed, 22 Feb 2017 11:50:56 +0100
Subject: [PATCH] bpo-29176: curses: use tmpfile() for getwin/putwin
Use C function tmpfile() for curses.getwin() and curses.putwin(),
instead of mkstemp() using an hardcoded /tmp path. The tmpfile()
chooses the directory for the temporary file and so is more portable
(especially, it works on Android).
Replace also IOError with OSError: since the PEP 3151, IOError is an
alias to OSError.
---
Modules/_cursesmodule.c | 49 ++++++++++++++---------------------------
1 file changed, 16 insertions(+), 33 deletions(-)
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index 78a79e8eef03bcd..3b74c8203b4ad16 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -1720,22 +1720,18 @@ PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *stream)
{
/* We have to simulate this by writing to a temporary FILE*,
then reading back, then writing to the argument stream. */
- char fn[100];
- int fd = -1;
- FILE *fp = NULL;
+ FILE *fp;
PyObject *res = NULL;
- strcpy(fn, "/tmp/py.curses.putwin.XXXXXX");
- fd = mkstemp(fn);
- if (fd < 0)
- return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
- if (_Py_set_inheritable(fd, 0, NULL) < 0)
- goto exit;
- fp = fdopen(fd, "wb+");
+ fp = tmpfile();
if (fp == NULL) {
- PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0) {
goto exit;
}
+
res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
if (res == NULL)
goto exit;
@@ -1754,11 +1750,7 @@ PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *stream)
}
exit:
- if (fp != NULL)
- fclose(fp);
- else if (fd != -1)
- close(fd);
- remove(fn);
+ fclose(fp);
return res;
}
@@ -2278,9 +2270,7 @@ PyCurses_UngetMouse(PyObject *self, PyObject *args)
static PyObject *
PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
{
- char fn[100];
- int fd = -1;
- FILE *fp = NULL;
+ FILE *fp;
PyObject *data;
size_t datalen;
WINDOW *win;
@@ -2289,15 +2279,12 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
PyCursesInitialised;
- strcpy(fn, "/tmp/py.curses.getwin.XXXXXX");
- fd = mkstemp(fn);
- if (fd < 0)
- return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
- if (_Py_set_inheritable(fd, 0, NULL) < 0)
- goto error;
- fp = fdopen(fd, "wb+");
+ fp = tmpfile();
if (fp == NULL) {
- PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0) {
goto error;
}
@@ -2314,7 +2301,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
datalen = PyBytes_GET_SIZE(data);
if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
Py_DECREF(data);
- PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
+ PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
Py_DECREF(data);
@@ -2328,11 +2315,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
res = PyCursesWindow_New(win, NULL);
error:
- if (fp != NULL)
- fclose(fp);
- else if (fd != -1)
- close(fd);
- remove(fn);
+ fclose(fp);
return res;
}