Skip to content

Commit 42244b4

Browse files
committed
Accept str as argument to fcntl and ioctl
1 parent ddd210e commit 42244b4

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

src/core/IronPython.Modules/fcntl.cs

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Reflection;
1212
using System.Runtime.InteropServices;
1313
using System.Runtime.Versioning;
14+
using System.Text;
1415

1516
using Mono.Unix;
1617
using Mono.Unix.Native;
@@ -78,15 +79,13 @@ public static object fcntl(int fd, int cmd, [NotNone] Bytes arg) {
7879
public static object fcntl(int fd, int cmd, [Optional] object? arg) {
7980
CheckFileDescriptor(fd);
8081

81-
long data = arg switch {
82-
Missing => 0,
83-
int i => i,
84-
uint ui => ui,
85-
long l => l,
86-
ulong ul => (long)ul,
87-
BigInteger bi => (long)bi,
88-
Extensible<BigInteger> ebi => (long)ebi.Value,
89-
_ => throw PythonOps.TypeErrorForBadInstance("integer argument expected, got {0}", arg)
82+
if (!TryGetInt64(arg, out long data)) {
83+
return arg switch {
84+
Bytes bytes => fcntl(fd, cmd, bytes),
85+
string s => fcntl(fd, cmd, Bytes.Make(Encoding.UTF8.GetBytes(s))),
86+
Extensible<string> es => fcntl(fd, cmd, Bytes.Make(Encoding.UTF8.GetBytes(es.Value))),
87+
_ => throw PythonOps.TypeErrorForBadInstance("integer or bytes argument expected, got {0}", arg)
88+
};
9089
};
9190

9291
if (!NativeConvert.TryToFcntlCommand(cmd, out FcntlCommand fcntlCommand)) {
@@ -107,15 +106,8 @@ public static object fcntl(int fd, int cmd, [Optional] object? arg) {
107106

108107

109108
[LightThrowing]
110-
public static object fcntl(CodeContext context, object? fd, int cmd, object? arg = null) {
111-
int fileno = GetFileDescriptor(context, fd);
112-
113-
if (arg is Bytes bytes) {
114-
return fcntl(fileno, cmd, bytes);
115-
}
116-
117-
return fcntl(fileno, cmd, arg);
118-
}
109+
public static object fcntl(CodeContext context, object? fd, int cmd, [Optional] object? arg)
110+
=> fcntl(GetFileDescriptor(context, fd), cmd, arg);
119111

120112
#endregion
121113

@@ -235,19 +227,17 @@ public static object ioctl(int fd, long request, [NotNone] IBufferProtocol arg,
235227
public static object ioctl(int fd, long request, [Optional] object? arg, bool mutate_flag = true) {
236228
CheckFileDescriptor(fd);
237229

238-
ulong cmd = unchecked((ulong)request);
239-
240-
long data = arg switch {
241-
Missing => 0,
242-
int i => i,
243-
uint ui => ui,
244-
long l => l,
245-
ulong ul => (long)ul,
246-
BigInteger bi => (long)bi,
247-
Extensible<BigInteger> ebi => (long)ebi.Value,
248-
_ => throw PythonOps.TypeErrorForBadInstance("integer argument expected, got {0}", arg)
230+
if (!TryGetInt64(arg, out long data)) {
231+
return arg switch {
232+
IBufferProtocol bp => ioctl(fd, request, bp),
233+
string s => ioctl(fd, request, Bytes.Make(Encoding.UTF8.GetBytes(s))),
234+
Extensible<string> es => ioctl(fd, request, Bytes.Make(Encoding.UTF8.GetBytes(es.Value))),
235+
_ => throw PythonOps.TypeErrorForBadInstance("integer or a bytes-like argument expected, got {0}", arg)
236+
};
249237
};
250238

239+
ulong cmd = unchecked((ulong)request);
240+
251241
#if !NETCOREAPP
252242
throw new PlatformNotSupportedException("ioctl is not supported on Mono");
253243
#else
@@ -271,15 +261,8 @@ public static object ioctl(int fd, long request, [Optional] object? arg, bool mu
271261

272262

273263
[LightThrowing]
274-
public static object ioctl(CodeContext context, object? fd, long request, [Optional] object? arg, bool mutate_flag = true) {
275-
int fileno = GetFileDescriptor(context, fd);
276-
277-
if (arg is IBufferProtocol bp) {
278-
return ioctl(fileno, request, bp, mutate_flag);
279-
}
280-
281-
return ioctl(fileno, request, arg, mutate_flag);
282-
}
264+
public static object ioctl(CodeContext context, object? fd, long request, [Optional] object? arg, bool mutate_flag = true)
265+
=> ioctl(GetFileDescriptor(context, fd), request, arg, mutate_flag);
283266

284267
#endregion
285268

@@ -376,6 +359,26 @@ private static void CheckFileDescriptor(int fd) {
376359
}
377360
}
378361

362+
363+
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;
380+
}
381+
379382
#endregion
380383

381384

0 commit comments

Comments
 (0)