Skip to content

Commit

Permalink
Support large enums in int constructor (#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp committed Mar 21, 2022
1 parent 0df9230 commit dd09613
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 16 additions & 1 deletion Src/IronPython/Runtime/Operations/BigIntegerOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,22 @@ public static partial class BigIntegerOps {
case decimal val:
return int.MinValue <= val && val <= int.MaxValue ? (object)(int)val : (BigInteger)val;
case Enum e:
return ((IConvertible)e).ToInt32(null); // TODO: check long enums
var ic = (IConvertible)e;
switch (ic.GetTypeCode()) {
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
return ic.ToInt32(null);
case TypeCode.Int64:
return (BigInteger)ic.ToInt64(null);
case TypeCode.UInt32:
case TypeCode.UInt64:
return (BigInteger)ic.ToUInt64(null);
default:
throw new InvalidOperationException(); // unreachable
}
case string s:
return LiteralParser.ParseIntegerSign(s, @base, FindStart(s, @base));
case Extensible<string> es:
Expand Down
8 changes: 7 additions & 1 deletion Tests/test_ironmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import unittest

from iptest import IronPythonTestCase, big, is_mono, run_test, skipUnlessIronPython
from iptest import IronPythonTestCase, big, is_mono, is_netcoreapp, run_test, skipUnlessIronPython

@skipUnlessIronPython()
class IronMathTest(IronPythonTestCase):
Expand Down Expand Up @@ -305,6 +305,12 @@ def test_misc(self):
self.assertEqual(big(1).CompareTo(None), 1)
self.assertEqual(big(1).CompareTo(True), 0)

if is_netcoreapp:
import clr
clr.AddReference("System.Net.Sockets")
from System.Net.Sockets import IOControlCode
self.assertEqual(int(IOControlCode.TranslateHandle), 0xc800000D)

def test_rightshiftby32_negative_bug(self):
# test workaround for https://github.com/dotnet/runtime/issues/43396
from System.Numerics import BigInteger
Expand Down

0 comments on commit dd09613

Please sign in to comment.