diff -r 5a6fa1b8767f Lib/test/test_mmap.py --- a/Lib/test/test_mmap.py Wed Apr 11 16:46:54 2012 -0400 +++ b/Lib/test/test_mmap.py Sat Sep 08 11:01:43 2012 -0400 @@ -466,6 +466,22 @@ f.flush () return mmap.mmap (f.fileno(), 0) + def test_empty_file (self): + f = open (TESTFN, 'w+b') + f.close() + f = open(TESTFN, "rb") + try: + m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) + m.close() + f.close() + self.fail("should not have been able to mmap empty file") + except ValueError as e: + f.close() + self.assertEqual(str(e), "cannot mmap an empty file") + except: + f.close() + self.fail("unexpected exception: " + str(e)) + def test_offset (self): f = open (TESTFN, 'w+b') diff -r 5a6fa1b8767f Modules/mmapmodule.c --- a/Modules/mmapmodule.c Wed Apr 11 16:46:54 2012 -0400 +++ b/Modules/mmapmodule.c Sat Sep 08 11:01:43 2012 -0400 @@ -1109,6 +1109,11 @@ # endif if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { if (map_size == 0) { + if (st.st_size == 0) { + PyErr_SetString(PyExc_ValueError, + "cannot mmap an empty file"); + return NULL; + } if (offset >= st.st_size) { PyErr_SetString(PyExc_ValueError, "mmap offset is greater than file size");