Skip to content

Commit 5b39885

Browse files
committed
Use TryToIndex for integer conversion
1 parent cba3964 commit 5b39885

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/core/IronPython.Modules/termios.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using IronPython.Runtime.Types;
2020

2121
using static IronPython.Modules.PythonIOModule;
22+
using IronPython.Hosting;
2223

2324

2425
[assembly: PythonModule("termios", typeof(IronPython.Modules.PythonTermios), PlatformsAttribute.PlatformFamily.Unix)]
@@ -493,14 +494,9 @@ static byte GetControlChar(object? o) {
493494
}
494495

495496
static ulong ToUInt64(object? o)
496-
=> o switch {
497-
int i => (ulong)i,
498-
uint ui => ui,
499-
long l => (ulong)l,
500-
BigInteger bi => (ulong)bi,
501-
Extensible<BigInteger> ebi => (ulong)ebi.Value,
502-
_ => throw PythonOps.TypeErrorForBadInstance("tcsetattr: an integer is required (got type {0})", o)
503-
};
497+
=> PythonOps.TryToIndex(o, out BigInteger index)
498+
? (ulong)index
499+
: throw PythonOps.TypeErrorForBadInstance("tcsetattr: an integer is required (got type {0})", o);
504500
}
505501

506502
[LightThrowing]
@@ -759,7 +755,7 @@ private static void CheckFileDescriptor(int fd) {
759755
if (fd < 0) {
760756
throw PythonOps.ValueError("file descriptor cannot be a negative integer ({0})", fd);
761757
}
762-
}
758+
}
763759

764760

765761
private static object ToPythonInt(this ulong value)

0 commit comments

Comments
 (0)