Skip to content

Commit

Permalink
Enable test_mmap on net8.0/POSIX (#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp authored Jan 4, 2025
1 parent 1cdaedd commit 97fc78a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
37 changes: 32 additions & 5 deletions Src/IronPython.Modules/mmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
[assembly: PythonModule("mmap", typeof(IronPython.Modules.MmapModule))]
namespace IronPython.Modules {
public static class MmapModule {
public const int ACCESS_DEFAULT = 0; // Since Python 3.7
public const int ACCESS_READ = 1;
public const int ACCESS_WRITE = 2;
public const int ACCESS_COPY = 3;
Expand All @@ -42,8 +43,6 @@ public static class MmapModule {
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
public const int MAP_PRIVATE = 2;

[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
public const int PROT_NONE = 0;
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
public const int PROT_READ = 1;
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
Expand Down Expand Up @@ -78,8 +77,28 @@ public static PythonType mmap {

[PythonType("mmap"), PythonHidden]
public class MmapUnix : MmapDefault {
public MmapUnix(CodeContext/*!*/ context, int fileno, long length, int flags = MAP_SHARED, int prot = PROT_WRITE | PROT_READ, int access = ACCESS_WRITE, long offset = 0)
: base(context, fileno, length, null, access, offset) { }
public MmapUnix(CodeContext/*!*/ context, int fileno, long length, int flags = MAP_SHARED, int prot = PROT_WRITE | PROT_READ, int access = ACCESS_DEFAULT, long offset = 0)
: base(context, fileno, length, null, NormalizeAccess(flags, prot, access), offset) { }

private static int NormalizeAccess(int flags, int prot, int access) {
if (access == ACCESS_DEFAULT) {
if ((flags & (MAP_PRIVATE | MAP_SHARED)) == 0) {
throw PythonOps.OSError(PythonErrorNumber.EINVAL, "Invalid argument");
}
if ((prot & PROT_WRITE) != 0) {
return (flags & MAP_PRIVATE) != 0 ? ACCESS_COPY : ACCESS_WRITE;
}
if ((prot & PROT_READ) != 0) {
return ACCESS_READ;
}
throw PythonOps.NotImplementedError("this combination of flags and prot is not supported");
} else if (flags != MAP_SHARED || prot != (PROT_WRITE | PROT_READ)) {
throw PythonOps.ValueError("mmap can't specify both access and flags, prot.");
} else if (access != ACCESS_READ && access != ACCESS_WRITE && access != ACCESS_COPY) {
throw PythonOps.ValueError("mmap invalid access parameter");
}
return access;
}
}

[PythonType("mmap"), PythonHidden]
Expand All @@ -96,11 +115,12 @@ public class MmapDefault : IWeakReferenceable {
private volatile bool _isClosed;
private int _refCount = 1;

public MmapDefault(CodeContext/*!*/ context, int fileno, long length, string tagname = null, int access = ACCESS_WRITE, long offset = 0) {
public MmapDefault(CodeContext/*!*/ context, int fileno, long length, string tagname = null, int access = ACCESS_DEFAULT, long offset = 0) {
switch (access) {
case ACCESS_READ:
_fileAccess = MemoryMappedFileAccess.Read;
break;
case ACCESS_DEFAULT: // On Windows, default access is write-through
case ACCESS_WRITE:
_fileAccess = MemoryMappedFileAccess.ReadWrite;
break;
Expand Down Expand Up @@ -170,6 +190,13 @@ public MmapDefault(CodeContext/*!*/ context, int fileno, long length, string tag
length -= _offset;
}

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
// Unix map does not support increasing size on open
if (_offset + length > _sourceStream.Length) {
throw PythonOps.ValueError("mmap length is greater than file size");
}
}

long capacity = checked(_offset + length);

// Enlarge the file as needed.
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPythonTest/Cases/CPythonCasesManifest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ IsolationLevel=PROCESS # Also weakref failures; https://github.com/IronLanguages
Ignore=true

[CPython.test_mmap]
RunCondition=NOT $(IS_POSIX)
RunCondition=NOT $(IS_POSIX) OR (NOT $(IS_MONO) AND '$(FRAMEWORK)' <> '.NETCoreApp,Version=v6.0')
IsolationLevel=PROCESS

[CPython.test_module]
Expand Down

0 comments on commit 97fc78a

Please sign in to comment.