Index: Doc/library/mmap.rst =================================================================== --- Doc/library/mmap.rst (revision 78575) +++ Doc/library/mmap.rst (working copy) @@ -119,6 +119,15 @@ map.close() + :class:`mmap` can also be used as a context manager in a :keyword:`with` + statement.:: + + import mmap + + with mmap.mmap(-1, 13) as map: + map.write("Hello world!") + + The next example demonstrates how to create an anonymous map and exchange data between the parent and child processes:: @@ -146,6 +155,11 @@ result in an exception being raised. + .. attribute:: closed + + True if the file is closed. + + .. method:: find(string[, start[, end]]) Returns the lowest index in the object where the substring *string* is @@ -243,5 +257,3 @@ position of the file pointer; the file position is advanced by ``1``. If the mmap was created with :const:`ACCESS_READ`, then writing to it will throw a :exc:`TypeError` exception. - - Index: Lib/test/test_mmap.py =================================================================== --- Lib/test/test_mmap.py (revision 78575) +++ Lib/test/test_mmap.py (working copy) @@ -586,7 +586,21 @@ pass m.close() + def test_context_manager(self): + with mmap.mmap(-1, 10) as m: + self.assertFalse(m.closed) + self.assertTrue(m.closed) + def test_context_manager_exception(self): + # Test that the IOError gets passed through + with self.assertRaises(Exception) as exc: + with mmap.mmap(-1, 10) as m: + raise IOError + self.assertIsInstance(exc.exception, IOError, + "wrong exception raised in context manager") + self.assertTrue(m.closed, "context manager failed") + + def test_main(): run_unittest(MmapTests) Index: Modules/mmapmodule.c =================================================================== --- Modules/mmapmodule.c (revision 78575) +++ Modules/mmapmodule.c (working copy) @@ -640,6 +640,33 @@ } } +static PyObject * +mmap_closed_get(mmap_object *self) +{ +#ifdef MS_WINDOWS + return PyBool_FromLong(self->map_handle == NULL ? 1 : 0); +#endif /* MS_WINDOWS */ +#ifdef UNIX + return PyBool_FromLong(self->data == NULL ? 1 : 0); +#endif /* UNIX */ +} + +static PyObject * +mmap__enter__method(mmap_object *self, PyObject *args) +{ + CHECK_VALID(NULL); + + Py_INCREF(self); + return (PyObject*)self; +} + +static PyObject * +mmap__exit__method(PyObject *self, PyObject *args) +{ + return PyObject_CallMethodObjArgs(self, PyString_FromString("close"), + NULL); +} + static struct PyMethodDef mmap_object_methods[] = { {"close", (PyCFunction) mmap_close_method, METH_NOARGS}, {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, @@ -655,9 +682,16 @@ {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS}, {"write", (PyCFunction) mmap_write_method, METH_VARARGS}, {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS}, + {"__enter__", (PyCFunction) mmap__enter__method, METH_NOARGS}, + {"__exit__", (PyCFunction) mmap__exit__method, METH_VARARGS}, {NULL, NULL} /* sentinel */ }; +static PyGetSetDef mmap_object_getset[] = { + {"closed", (getter)mmap_closed_get, NULL, NULL}, + {NULL} +}; + /* Functions for treating an mmap'ed file as a buffer */ static Py_ssize_t @@ -1054,7 +1088,7 @@ 0, /* tp_iternext */ mmap_object_methods, /* tp_methods */ 0, /* tp_members */ - 0, /* tp_getset */ + mmap_object_getset, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */