Skip to content

Commit ea35d4b

Browse files
committed
Update by comments
1 parent e08de1c commit ea35d4b

File tree

3 files changed

+11
-26
lines changed

3 files changed

+11
-26
lines changed

Doc/library/mmap.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
277277
pagefile) will silently create a new map with the original data copied over
278278
up to the length of the new size.
279279

280-
.. availability:: Linux, Windows
280+
Availability: Windows and Systems with the ``mremap()`` system call.
281281

282282
.. versionchanged:: 3.11
283283
Correctly fails if attempting to resize when another map is held

Lib/test/test_mmap.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,12 @@ def test_basic(self):
118118
@unittest.skipUnless(hasattr(mmap.mmap, 'resize'), 'requires mmap.resize')
119119
def test_resize(self):
120120
# Create a file to be mmap'ed.
121-
f = open(TESTFN, 'bw+')
122-
try:
121+
with open(TESTFN, 'bw+') as f:
123122
# Write 2 pages worth of data to the file
124123
f.write(b'\0'* 2 * PAGESIZE)
125124
f.flush()
126125
m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
127126
self.addCleanup(m.close)
128-
finally:
129-
f.close()
130127

131128
# Try resizing map
132129
m.resize(512)
@@ -136,12 +133,9 @@ def test_resize(self):
136133

137134
# Check that the underlying file is truncated too
138135
# (bug #728515)
139-
f = open(TESTFN, 'rb')
140-
try:
136+
with open(TESTFN, 'rb') as f:
141137
f.seek(0, 2)
142138
self.assertEqual(f.tell(), 512)
143-
finally:
144-
f.close()
145139
self.assertEqual(m.size(), 512)
146140

147141
def test_access_parameter(self):
@@ -187,15 +181,10 @@ def test_access_parameter(self):
187181
else:
188182
self.fail("Able to write to readonly memory map")
189183

190-
# Ensuring that readonly mmap can't be resized
191-
try:
192-
m.resize(2*mapsize)
193-
except AttributeError: # resize is not universally supported
194-
pass
195-
except TypeError:
196-
pass
197-
else:
198-
self.fail("Able to resize readonly memory map")
184+
if hasattr(m, 'resize'):
185+
# Ensuring that readonly mmap can't be resized
186+
with self.assertRaises(TypeError):
187+
m.resize(2*mapsize)
199188
with open(TESTFN, "rb") as fp:
200189
self.assertEqual(fp.read(), b'a'*mapsize,
201190
"Readonly memory map data file was modified")
@@ -621,13 +610,9 @@ def test_offset (self):
621610
self.assertEqual(m[0:3], b'foo')
622611
f.close()
623612

624-
# Try resizing map
625-
try:
613+
if hasattr(m, 'resize'):
614+
# Try resizing map
626615
m.resize(512)
627-
except AttributeError:
628-
pass
629-
else:
630-
# resize() is supported
631616
self.assertEqual(len(m), 512)
632617
# Check that we can no longer seek beyond the new size.
633618
self.assertRaises(ValueError, m.seek, 513, 0)

Modules/mmapmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ is_resizeable(mmap_object *self)
657657
return 0;
658658

659659
}
660-
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */
660+
#endif /* MS_WINDOWS || HAVE_MREMAP */
661661

662662

663663
static PyObject *
@@ -912,7 +912,7 @@ mmap_resize_method(PyObject *op, PyObject *args)
912912
#endif /* UNIX */
913913
}
914914
}
915-
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */
915+
#endif /* defined(MS_WINDOWS) || defined(HAVE_MREMAP) */
916916

917917
static PyObject *
918918
mmap_tell_method(PyObject *op, PyObject *Py_UNUSED(ignored))

0 commit comments

Comments
 (0)