Skip to content

Commit 93425d5

Browse files
committed
Fix mmap.resize errors
1 parent 1cd8e71 commit 93425d5

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

Src/IronPython.Modules/mmap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ public void resize(long newsize) {
846846

847847
if (newsize == 0) {
848848
// resizing to an empty mapped region is not allowed
849-
throw WindowsError(_offset == 0
849+
throw WindowsError(_offset != 0
850850
? PythonExceptions._OSError.ERROR_ACCESS_DENIED
851851
: PythonExceptions._OSError.ERROR_FILE_INVALID
852852
);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Licensed to the .NET Foundation under one or more agreements.
2+
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information.
4+
5+
'''
6+
Tests the _mmap standard module.
7+
'''
8+
9+
import sys
10+
import os
11+
import errno
12+
import mmap
13+
14+
from iptest import IronPythonTestCase, is_cli, is_posix, is_windows, run_test
15+
16+
class MmapTest(IronPythonTestCase):
17+
18+
def setUp(self):
19+
super(MmapTest, self).setUp()
20+
21+
self.temp_file = os.path.join(self.temporary_dir, "temp_%d.dat" % os.getpid())
22+
23+
def tearDown(self):
24+
self.delete_files(self.temp_file)
25+
return super().tearDown()
26+
27+
28+
def test_constants(self):
29+
self.assertTrue(hasattr(mmap, "PAGESIZE"))
30+
self.assertTrue(hasattr(mmap, "ALLOCATIONGRANULARITY"))
31+
32+
self.assertEqual(mmap.ACCESS_READ, 1)
33+
self.assertEqual(mmap.ACCESS_WRITE, 2)
34+
self.assertEqual(mmap.ACCESS_COPY, 3)
35+
if sys.version_info >= (3, 7) or is_cli:
36+
self.assertEqual(mmap.ACCESS_DEFAULT, 0)
37+
self.assertFalse(hasattr(mmap, "ACCESS_NONE"))
38+
39+
if is_posix:
40+
self.assertEqual(mmap.MAP_SHARED, 1)
41+
self.assertEqual(mmap.MAP_PRIVATE, 2)
42+
self.assertEqual(mmap.PROT_READ, 1)
43+
self.assertEqual(mmap.PROT_WRITE, 2)
44+
self.assertEqual(mmap.PROT_EXEC, 4)
45+
46+
47+
def test_resize_errors(self):
48+
with open(self.temp_file, "wb+") as f:
49+
f.write(b"x" * mmap.ALLOCATIONGRANULARITY * 2)
50+
51+
with open(self.temp_file, "rb+") as f:
52+
m = mmap.mmap(f.fileno(), 0, offset=0)
53+
with self.assertRaises(OSError) as cm:
54+
m.resize(0)
55+
56+
self.assertEqual(cm.exception.errno, errno.EINVAL) # 22
57+
if is_windows:
58+
self.assertEqual(cm.exception.winerror, 1006) # ERROR_FILE_INVALID
59+
self.assertEqual(cm.exception.strerror, "The volume for a file has been externally altered so that the opened file is no longer valid")
60+
else:
61+
self.assertEqual(cm.exception.strerror, "Invalid argument")
62+
63+
64+
def test_resize_errors_negative(self):
65+
with open(self.temp_file, "wb+") as f:
66+
f.write(b"x" * mmap.ALLOCATIONGRANULARITY * 2)
67+
68+
with open(self.temp_file, "rb+") as f:
69+
m = mmap.mmap(f.fileno(), 0, offset=0)
70+
if is_cli or sys.version_info >= (3, 5):
71+
self.assertRaises(ValueError, m.resize, -1)
72+
else:
73+
self.assertRaises(OSError, m.resize, -1)
74+
75+
m.close()
76+
77+
78+
def test_resize_errors_offset(self):
79+
with open(self.temp_file, "wb+") as f:
80+
f.write(b"x" * mmap.ALLOCATIONGRANULARITY * 2)
81+
82+
with open(self.temp_file, "rb+") as f:
83+
m = mmap.mmap(f.fileno(), 0, offset=mmap.ALLOCATIONGRANULARITY)
84+
85+
if is_windows:
86+
with self.assertRaises(PermissionError) as cm:
87+
m.resize(0)
88+
self.assertEqual(cm.exception.errno, errno.EACCES) # 13
89+
self.assertEqual(cm.exception.winerror, 5) # ERROR_ACCESS_DENIED
90+
self.assertEqual(cm.exception.strerror, "Access is denied")
91+
else:
92+
with self.assertRaises(OSError) as cm:
93+
m.resize(0)
94+
self.assertEqual(cm.exception.errno, errno.EINVAL) # 22
95+
self.assertEqual(cm.exception.strerror, "Invalid argument")
96+
m.close()
97+
98+
99+
run_test(__name__)
100+

0 commit comments

Comments
 (0)