@@ -1086,6 +1086,36 @@ BZ2File_close(BZ2FileObject *self)
10861086 return ret ;
10871087}
10881088
1089+ PyDoc_STRVAR (BZ2File_enter_doc ,
1090+ "__enter__() -> self." );
1091+
1092+ static PyObject *
1093+ BZ2File_enter (BZ2FileObject * self )
1094+ {
1095+ if (self -> mode == MODE_CLOSED ) {
1096+ PyErr_SetString (PyExc_ValueError ,
1097+ "I/O operation on closed file" );
1098+ return NULL ;
1099+ }
1100+ Py_INCREF (self );
1101+ return (PyObject * ) self ;
1102+ }
1103+
1104+ PyDoc_STRVAR (BZ2File_exit_doc ,
1105+ "__exit__(*excinfo) -> None. Closes the file." );
1106+
1107+ static PyObject *
1108+ BZ2File_exit (BZ2FileObject * self , PyObject * args )
1109+ {
1110+ PyObject * ret = PyObject_CallMethod ((PyObject * ) self , "close" , NULL );
1111+ if (!ret )
1112+ /* If error occurred, pass through */
1113+ return NULL ;
1114+ Py_DECREF (ret );
1115+ Py_RETURN_NONE ;
1116+ }
1117+
1118+
10891119static PyObject * BZ2File_getiter (BZ2FileObject * self );
10901120
10911121static PyMethodDef BZ2File_methods [] = {
@@ -1097,6 +1127,8 @@ static PyMethodDef BZ2File_methods[] = {
10971127 {"seek" , (PyCFunction )BZ2File_seek , METH_VARARGS , BZ2File_seek__doc__ },
10981128 {"tell" , (PyCFunction )BZ2File_tell , METH_NOARGS , BZ2File_tell__doc__ },
10991129 {"close" , (PyCFunction )BZ2File_close , METH_NOARGS , BZ2File_close__doc__ },
1130+ {"__enter__" , (PyCFunction )BZ2File_enter , METH_NOARGS , BZ2File_enter_doc },
1131+ {"__exit__" , (PyCFunction )BZ2File_exit , METH_VARARGS , BZ2File_exit_doc },
11001132 {NULL , NULL } /* sentinel */
11011133};
11021134
0 commit comments