Skip to content

Commit 474a4aa

Browse files
authored
Implement __int__ across all integer types (#1382)
1 parent c637be7 commit 474a4aa

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

Src/IronPython/Runtime/Operations/IntOps.Generated.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public static object Abs(SByte x) {
9393

9494
public static SByte __trunc__(SByte x) => x;
9595

96+
public static int __int__(SByte x) => unchecked((int)x);
97+
9698
public static int __index__(SByte x) => unchecked((int)x);
9799

98100
public static int __hash__(SByte x) {
@@ -353,6 +355,8 @@ public static object __new__(PythonType cls, object value) {
353355

354356
public static Byte __trunc__(Byte x) => x;
355357

358+
public static int __int__(Byte x) => unchecked((int)x);
359+
356360
public static int __index__(Byte x) => unchecked((int)x);
357361

358362
public static int __hash__(Byte x) {
@@ -715,6 +719,8 @@ public static object Abs(Int16 x) {
715719

716720
public static Int16 __trunc__(Int16 x) => x;
717721

722+
public static int __int__(Int16 x) => unchecked((int)x);
723+
718724
public static int __index__(Int16 x) => unchecked((int)x);
719725

720726
public static int __hash__(Int16 x) {
@@ -980,6 +986,8 @@ public static object __new__(PythonType cls, object value) {
980986

981987
public static UInt16 __trunc__(UInt16 x) => x;
982988

989+
public static int __int__(UInt16 x) => unchecked((int)x);
990+
983991
public static int __index__(UInt16 x) => unchecked((int)x);
984992

985993
public static int __hash__(UInt16 x) {
@@ -1352,6 +1360,8 @@ public static object Abs(Int32 x) {
13521360

13531361
public static Int32 __trunc__(Int32 x) => x;
13541362

1363+
public static int __int__(Int32 x) => unchecked((int)x);
1364+
13551365
public static int __index__(Int32 x) => unchecked((int)x);
13561366

13571367
public static int __hash__(Int32 x) {
@@ -1592,6 +1602,8 @@ public static object __new__(PythonType cls, object value) {
15921602

15931603
public static UInt32 __trunc__(UInt32 x) => x;
15941604

1605+
public static BigInteger __int__(UInt32 x) => unchecked((BigInteger)x);
1606+
15951607
public static BigInteger __index__(UInt32 x) => unchecked((BigInteger)x);
15961608

15971609
public static int __hash__(UInt32 x) {
@@ -1964,6 +1976,8 @@ public static object Abs(Int64 x) {
19641976

19651977
public static Int64 __trunc__(Int64 x) => x;
19661978

1979+
public static BigInteger __int__(Int64 x) => unchecked((BigInteger)x);
1980+
19671981
public static BigInteger __index__(Int64 x) => unchecked((BigInteger)x);
19681982

19691983
public static int __hash__(Int64 x) {
@@ -2232,6 +2246,8 @@ public static object __new__(PythonType cls, object value) {
22322246

22332247
public static UInt64 __trunc__(UInt64 x) => x;
22342248

2249+
public static BigInteger __int__(UInt64 x) => unchecked((BigInteger)x);
2250+
22352251
public static BigInteger __index__(UInt64 x) => unchecked((BigInteger)x);
22362252

22372253
public static int __hash__(UInt64 x) {

Src/IronPython/Runtime/Operations/IntOps.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,6 @@ public static object __rdivmod__(int x, int y) {
154154
return __divmod__(y, x);
155155
}
156156

157-
public static int __int__(int self) {
158-
return self;
159-
}
160-
161157
public static double __float__(int self) {
162158
return (double)self;
163159
}

Src/Scripts/generate_alltypes.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def is_implicit(self, oty):
104104
simple_identity_method = """
105105
public static %(type)s %(method_name)s(%(type)s x) => x;"""
106106

107+
unchecked_cast_method = """
108+
public static %(cast_type)s %(method_name)s(%(type)s x) => unchecked((%(cast_type)s)x);"""
109+
107110
identity_method = """
108111
[SpecialName]
109112
public static %(type)s %(method_name)s(%(type)s x) => x;"""
@@ -175,11 +178,12 @@ def gen_unaryops(cw, ty):
175178
else:
176179
cw.write(simple_identity_method, type=ty.name, method_name="__trunc__")
177180

178-
cw.writeline()
179181
if ty.max > Int32.MaxValue:
180-
cw.write('public static BigInteger __index__(%s x) => unchecked((BigInteger)x);' % (ty.name))
182+
cw.write(unchecked_cast_method, type=ty.name, method_name="__int__", cast_type="BigInteger")
183+
cw.write(unchecked_cast_method, type=ty.name, method_name="__index__", cast_type="BigInteger")
181184
else:
182-
cw.write('public static int __index__(%s x) => unchecked((int)x);' % (ty.name))
185+
cw.write(unchecked_cast_method, type=ty.name, method_name="__int__", cast_type="int")
186+
cw.write(unchecked_cast_method, type=ty.name, method_name="__index__", cast_type="int")
183187

184188
cw.writeline()
185189
cw.enter_block('public static int __hash__(%s x)' % (ty.name))

0 commit comments

Comments
 (0)