diff options
author | Ross Lagerwall <rosslagerwall@gmail.com> | 2011-10-31 20:34:46 +0200 |
---|---|---|
committer | Ross Lagerwall <rosslagerwall@gmail.com> | 2011-10-31 20:34:46 +0200 |
commit | 3930babb026d8b6790c56e9011373afeb9f9b13f (patch) | |
tree | 8055ba99ae6ab48b230a96e20d67728dbaa04b58 /Modules/_io/_iomodule.c | |
parent | baecb919b66f1780b49a5d8d8ed4192951fdf4e4 (diff) | |
download | cpython-3930babb026d8b6790c56e9011373afeb9f9b13f.tar.gz |
Issue #12797: Added custom opener parameter to builtin open() and FileIO.open().
Diffstat (limited to 'Modules/_io/_iomodule.c')
-rw-r--r-- | Modules/_io/_iomodule.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 2ad002ecdd..a8d3ed4354 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -95,7 +95,7 @@ PyDoc_STRVAR(module_doc, */ PyDoc_STRVAR(open_doc, "open(file, mode='r', buffering=-1, encoding=None,\n" -" errors=None, newline=None, closefd=True) -> file object\n" +" errors=None, newline=None, closefd=True, opener=None) -> file object\n" "\n" "Open file and return a stream. Raise IOError upon failure.\n" "\n" @@ -190,6 +190,12 @@ PyDoc_STRVAR(open_doc, "when the file is closed. This does not work when a file name is given\n" "and must be True in that case.\n" "\n" +"A custom opener can be used by passing a callable as *opener*. The\n" +"underlying file descriptor for the file object is then obtained by\n" +"calling *opener* with (*file*, *flags*). *opener* must return an open\n" +"file descriptor (passing os.open as *opener* results in functionality\n" +"similar to passing None).\n" +"\n" "open() returns a file object whose type depends on the mode, and\n" "through which the standard file operations such as reading and writing\n" "are performed. When open() is used to open a file in a text mode ('w',\n" @@ -210,8 +216,8 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds) { char *kwlist[] = {"file", "mode", "buffering", "encoding", "errors", "newline", - "closefd", NULL}; - PyObject *file; + "closefd", "opener", NULL}; + PyObject *file, *opener = Py_None; char *mode = "r"; int buffering = -1, closefd = 1; char *encoding = NULL, *errors = NULL, *newline = NULL; @@ -228,10 +234,10 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds) _Py_IDENTIFIER(isatty); _Py_IDENTIFIER(fileno); - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzzi:open", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzziO:open", kwlist, &file, &mode, &buffering, &encoding, &errors, &newline, - &closefd)) { + &closefd, &opener)) { return NULL; } @@ -331,7 +337,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds) /* Create the Raw file stream */ raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type, - "Osi", file, rawmode, closefd); + "OsiO", file, rawmode, closefd, opener); if (raw == NULL) return NULL; |