Index: Lib/test/test_mmap.py =================================================================== --- Lib/test/test_mmap.py (revision 69718) +++ Lib/test/test_mmap.py (working copy) @@ -348,6 +348,24 @@ m[x] = ch = chr(x & 255) self.assertEqual(m[x], ch) + def test_anonymous_resize(self): + RESIZE = 2 * PAGESIZE + m = mmap.mmap(-1, PAGESIZE) + try: + m.resize(RESIZE) + except SystemError: + # resize() not supported + # No messages are printed, since the output of this test suite + # would then be different across platforms. + pass + else: + # resize() is supported + self.assertEqual(len(m), RESIZE) + # Check that we can no longer seek beyond the new size. + self.assertRaises(ValueError, m.seek, 1+RESIZE, 0) + finally: + m.close() + def test_extended_getslice(self): # Test extended slicing by comparing with list slicing. s = "".join(chr(c) for c in reversed(range(256))) Index: Modules/mmapmodule.c =================================================================== --- Modules/mmapmodule.c (revision 69718) +++ Modules/mmapmodule.c (working copy) @@ -448,17 +448,20 @@ off_hi = 0; off_lo = (DWORD)self->offset; #endif - SetFilePointer(self->file_handle, - newSizeLow, &newSizeHigh, FILE_BEGIN); - /* Change the size of the file */ - SetEndOfFile(self->file_handle); + if (self->file_handle != INVALID_HANDLE_VALUE) { + DWORD copySizeHigh = newSizeHigh; + SetFilePointer(self->file_handle, + newSizeLow, ©SizeHigh, FILE_BEGIN); + /* Change the size of the file */ + SetEndOfFile(self->file_handle); + } /* Create another mapping object and remap the file view */ self->map_handle = CreateFileMapping( self->file_handle, NULL, PAGE_READWRITE, - 0, - 0, + newSizeHigh, + newSizeLow, self->tagname); if (self->map_handle != NULL) { self->data = (char *) MapViewOfFile(self->map_handle,