Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support large enums in int constructor #1367

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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