diff --git a/src/core/IronPython.Modules/mmap.cs b/src/core/IronPython.Modules/mmap.cs index 56f99230b..d33f997c3 100644 --- a/src/core/IronPython.Modules/mmap.cs +++ b/src/core/IronPython.Modules/mmap.cs @@ -920,6 +920,8 @@ public void resize(long newsize) { throw PythonOps.ValueError("new size out of range"); } + long capacity = checked(_offset + newsize); + if (_handle is not null && (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))) { // resize on Posix platforms @@ -941,14 +943,14 @@ public void resize(long newsize) { // Resize the underlying file as needed. int fd = unchecked((int)_handle.DangerousGetHandle()); - PythonNT.ftruncateUnix(fd, newsize); + PythonNT.ftruncateUnix(fd, capacity); #if NET8_0_OR_GREATER - _file = MemoryMappedFile.CreateFromFile(_handle, _mapName, newsize, _fileAccess, HandleInheritability.None, leaveOpen: true); + _file = MemoryMappedFile.CreateFromFile(_handle, _mapName, capacity, _fileAccess, HandleInheritability.None, leaveOpen: true); #else _sourceStream?.Dispose(); _sourceStream = new FileStream(new SafeFileHandle((IntPtr)fd, ownsHandle: false), FileAccess.ReadWrite); - _file = CreateFromFile(_sourceStream, _mapName, newsize, _fileAccess, HandleInheritability.None, leaveOpen: true); + _file = CreateFromFile(_sourceStream, _mapName, capacity, _fileAccess, HandleInheritability.None, leaveOpen: true); #endif _view = _file.CreateViewAccessor(_offset, newsize, _fileAccess); return; @@ -968,8 +970,6 @@ public void resize(long newsize) { return; } - long capacity = checked(_offset + newsize); - try { if (newsize == 0) { // resizing to an empty mapped region is not allowed @@ -1401,17 +1401,18 @@ private struct SYSTEM_INFO { internal short wProcessorRevision; } + [SupportedOSPlatform("windows")] [DllImport("kernel32", SetLastError = true)] private static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo); private static int GetAllocationGranularity() { - try { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return GetAllocationGranularityWorker(); - } catch { - return System.Environment.SystemPageSize; } + return System.Environment.SystemPageSize; } + [SupportedOSPlatform("windows")] [MethodImpl(MethodImplOptions.NoInlining)] private static int GetAllocationGranularityWorker() { SYSTEM_INFO info = new SYSTEM_INFO(); diff --git a/src/core/IronPython.StdLib/lib/test/test_mmap.py b/src/core/IronPython.StdLib/lib/test/test_mmap.py index 7882be8ca..d21d25578 100644 --- a/src/core/IronPython.StdLib/lib/test/test_mmap.py +++ b/src/core/IronPython.StdLib/lib/test/test_mmap.py @@ -102,7 +102,7 @@ def test_basic(self): # resize() not supported # No messages are printed, since the output of this test suite # would then be different across platforms. - pass + raise # ironpython: all our runners currently support resize else: # resize() is supported self.assertEqual(len(m), 512) @@ -168,7 +168,7 @@ def test_access_parameter(self): try: m.resize(2*mapsize) except SystemError: # resize is not universally supported - pass + raise # ironpython: all our runners currently support resize except TypeError: pass else: @@ -539,7 +539,7 @@ def test_offset (self): try: m.resize(512) except SystemError: - pass + raise # ironpython: all our runners currently support resize else: # resize() is supported self.assertEqual(len(m), 512)