:ctype:`PyFileObject`.
-.. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *newline)
+.. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *newline, int closefd)
Create a new :ctype:`PyFileObject` from the file descriptor of an already
opened file *fd*. The arguments *name*, *encoding* and *newline* can be
- *NULL* as well as buffering can be *-1* to use the defaults. Return *NULL* on
- failure.
+ *NULL* to use the defaults; *buffering* can be *-1* to use the default.
+ Return *NULL* on failure.
.. warning::
#define PY_STDIOTEXTMODE "b"
-PyAPI_FUNC(PyObject *) PyFile_FromFd(int, char *, char *, int, char *, char *);
+PyAPI_FUNC(PyObject *) PyFile_FromFd(int, char *, char *, int, char *, char *,
+ int);
PyAPI_FUNC(PyObject *) PyFile_GetLine(PyObject *, int);
PyAPI_FUNC(int) PyFile_WriteObject(PyObject *, PyObject *, int);
PyAPI_FUNC(int) PyFile_WriteString(const char *, PyObject *);
self.characters_written = characters_written
-def open(file, mode="r", buffering=None, encoding=None, newline=None):
+def open(file, mode="r", buffering=None, encoding=None, newline=None,
+ closefd=True):
r"""Replacement for the built-in open function.
Args:
other legal values, any `'\n'` characters written are
translated to the given string.
+ closefd: optional argument to keep the underlying file descriptor
+ open when the file is closed. It must not be false when
+ a filename is given.
+
(*) If a file descriptor is given, it is closed when the returned
- I/O object is closed. If you don't want this to happen, use
- os.dup() to create a duplicate file descriptor.
+ I/O object is closed, unless closefd=False is give.
Mode strings characters:
'r': open for reading (default)
(reading and "r" or "") +
(writing and "w" or "") +
(appending and "a" or "") +
- (updating and "+" or ""))
+ (updating and "+" or ""),
+ closefd)
if buffering is None:
buffering = -1
if buffering < 0 and raw.isatty():
sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
sts = 1
continue
- if deco:
- decode(fp, sys.stdout.buffer)
- else:
- encode(fp, sys.stdout.buffer, tabs)
- if fp is not sys.stdin:
- fp.close()
+ try:
+ if deco:
+ decode(fp, sys.stdout.buffer)
+ else:
+ encode(fp, sys.stdout.buffer, tabs)
+ finally:
+ if file != '-':
+ fp.close()
if sts:
sys.exit(sts)
self.assertEqual(f.write(a), n)
f.close()
+ def test_closefd(self):
+ self.assertRaises(ValueError, io.open, test_support.TESTFN, 'w',
+ closefd=False)
class MemorySeekTestMixin:
with `Py_FileSystemDefaultEncoding` and a new API method
`PyUnicode_DecodeFSDefault(char*)` was added.
+- io.open() and _fileio.FileIO have grown a new argument closefd. A false
+ value disables the closing of the file descriptor.
+
Extension Modules
-----------------
unsigned readable : 1;
unsigned writable : 1;
int seekable : 2; /* -1 means unknown */
+ int closefd : 1;
PyObject *weakreflist;
} PyFileIOObject;
static PyObject *
fileio_close(PyFileIOObject *self)
{
+ if (!self->closefd) {
+ if (PyErr_WarnEx(PyExc_RuntimeWarning,
+ "Trying to close unclosable fd!", 3) < 0) {
+ return NULL;
+ }
+ Py_RETURN_NONE;
+ }
errno = internal_close(self);
if (errno < 0) {
PyErr_SetFromErrno(PyExc_IOError);
fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
{
PyFileIOObject *self = (PyFileIOObject *) oself;
- static char *kwlist[] = {"file", "mode", NULL};
+ static char *kwlist[] = {"file", "mode", "closefd", NULL};
char *name = NULL;
char *mode = "r";
char *s;
int rwa = 0, plus = 0, append = 0;
int flags = 0;
int fd = -1;
+ int closefd = 1;
assert(PyFileIO_Check(oself));
if (self->fd >= 0) {
return -1;
}
- if (PyArg_ParseTupleAndKeywords(args, kwds, "i|s:fileio",
- kwlist, &fd, &mode)) {
+ if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio",
+ kwlist, &fd, &mode, &closefd)) {
if (fd < 0) {
PyErr_SetString(PyExc_ValueError,
"Negative filedescriptor");
if (GetVersion() < 0x80000000) {
/* On NT, so wide API available */
PyObject *po;
- if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:fileio",
- kwlist, &po, &mode)) {
+ if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio",
+ kwlist, &po, &mode, &closefd)
+ ) {
widename = PyUnicode_AS_UNICODE(po);
} else {
/* Drop the argument parsing error as narrow
PyErr_Clear();
}
}
- if (widename == NULL)
+ if (widename == NULL)
#endif
{
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:fileio",
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio",
kwlist,
Py_FileSystemDefaultEncoding,
- &name, &mode))
+ &name, &mode, &closefd))
goto error;
}
}
if (fd >= 0) {
self->fd = fd;
+ self->closefd = closefd;
}
else {
+ self->closefd = 1;
+ if (!closefd) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot use closefd=True with file name");
+ goto error;
+ }
+
Py_BEGIN_ALLOW_THREADS
errno = 0;
#ifdef MS_WINDOWS
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
- if (self->fd >= 0) {
+ if (self->fd >= 0 && self->closefd) {
errno = internal_close(self);
if (errno < 0) {
#ifdef HAVE_STRERROR
PyObject *
PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding,
- char *newline)
+ char *newline, int closefd)
{
PyObject *io, *stream, *nameobj = NULL;
io = PyImport_ImportModule("io");
if (io == NULL)
return NULL;
- stream = PyObject_CallMethod(io, "open", "isiss", fd, mode,
- buffering, encoding, newline);
+ stream = PyObject_CallMethod(io, "open", "isissi", fd, mode,
+ buffering, encoding, newline, closefd);
Py_DECREF(io);
if (stream == NULL)
return NULL;
(char*)PyUnicode_GetDefaultEncoding();
}
fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
- (char*)encoding, NULL);
+ (char*)encoding, NULL, 1);
if (fob == NULL) {
close(fd);
PyMem_FREE(found_encoding);
/* Set sys.stdin */
if (!(std = PyFile_FromFd(fileno(stdin), "<stdin>", "r", -1,
- NULL, "\n"))) {
+ NULL, "\n", 0))) {
goto error;
}
PySys_SetObject("__stdin__", std);
/* Set sys.stdout */
if (!(std = PyFile_FromFd(fileno(stdout), "<stdout>", "w", -1,
- NULL, "\n"))) {
+ NULL, "\n", 0))) {
goto error;
}
PySys_SetObject("__stdout__", std);
PySys_SetObject("stdout", std);
Py_DECREF(std);
- /* Set sys.stderr */
+ /* Set sys.stderr, replaces the preliminary stderr */
if (!(std = PyFile_FromFd(fileno(stderr), "<stderr>", "w", -1,
- NULL, "\n"))) {
+ NULL, "\n", 0))) {
goto error;
}
PySys_SetObject("__stderr__", std);