Skip to content

Commit f874612

Browse files
authored
Fix mmap.resize with offset (#1897)
* Fix mmap.resize with offset * Add SupportedOSPlatform
1 parent 144146d commit f874612

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/core/IronPython.Modules/mmap.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,8 @@ public void resize(long newsize) {
920920
throw PythonOps.ValueError("new size out of range");
921921
}
922922

923+
long capacity = checked(_offset + newsize);
924+
923925
if (_handle is not null
924926
&& (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))) {
925927
// resize on Posix platforms
@@ -941,14 +943,14 @@ public void resize(long newsize) {
941943

942944
// Resize the underlying file as needed.
943945
int fd = unchecked((int)_handle.DangerousGetHandle());
944-
PythonNT.ftruncateUnix(fd, newsize);
946+
PythonNT.ftruncateUnix(fd, capacity);
945947

946948
#if NET8_0_OR_GREATER
947-
_file = MemoryMappedFile.CreateFromFile(_handle, _mapName, newsize, _fileAccess, HandleInheritability.None, leaveOpen: true);
949+
_file = MemoryMappedFile.CreateFromFile(_handle, _mapName, capacity, _fileAccess, HandleInheritability.None, leaveOpen: true);
948950
#else
949951
_sourceStream?.Dispose();
950952
_sourceStream = new FileStream(new SafeFileHandle((IntPtr)fd, ownsHandle: false), FileAccess.ReadWrite);
951-
_file = CreateFromFile(_sourceStream, _mapName, newsize, _fileAccess, HandleInheritability.None, leaveOpen: true);
953+
_file = CreateFromFile(_sourceStream, _mapName, capacity, _fileAccess, HandleInheritability.None, leaveOpen: true);
952954
#endif
953955
_view = _file.CreateViewAccessor(_offset, newsize, _fileAccess);
954956
return;
@@ -968,8 +970,6 @@ public void resize(long newsize) {
968970
return;
969971
}
970972

971-
long capacity = checked(_offset + newsize);
972-
973973
try {
974974
if (newsize == 0) {
975975
// resizing to an empty mapped region is not allowed
@@ -1401,17 +1401,18 @@ private struct SYSTEM_INFO {
14011401
internal short wProcessorRevision;
14021402
}
14031403

1404+
[SupportedOSPlatform("windows")]
14041405
[DllImport("kernel32", SetLastError = true)]
14051406
private static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);
14061407

14071408
private static int GetAllocationGranularity() {
1408-
try {
1409+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
14091410
return GetAllocationGranularityWorker();
1410-
} catch {
1411-
return System.Environment.SystemPageSize;
14121411
}
1412+
return System.Environment.SystemPageSize;
14131413
}
14141414

1415+
[SupportedOSPlatform("windows")]
14151416
[MethodImpl(MethodImplOptions.NoInlining)]
14161417
private static int GetAllocationGranularityWorker() {
14171418
SYSTEM_INFO info = new SYSTEM_INFO();

src/core/IronPython.StdLib/lib/test/test_mmap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_basic(self):
102102
# resize() not supported
103103
# No messages are printed, since the output of this test suite
104104
# would then be different across platforms.
105-
pass
105+
raise # ironpython: all our runners currently support resize
106106
else:
107107
# resize() is supported
108108
self.assertEqual(len(m), 512)
@@ -168,7 +168,7 @@ def test_access_parameter(self):
168168
try:
169169
m.resize(2*mapsize)
170170
except SystemError: # resize is not universally supported
171-
pass
171+
raise # ironpython: all our runners currently support resize
172172
except TypeError:
173173
pass
174174
else:
@@ -539,7 +539,7 @@ def test_offset (self):
539539
try:
540540
m.resize(512)
541541
except SystemError:
542-
pass
542+
raise # ironpython: all our runners currently support resize
543543
else:
544544
# resize() is supported
545545
self.assertEqual(len(m), 512)

0 commit comments

Comments
 (0)