Skip to content

Commit 2b221b7

Browse files
tiranvstinner
authored andcommitted
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 <[email protected]>
1 parent cb90f26 commit 2b221b7

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

Modules/_cursesmodule.c

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,22 +1720,14 @@ PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *stream)
17201720
{
17211721
/* We have to simulate this by writing to a temporary FILE*,
17221722
then reading back, then writing to the argument stream. */
1723-
char fn[100];
1724-
int fd = -1;
1725-
FILE *fp = NULL;
1723+
FILE *fp;
17261724
PyObject *res = NULL;
17271725

1728-
strcpy(fn, "/tmp/py.curses.putwin.XXXXXX");
1729-
fd = mkstemp(fn);
1730-
if (fd < 0)
1731-
return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
1732-
if (_Py_set_inheritable(fd, 0, NULL) < 0)
1733-
goto exit;
1734-
fp = fdopen(fd, "wb+");
1735-
if (fp == NULL) {
1736-
PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
1726+
fp = tmpfile();
1727+
if (fp == NULL)
1728+
return PyErr_SetFromErrno(PyExc_OSError);
1729+
if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
17371730
goto exit;
1738-
}
17391731
res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
17401732
if (res == NULL)
17411733
goto exit;
@@ -1754,11 +1746,7 @@ PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *stream)
17541746
}
17551747

17561748
exit:
1757-
if (fp != NULL)
1758-
fclose(fp);
1759-
else if (fd != -1)
1760-
close(fd);
1761-
remove(fn);
1749+
fclose(fp);
17621750
return res;
17631751
}
17641752

@@ -2278,9 +2266,7 @@ PyCurses_UngetMouse(PyObject *self, PyObject *args)
22782266
static PyObject *
22792267
PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
22802268
{
2281-
char fn[100];
2282-
int fd = -1;
2283-
FILE *fp = NULL;
2269+
FILE *fp;
22842270
PyObject *data;
22852271
size_t datalen;
22862272
WINDOW *win;
@@ -2289,17 +2275,13 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
22892275

22902276
PyCursesInitialised;
22912277

2292-
strcpy(fn, "/tmp/py.curses.getwin.XXXXXX");
2293-
fd = mkstemp(fn);
2294-
if (fd < 0)
2295-
return PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2296-
if (_Py_set_inheritable(fd, 0, NULL) < 0)
2297-
goto error;
2298-
fp = fdopen(fd, "wb+");
2299-
if (fp == NULL) {
2300-
PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2278+
fp = tmpfile();
2279+
if (fp == NULL)
2280+
return PyErr_SetFromErrno(PyExc_OSError);
2281+
2282+
if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
23012283
goto error;
2302-
}
2284+
23032285

23042286
data = _PyObject_CallMethodId(stream, &PyId_read, NULL);
23052287
if (data == NULL)
@@ -2314,7 +2296,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
23142296
datalen = PyBytes_GET_SIZE(data);
23152297
if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
23162298
Py_DECREF(data);
2317-
PyErr_SetFromErrnoWithFilename(PyExc_IOError, fn);
2299+
PyErr_SetFromErrno(PyExc_OSError);
23182300
goto error;
23192301
}
23202302
Py_DECREF(data);
@@ -2328,11 +2310,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream)
23282310
res = PyCursesWindow_New(win, NULL);
23292311

23302312
error:
2331-
if (fp != NULL)
2332-
fclose(fp);
2333-
else if (fd != -1)
2334-
close(fd);
2335-
remove(fn);
2313+
fclose(fp);
23362314
return res;
23372315
}
23382316

0 commit comments

Comments
 (0)