Skip to content

Commit 2c5de6f

Browse files
authored
Support ioctl on Mono (#1905)
1 parent 662a97d commit 2c5de6f

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

src/core/IronPython.Modules/fcntl.cs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#nullable enable
66

77
using System;
8-
using System.Buffers;
98
using System.Diagnostics;
109
using System.Numerics;
1110
using System.Reflection;
@@ -173,9 +172,6 @@ public static object ioctl(int fd, long request, [NotNone] IBufferProtocol arg,
173172
}
174173
bool in_place = bufSize > defaultBufSize; // only large buffers are mutated in place
175174

176-
#if !NETCOREAPP
177-
throw new PlatformNotSupportedException("ioctl is not supported on Mono");
178-
#else
179175
try {
180176
Debug.Assert(!in_place || mutate_flag); // in_place implies mutate_flag
181177

@@ -192,7 +188,7 @@ public static object ioctl(int fd, long request, [NotNone] IBufferProtocol arg,
192188
unsafe {
193189
fixed (byte* ptr = workSpan) {
194190
do {
195-
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64) {
191+
if (IsArchitecutreArm64()) {
196192
// workaround for Arm64 vararg calling convention (but not for ARM64EC on Windows)
197193
result = _ioctl_arm64(fd, cmd, 0, 0, 0, 0, 0, 0, ptr);
198194
} else {
@@ -219,7 +215,6 @@ public static object ioctl(int fd, long request, [NotNone] IBufferProtocol arg,
219215
} finally {
220216
buf.Dispose();
221217
}
222-
#endif
223218
}
224219

225220

@@ -238,13 +233,10 @@ public static object ioctl(int fd, long request, [Optional] object? arg, bool mu
238233

239234
ulong cmd = unchecked((ulong)request);
240235

241-
#if !NETCOREAPP
242-
throw new PlatformNotSupportedException("ioctl is not supported on Mono");
243-
#else
244236
int result;
245237
Errno errno;
246238
do {
247-
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64) {
239+
if (IsArchitecutreArm64()) {
248240
// workaround for Arm64 vararg calling convention (but not for ARM64EC on Windows)
249241
result = _ioctl_arm64(fd, cmd, 0, 0, 0, 0, 0, 0, data);
250242
} else {
@@ -256,7 +248,6 @@ public static object ioctl(int fd, long request, [Optional] object? arg, bool mu
256248
return LightExceptions.Throw(PythonNT.GetOsError(NativeConvert.FromErrno(errno)));
257249
}
258250
return ScriptingRuntimeHelpers.Int32ToObject(result);
259-
#endif
260251
}
261252

262253

@@ -359,24 +350,27 @@ private static void CheckFileDescriptor(int fd) {
359350
}
360351
}
361352

353+
private static bool IsArchitecutreArm64() {
354+
#if NETCOREAPP
355+
return RuntimeInformation.ProcessArchitecture == Architecture.Arm64;
356+
#else
357+
if (Syscall.uname(out Utsname info) == 0) {
358+
return info.machine is "arm64" or "aarch64";
359+
}
360+
return false;
361+
#endif
362+
}
362363

363364
private static bool TryGetInt64(object? obj, out long value) {
364-
int success = 1;
365-
value = obj switch {
366-
Missing => 0,
367-
int i => i,
368-
uint ui => ui,
369-
long l => l,
370-
ulong ul => (long)ul,
371-
BigInteger bi => (long)bi,
372-
Extensible<BigInteger> ebi => (long)ebi.Value,
373-
byte b => b,
374-
sbyte sb => sb,
375-
short s => s,
376-
ushort us => us,
377-
_ => success = 0
378-
};
379-
return success != 0;
365+
value = default;
366+
if (obj is Missing) {
367+
return true;
368+
}
369+
if (PythonOps.TryToIndex(obj, out BigInteger bi)) {
370+
value = (long)bi;
371+
return true;
372+
}
373+
return false;
380374
}
381375

382376
#endregion

0 commit comments

Comments
 (0)