From 00c9eb2c2367f5ce8cb4bdb79f85dd844529d5f4 Mon Sep 17 00:00:00 2001 From: Pavel Koneski Date: Wed, 5 Feb 2025 13:29:25 -0800 Subject: [PATCH 1/2] Support `ioctl` on Mono --- src/core/IronPython.Modules/fcntl.cs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/core/IronPython.Modules/fcntl.cs b/src/core/IronPython.Modules/fcntl.cs index bee20458d..6afa74a3f 100644 --- a/src/core/IronPython.Modules/fcntl.cs +++ b/src/core/IronPython.Modules/fcntl.cs @@ -5,7 +5,6 @@ #nullable enable using System; -using System.Buffers; using System.Diagnostics; using System.Numerics; using System.Reflection; @@ -173,9 +172,6 @@ public static object ioctl(int fd, long request, [NotNone] IBufferProtocol arg, } bool in_place = bufSize > defaultBufSize; // only large buffers are mutated in place -#if !NETCOREAPP - throw new PlatformNotSupportedException("ioctl is not supported on Mono"); -#else try { Debug.Assert(!in_place || mutate_flag); // in_place implies mutate_flag @@ -192,7 +188,7 @@ public static object ioctl(int fd, long request, [NotNone] IBufferProtocol arg, unsafe { fixed (byte* ptr = workSpan) { do { - if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64) { + if (IsArchitecutreArm64()) { // workaround for Arm64 vararg calling convention (but not for ARM64EC on Windows) result = _ioctl_arm64(fd, cmd, 0, 0, 0, 0, 0, 0, ptr); } else { @@ -219,7 +215,6 @@ public static object ioctl(int fd, long request, [NotNone] IBufferProtocol arg, } finally { buf.Dispose(); } -#endif } @@ -238,13 +233,10 @@ public static object ioctl(int fd, long request, [Optional] object? arg, bool mu ulong cmd = unchecked((ulong)request); -#if !NETCOREAPP - throw new PlatformNotSupportedException("ioctl is not supported on Mono"); -#else int result; Errno errno; do { - if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64) { + if (IsArchitecutreArm64()) { // workaround for Arm64 vararg calling convention (but not for ARM64EC on Windows) result = _ioctl_arm64(fd, cmd, 0, 0, 0, 0, 0, 0, data); } else { @@ -256,7 +248,6 @@ public static object ioctl(int fd, long request, [Optional] object? arg, bool mu return LightExceptions.Throw(PythonNT.GetOsError(NativeConvert.FromErrno(errno))); } return ScriptingRuntimeHelpers.Int32ToObject(result); -#endif } @@ -359,11 +350,21 @@ private static void CheckFileDescriptor(int fd) { } } + private static bool IsArchitecutreArm64() { +#if NETCOREAPP + return RuntimeInformation.ProcessArchitecture == Architecture.Arm64; +#else + if (Syscall.uname(out Utsname info) == 0) { + return info.machine is "arm64" or "aarch64"; + } + return false; +#endif + } private static bool TryGetInt64(object? obj, out long value) { int success = 1; value = obj switch { - Missing => 0, + Missing => default, int i => i, uint ui => ui, long l => l, From 4c8b33427c5187443ce5e7ddbedcd4a03d0da0ec Mon Sep 17 00:00:00 2001 From: Pavel Koneski Date: Thu, 6 Feb 2025 14:08:17 -0800 Subject: [PATCH 2/2] Support `__index__` in conversion to `Int64` --- src/core/IronPython.Modules/fcntl.cs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/core/IronPython.Modules/fcntl.cs b/src/core/IronPython.Modules/fcntl.cs index 6afa74a3f..67aeb43d1 100644 --- a/src/core/IronPython.Modules/fcntl.cs +++ b/src/core/IronPython.Modules/fcntl.cs @@ -362,22 +362,15 @@ private static bool IsArchitecutreArm64() { } private static bool TryGetInt64(object? obj, out long value) { - int success = 1; - value = obj switch { - Missing => default, - int i => i, - uint ui => ui, - long l => l, - ulong ul => (long)ul, - BigInteger bi => (long)bi, - Extensible ebi => (long)ebi.Value, - byte b => b, - sbyte sb => sb, - short s => s, - ushort us => us, - _ => success = 0 - }; - return success != 0; + value = default; + if (obj is Missing) { + return true; + } + if (PythonOps.TryToIndex(obj, out BigInteger bi)) { + value = (long)bi; + return true; + } + return false; } #endregion