diff options
author | Christian Heimes <christian@python.org> | 2017-03-02 10:09:01 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2017-03-02 10:09:01 (GMT) |
commit | 2b221b78d602f1684a81b85af748fa55b1449dac (patch) | |
tree | 2b414be1bdeb518fd4d0c4179e56a072322e5857 /Modules | |
parent | cb90f261226fc2d4db60a68b53a788e8ae5905cf (diff) | |
download | cpython-2b221b78d602f1684a81b85af748fa55b1449dac.zip cpython-2b221b78d602f1684a81b85af748fa55b1449dac.tar.gz cpython-2b221b78d602f1684a81b85af748fa55b1449dac.tar.bz2 |
bpo-29176 Use tmpfile() in curses module (#235)
The curses module used mkstemp() + fopen() to create a temporary file in
/tmp. The /tmp directory does not exist on Android. The tmpfile()
function simplifies the task a lot. It creates a temporary file in a
correct directory, takes care of cleanup and returns FILE*.
tmpfile is supported on all platforms (C89, POSIX 2001, Android,
Windows).
Signed-off-by: Christian Heimes <christian@python.org>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_cursesmodule.c | 52 |
1 files changed, 15 insertions, 37 deletions
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 78a79e8..4d714ce 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1720,22 +1720,14 @@ 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+"); - if (fp == NULL) { - PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn); + fp = tmpfile(); + if (fp == NULL) + return PyErr_SetFromErrno(PyExc_OSError); + 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 +1746,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 +2266,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,17 +2275,13 @@ 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+"); - if (fp == NULL) { - PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn); + fp = tmpfile(); + if (fp == NULL) + return PyErr_SetFromErrno(PyExc_OSError); + + if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0) goto error; - } + data = _PyObject_CallMethodId(stream, &PyId_read, NULL); if (data == NULL) @@ -2314,7 +2296,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 +2310,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; } |