Skip to content

Commit 7ae32b1

Browse files
authored
Add test for int ctor (#1155)
1 parent 4b75732 commit 7ae32b1

4 files changed

Lines changed: 65 additions & 13 deletions

File tree

Src/IronPython/Lib/iptest/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,3 @@
66
from .test_env import *
77
from .type_util import *
88
from .misc_util import ip_supported_encodings
9-
10-
long = type(1 << 63) # https://github.com/IronLanguages/ironpython3/issues/52

Src/IronPython/Lib/iptest/type_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
# types derived from built-in types
66

7+
long = type(1 << 63) # https://github.com/IronLanguages/ironpython3/issues/52
8+
79
class myint(int): pass
8-
class mylong(int): pass
10+
class mylong(long): pass
911
class myfloat(float): pass
1012
class mycomplex(complex): pass
1113

Src/IronPython/Runtime/Operations/IntOps.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ private static object FastNew(CodeContext/*!*/ context, object o, int @base = 10
3232
case Extensible<int> ei:
3333
return TryInvokeInt(context, o, out var value) ? value : ei.Value;
3434
case BigInteger val:
35-
return val.IsInt32() ? (int)val : o;
36-
case Extensible<BigInteger> el:
37-
return TryInvokeInt(context, o, out result) ? result : el.Value.IsInt32() ? (int)el.Value : el.Value;
35+
return val.IsInt32() ? (object)(int)val : o;
36+
case Extensible<BigInteger> ebi:
37+
return TryInvokeInt(context, o, out result) ? result : ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value;
3838
case float f:
3939
return DoubleOps.__int__(f);
4040
case long val:
41-
return int.MinValue <= val && val <= int.MaxValue ? (int)val : (BigInteger)val;
41+
return int.MinValue <= val && val <= int.MaxValue ? (object)(int)val : (BigInteger)val;
4242
case uint val:
43-
return val <= int.MaxValue ? (int)val : (BigInteger)val;
43+
return val <= int.MaxValue ? (object)(int)val : (BigInteger)val;
4444
case ulong val:
45-
return val <= int.MaxValue ? (int)val : (BigInteger)val;
45+
return val <= int.MaxValue ? (object)(int)val : (BigInteger)val;
4646
case decimal val:
47-
return int.MinValue <= val && val <= int.MaxValue ? (int)val : (BigInteger)val;
47+
return int.MinValue <= val && val <= int.MaxValue ? (object)(int)val : (BigInteger)val;
4848
case Enum e:
4949
return ((IConvertible)e).ToInt32(null);
5050
case string s:
@@ -62,13 +62,13 @@ private static object FastNew(CodeContext/*!*/ context, object o, int @base = 10
6262
case int _:
6363
return result;
6464
case BigInteger bi:
65-
return bi.IsInt32() ? (int)bi : result;
65+
return bi.IsInt32() ? (object)(int)bi : result;
6666
case bool b:
6767
return BoolOps.__int__(b); // Python 3.6: return the int value
6868
case Extensible<int> ei:
6969
return ei.Value; // Python 3.6: return the int value
7070
case Extensible<BigInteger> ebi:
71-
return ebi.Value.IsInt32() ? (int)ebi.Value : ebi.Value; // Python 3.6: return the int value
71+
return ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value; // Python 3.6: return the int value
7272
default: {
7373
if (TryInvokeInt(context, result, out var intResult)) {
7474
return intResult;
@@ -98,7 +98,7 @@ static bool TryInvokeInt(CodeContext context, object o, out object result) {
9898
return true;
9999
case Extensible<BigInteger> ebi:
100100
Warn(context, result);
101-
result = ebi.Value.IsInt32() ? (int)ebi.Value : ebi.Value; // Python 3.6: return the int value
101+
result = ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value; // Python 3.6: return the int value
102102
return true;
103103
default:
104104
throw PythonOps.TypeError("__int__ returned non-int (type {0})", PythonTypeOps.GetName(result));

Tests/test_int.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,66 @@
22
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
# See the LICENSE file in the project root for more information.
44

5+
import sys
56
import unittest
67

8+
from iptest import is_cli
9+
710
class IntSubclass(int): pass
811

912
class IntTest(unittest.TestCase):
1013
def test_from_bytes(self):
1114
self.assertEqual(type(int.from_bytes(b"abc", "big")), int)
1215
self.assertEqual(type(IntSubclass.from_bytes(b"abc", "big")), IntSubclass) # https://github.com/IronLanguages/ironpython3/pull/973
1316

17+
def test_int(self):
18+
from iptest import long, myint, mylong
19+
20+
class MyTrunc:
21+
def __init__(self, x):
22+
self.x = x
23+
def __trunc__(self):
24+
return self.x
25+
26+
class MyInt:
27+
def __init__(self, x):
28+
self.x = x
29+
def __int__(self):
30+
return self.x
31+
32+
for value in ("0", b"0"):
33+
# int(str/bytes)
34+
v = int(value)
35+
self.assertEqual(v, 0)
36+
self.assertIs(type(v), int)
37+
38+
for t in (bool, int, long, myint, mylong):
39+
# int()
40+
v = int(t(0))
41+
self.assertEqual(v, 0)
42+
self.assertIs(type(v), int)
43+
44+
# __trunc__
45+
v = int(MyTrunc(t(0)))
46+
self.assertEqual(v, 0)
47+
self.assertIs(type(v), int if is_cli or sys.version_info >= (3,6) else t)
48+
49+
# __int__
50+
if t in (bool, myint, mylong):
51+
with self.assertWarns(DeprecationWarning):
52+
v = int(MyInt(t(0)))
53+
else:
54+
v = int(MyInt(t(0)))
55+
self.assertEqual(v, 0)
56+
self.assertIs(type(v), int if is_cli or sys.version_info >= (3,6) else t)
57+
58+
if is_cli:
59+
from iptest import clr_all_types
60+
for t in clr_all_types:
61+
# int(System.*)
62+
v = int(t(0))
63+
self.assertEqual(v, 0)
64+
self.assertIs(type(v), int)
65+
1466
if __name__ == "__main__":
1567
unittest.main()

0 commit comments

Comments
 (0)