@@ -1201,6 +1201,36 @@ BZ2File_close(BZ2FileObject *self)
1201
1201
return ret ;
1202
1202
}
1203
1203
1204
+ PyDoc_STRVAR (BZ2File_enter_doc ,
1205
+ "__enter__() -> self." );
1206
+
1207
+ static PyObject *
1208
+ BZ2File_enter (BZ2FileObject * self )
1209
+ {
1210
+ if (self -> mode == MODE_CLOSED ) {
1211
+ PyErr_SetString (PyExc_ValueError ,
1212
+ "I/O operation on closed file" );
1213
+ return NULL ;
1214
+ }
1215
+ Py_INCREF (self );
1216
+ return (PyObject * ) self ;
1217
+ }
1218
+
1219
+ PyDoc_STRVAR (BZ2File_exit_doc ,
1220
+ "__exit__(*excinfo) -> None. Closes the file." );
1221
+
1222
+ static PyObject *
1223
+ BZ2File_exit (BZ2FileObject * self , PyObject * args )
1224
+ {
1225
+ PyObject * ret = PyObject_CallMethod ((PyObject * ) self , "close" , NULL );
1226
+ if (!ret )
1227
+ /* If error occurred, pass through */
1228
+ return NULL ;
1229
+ Py_DECREF (ret );
1230
+ Py_RETURN_NONE ;
1231
+ }
1232
+
1233
+
1204
1234
static PyObject * BZ2File_getiter (BZ2FileObject * self );
1205
1235
1206
1236
static PyMethodDef BZ2File_methods [] = {
@@ -1213,6 +1243,8 @@ static PyMethodDef BZ2File_methods[] = {
1213
1243
{"seek" , (PyCFunction )BZ2File_seek , METH_VARARGS , BZ2File_seek__doc__ },
1214
1244
{"tell" , (PyCFunction )BZ2File_tell , METH_NOARGS , BZ2File_tell__doc__ },
1215
1245
{"close" , (PyCFunction )BZ2File_close , METH_NOARGS , BZ2File_close__doc__ },
1246
+ {"__enter__" , (PyCFunction )BZ2File_enter , METH_NOARGS , BZ2File_enter_doc },
1247
+ {"__exit__" , (PyCFunction )BZ2File_exit , METH_VARARGS , BZ2File_exit_doc },
1216
1248
{NULL , NULL } /* sentinel */
1217
1249
};
1218
1250
0 commit comments