diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2009-01-10 16:22:51 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2009-01-10 16:22:51 (GMT) |
commit | 308705e4fab1fe784218bde5cc8c8d2daddf6700 (patch) | |
tree | c179157d874086e0e6472b292287e1eb3e7806c0 /Modules | |
parent | ab868311a5282f188a8cf831b021938420fee5c4 (diff) | |
download | cpython-308705e4fab1fe784218bde5cc8c8d2daddf6700.zip cpython-308705e4fab1fe784218bde5cc8c8d2daddf6700.tar.gz cpython-308705e4fab1fe784218bde5cc8c8d2daddf6700.tar.bz2 |
Merged revisions 68484-68485 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68484 | antoine.pitrou | 2009-01-10 17:13:45 +0100 (sam., 10 janv. 2009) | 3 lines
Issue #3860: GzipFile and BZ2File now support the context manager protocol.
........
r68485 | antoine.pitrou | 2009-01-10 17:15:24 +0100 (sam., 10 janv. 2009) | 1 line
Add NEWS entry for r68484.
........
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/bz2module.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Modules/bz2module.c b/Modules/bz2module.c index 4bf31ee..c7cc767 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -1086,6 +1086,36 @@ BZ2File_close(BZ2FileObject *self) return ret; } +PyDoc_STRVAR(BZ2File_enter_doc, +"__enter__() -> self."); + +static PyObject * +BZ2File_enter(BZ2FileObject *self) +{ + if (self->mode == MODE_CLOSED) { + PyErr_SetString(PyExc_ValueError, + "I/O operation on closed file"); + return NULL; + } + Py_INCREF(self); + return (PyObject *) self; +} + +PyDoc_STRVAR(BZ2File_exit_doc, +"__exit__(*excinfo) -> None. Closes the file."); + +static PyObject * +BZ2File_exit(BZ2FileObject *self, PyObject *args) +{ + PyObject *ret = PyObject_CallMethod((PyObject *) self, "close", NULL); + if (!ret) + /* If error occurred, pass through */ + return NULL; + Py_DECREF(ret); + Py_RETURN_NONE; +} + + static PyObject *BZ2File_getiter(BZ2FileObject *self); static PyMethodDef BZ2File_methods[] = { @@ -1097,6 +1127,8 @@ static PyMethodDef BZ2File_methods[] = { {"seek", (PyCFunction)BZ2File_seek, METH_VARARGS, BZ2File_seek__doc__}, {"tell", (PyCFunction)BZ2File_tell, METH_NOARGS, BZ2File_tell__doc__}, {"close", (PyCFunction)BZ2File_close, METH_NOARGS, BZ2File_close__doc__}, + {"__enter__", (PyCFunction)BZ2File_enter, METH_NOARGS, BZ2File_enter_doc}, + {"__exit__", (PyCFunction)BZ2File_exit, METH_VARARGS, BZ2File_exit_doc}, {NULL, NULL} /* sentinel */ }; |