Christian Heimes added the comment:
Here you go, Guido!
Added file: http://bugs.python.org/file8647/py3k_closefd.patch
__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1329>
__________________________________
Index: Python/pythonrun.c
===================================================================
--- Python/pythonrun.c (revision 58695)
+++ Python/pythonrun.c (working copy)
@@ -720,7 +720,7 @@
/* Set sys.stdin */
if (!(std = PyFile_FromFd(fileno(stdin), "<stdin>", "r", -1,
- NULL, "\n"))) {
+ NULL, "\n", 0))) {
goto error;
}
PySys_SetObject("__stdin__", std);
@@ -729,7 +729,7 @@
/* Set sys.stdout */
if (!(std = PyFile_FromFd(fileno(stdout), "<stdout>", "w", -1,
- NULL, "\n"))) {
+ NULL, "\n", 0))) {
goto error;
}
PySys_SetObject("__stdout__", std);
@@ -738,7 +738,7 @@
/* Set sys.stderr */
if (!(std = PyFile_FromFd(fileno(stderr), "<stderr>", "w", -1,
- NULL, "\n"))) {
+ NULL, "\n", 0))) {
goto error;
}
PySys_SetObject("__stderr__", std);
Index: Python/import.c
===================================================================
--- Python/import.c (revision 58695)
+++ Python/import.c (working copy)
@@ -2588,7 +2588,7 @@
(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);
Index: Include/fileobject.h
===================================================================
--- Include/fileobject.h (revision 58695)
+++ Include/fileobject.h (working copy)
@@ -8,7 +8,8 @@
#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 *);
Index: Objects/fileobject.c
===================================================================
--- Objects/fileobject.c (revision 58695)
+++ Objects/fileobject.c (working copy)
@@ -27,15 +27,19 @@
PyObject *
PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding,
- char *newline)
+ char *newline, int closefd)
{
PyObject *io, *stream, *nameobj = NULL;
+ if (closefd < 0) {
+ closefd = 1;
+ }
+
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;
Index: Misc/NEWS
===================================================================
--- Misc/NEWS (revision 58695)
+++ Misc/NEWS (working copy)
@@ -28,6 +28,9 @@
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
-----------------
Index: Doc/c-api/concrete.rst
===================================================================
--- Doc/c-api/concrete.rst (revision 58696)
+++ Doc/c-api/concrete.rst (working copy)
@@ -2401,12 +2401,12 @@
: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* as well as *buffering* and *closefd* can be *-1* to use the defaults.
+ Return *NULL* on failure.
.. warning::
Index: Lib/io.py
===================================================================
--- Lib/io.py (revision 58695)
+++ Lib/io.py (working copy)
@@ -49,7 +49,8 @@
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:
@@ -81,6 +82,8 @@
other legal values, any `'\n'` characters written are
translated to the given string.
+ closefd: Disable the closing of the underlying file descriptor
+
(*) 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.
@@ -138,7 +141,8 @@
(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():
Index: Modules/_fileio.c
===================================================================
--- Modules/_fileio.c (revision 58695)
+++ Modules/_fileio.c (working copy)
@@ -33,6 +33,7 @@
unsigned readable : 1;
unsigned writable : 1;
int seekable : 2; /* -1 means unknown */
+ int closefd : 1;
PyObject *weakreflist;
} PyFileIOObject;
@@ -59,6 +60,11 @@
static PyObject *
fileio_close(PyFileIOObject *self)
{
+ if (self->closefd == 0) {
+ PyErr_WarnEx(PyExc_RuntimeWarning,
+ "Trying to close unclosable fd!", 3);
+ Py_RETURN_NONE;
+ }
errno = internal_close(self);
if (errno < 0) {
PyErr_SetFromErrno(PyExc_IOError);
@@ -119,9 +125,10 @@
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";
+ PyObject *close = NULL;
char *s;
#ifdef MS_WINDOWS
Py_UNICODE *widename = NULL;
@@ -130,6 +137,7 @@
int rwa = 0, plus = 0, append = 0;
int flags = 0;
int fd = -1;
+ int closefd = 1;
assert(PyFileIO_Check(oself));
if (self->fd >= 0) {
@@ -138,8 +146,8 @@
return -1;
}
- if (PyArg_ParseTupleAndKeywords(args, kwds, "i|s:fileio",
- kwlist, &fd, &mode)) {
+ if (PyArg_ParseTupleAndKeywords(args, kwds, "i|sO:fileio",
+ kwlist, &fd, &mode, &close)) {
if (fd < 0) {
PyErr_SetString(PyExc_ValueError,
"Negative filedescriptor");
@@ -153,8 +161,8 @@
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|sO:fileio",
+ kwlist, &po, &mode, &close)) {
widename = PyUnicode_AS_UNICODE(po);
} else {
/* Drop the argument parsing error as narrow
@@ -165,10 +173,10 @@
if (widename == NULL)
#endif
{
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:fileio",
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|sO:fileio",
kwlist,
Py_FileSystemDefaultEncoding,
- &name, &mode))
+ &name, &mode, &close))
goto error;
}
}
@@ -235,10 +243,22 @@
flags |= O_APPEND;
#endif
+ if (close != NULL && PyObject_IsTrue(close) == 0) {
+ closefd = 0;
+ }
+
if (fd >= 0) {
self->fd = fd;
+ self->closefd = closefd;
}
else {
+ if (closefd != 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot disable close fd with file name!");
+ goto error;
+ }
+ self->closefd = closefd;
+
Py_BEGIN_ALLOW_THREADS
errno = 0;
#ifdef MS_WINDOWS
@@ -270,7 +290,7 @@
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
- if (self->fd >= 0) {
+ if (self->fd >= 0 && self->closefd != 0) {
errno = internal_close(self);
if (errno < 0) {
#ifdef HAVE_STRERROR
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com