Skip to content

Commit c55190b

Browse files
authored
Rename ConvertToLong to ConvertToInt (#1403)
* Rename ConvertToLong to ConvertToBigInt * Remove ConvertToInt * Rename ConvertToBigInt to ConvertToInt * Clean up XXXConvertToYYY in PythonOps * Optimize conversions
1 parent 460136d commit c55190b

5 files changed

Lines changed: 39 additions & 36 deletions

File tree

Src/IronPython.Modules/_csv.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,7 @@ public void writerow(CodeContext/*!*/ context, object sequence) {
910910
object field = e.Current;
911911
var quoted = _dialect.quoting switch {
912912
QUOTE_NONNUMERIC => !(PythonOps.CheckingConvertToFloat(field) ||
913-
PythonOps.CheckingConvertToInt(field) ||
914-
PythonOps.CheckingConvertToLong(field)),
913+
PythonOps.CheckingConvertToInt(field)),
915914
QUOTE_ALL => true,
916915
_ => false,
917916
};

Src/IronPython/Runtime/Binding/MetaUserObject.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private DynamicMetaObject TryPythonConversion(DynamicMetaObjectBinder conversion
179179
(x) => x);
180180
} else if (type == typeof(BigInteger)) {
181181
if (!typeof(Extensible<BigInteger>).IsAssignableFrom(LimitType)) {
182-
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToLong",
182+
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToInt",
183183
() => FallbackConvert(conversion),
184184
(x) => Ast.Call(null, typeof(PythonOps).GetMethod(nameof(PythonOps.ConvertIntToBigInt)), x)); // GH #52
185185
}
@@ -192,7 +192,9 @@ private DynamicMetaObject TryPythonConversion(DynamicMetaObjectBinder conversion
192192
}
193193
break;
194194
case TypeCode.Int32:
195-
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToInt");
195+
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToInt",
196+
() => FallbackConvert(conversion),
197+
(x) => Ast.Call(null, typeof(PythonOps).GetMethod(nameof(PythonOps.ConvertIntToInt32)), x)); // GH #52
196198
case TypeCode.Double:
197199
return MakeConvertRuleForCall(conversion, type, this, "__float__", "ConvertToFloat");
198200
case TypeCode.Boolean:

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,55 +2816,58 @@ public static object ConvertToPythonPrimitive(object value) {
28162816
return value switch
28172817
{
28182818
float f => (double)f,
2819-
double d => d,
28202819
sbyte sb => (int)sb,
28212820
byte b => (int)b,
28222821
char c => (int)c,
28232822
short s => (int)s,
28242823
ushort us => (int)us,
2825-
int i => i,
28262824
uint ui => (BigInteger)ui,
28272825
long l => (BigInteger)l,
28282826
ulong ul => (BigInteger)ul,
2829-
BigInteger bi => bi,
2830-
bool b => b,
2827+
// no conversion needed for: double, int, BigInteger, bool
28312828
_ => value,
28322829
};
28332830
}
28342831

28352832
public static object? ConvertFloatToComplex(object value) {
2836-
if (value == null) {
2837-
return null;
2838-
}
2839-
2840-
if (value is double d) return new Complex(d, 0.0);
2841-
if (value is Extensible<double> ed) return new Complex(ed.Value, 0.0);
2842-
throw new InvalidOperationException();
2833+
return value switch {
2834+
null => null,
2835+
double d => new Complex(d, 0.0),
2836+
Extensible<double> ed => new Complex(ed.Value, 0.0),
2837+
_ => throw new InvalidOperationException(),
2838+
};
28432839
}
28442840

28452841
public static object? ConvertIntToBigInt(object? value) {
28462842
return value switch {
28472843
null => null,
28482844
int i => new BigInteger(i),
2849-
BigInteger bi => bi,
2845+
BigInteger => value,
2846+
Extensible<BigInteger> ebi => ebi.Value,
28502847
_ => throw new InvalidOperationException(),
28512848
};
28522849
}
28532850

2854-
internal static bool CheckingConvertToInt(object value) {
2855-
return value is int || value is BigInteger || value is Extensible<BigInteger>;
2851+
public static object? ConvertIntToInt32(object? value) {
2852+
return value switch {
2853+
null => null,
2854+
int => value,
2855+
BigInteger bi => (int)bi,
2856+
Extensible<BigInteger> ebi => (int)ebi.Value,
2857+
_ => throw new InvalidOperationException(),
2858+
};
28562859
}
28572860

2858-
internal static bool CheckingConvertToLong(object value) {
2859-
return CheckingConvertToInt(value);
2861+
internal static bool CheckingConvertToInt(object value) {
2862+
return value is int || value is BigInteger || value is Extensible<BigInteger>;
28602863
}
28612864

28622865
internal static bool CheckingConvertToFloat(object value) {
2863-
return value is double || (value != null && value is Extensible<double>);
2866+
return value is double || value is Extensible<double>;
28642867
}
28652868

28662869
internal static bool CheckingConvertToComplex(object value) {
2867-
return value is Complex || value is Extensible<Complex> || CheckingConvertToInt(value) || CheckingConvertToFloat(value);
2870+
return value is Complex || value is Extensible<Complex>;
28682871
}
28692872

28702873
internal static bool CheckingConvertToString(object value) {
@@ -2880,11 +2883,6 @@ public static bool CheckingConvertToBool(object value) {
28802883
return value;
28812884
}
28822885

2883-
public static object? NonThrowingConvertToLong(object value) {
2884-
if (!CheckingConvertToInt(value)) return null;
2885-
return value;
2886-
}
2887-
28882886
public static object? NonThrowingConvertToFloat(object value) {
28892887
if (!CheckingConvertToFloat(value)) return null;
28902888
return value;
@@ -2920,11 +2918,6 @@ public static object ThrowingConvertToComplex(object value) {
29202918
return value;
29212919
}
29222920

2923-
public static object ThrowingConvertToLong(object value) {
2924-
if (!CheckingConvertToLong(value)) throw TypeError(" __int__ returned non-int (type {0})", PythonOps.GetPythonTypeName(value));
2925-
return value;
2926-
}
2927-
29282921
public static object ThrowingConvertToString(object value) {
29292922
if (!CheckingConvertToString(value)) throw TypeError(" __str__ returned non-str (type {0})", PythonOps.GetPythonTypeName(value));
29302923
return value;

Src/IronPython/Runtime/PythonList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ public void sort(CodeContext/*!*/ context,
866866
key = arg.Value;
867867
break;
868868
case "reverse":
869-
if (!PythonOps.CheckingConvertToBool(arg.Value) && !PythonOps.CheckingConvertToInt(arg.Value)) {
869+
if (!PythonOps.CheckingConvertToBool(arg.Value) && !PythonOps.CheckingConvertToInt(arg.Value)) { // Python 3.8: PythonOps.TryToIndex
870870
throw PythonOps.TypeErrorForTypeMismatch("integer", arg.Value);
871871
}
872872
reverse = Convert.ToBoolean(arg.Value);

Src/IronPythonTest/EngineTest.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,9 @@ def __setitem__(self, index, value):
10881088
10891089
SomeDelegate = somecallable
10901090
1091+
class ns_bigint(ns):
1092+
def __int__(self): return (42).ToBigInteger()
1093+
10911094
class ns_getattr(object):
10921095
ClassVal = 'ClassVal'
10931096
@@ -1192,10 +1195,14 @@ def __setitem__(self, index, value):
11921195
11931196
SomeDelegate = somecallable
11941197
1198+
class os_bigint(os):
1199+
def __int__(self): return (42).ToBigInteger()
1200+
11951201
class plain_os:
11961202
pass
11971203
1198-
class plain_ns(object): pass
1204+
class plain_ns(object):
1205+
pass
11991206
12001207
class os_getattr:
12011208
ClassVal = 'ClassVal'
@@ -1242,12 +1249,14 @@ def Invokable(*args, **kwargs):
12421249
if not clr.IsNetCoreApp and not clr.IsMono:
12431250
controlinst = control()
12441251
nsinst = ns()
1252+
nsinstbig = ns_bigint()
12451253
iterable = IterableObject()
12461254
iterableos = IterableObjectOs()
12471255
plainnsinst = plain_ns()
12481256
nsmethod = nsinst.NsMethod
12491257
alinst = MyArrayList()
12501258
osinst = os()
1259+
osinstbig = os_bigint()
12511260
plainosinst = plain_os()
12521261
os_getattrinst = os_getattr()
12531262
ns_getattrinst = ns_getattr()
@@ -1269,7 +1278,7 @@ def Invokable(*args, **kwargs):
12691278
var indexableObjects = new object[] { scope.GetVariable("nsinst"), scope.GetVariable("osinst") };
12701279
var unindexableObjects = new object[] { scope.GetVariable("TestFunc"), scope.GetVariable("ns_getattrinst"), scope.GetVariable("somecallable") }; // scope.GetVariable("plainosinst"),
12711280
var invokableObjects = new object[] { scope.GetVariable("Invokable"), scope.GetVariable("nsinst"), scope.GetVariable("osinst"), scope.GetVariable("nsmethod"), };
1272-
var convertableObjects = new object[] { scope.GetVariable("nsinst"), scope.GetVariable("osinst") };
1281+
var convertableObjects = new object[] { scope.GetVariable("nsinst"), scope.GetVariable("osinst"), scope.GetVariable("nsinstbig"), scope.GetVariable("osinstbig") };
12731282
var unconvertableObjects = new object[] { scope.GetVariable("plainnsinst"), scope.GetVariable("plainosinst") };
12741283
var iterableObjects = new object[] { scope.GetVariable("iterable"), scope.GetVariable("iterableos") };
12751284

@@ -1377,7 +1386,7 @@ def Invokable(*args, **kwargs):
13771386
var ssite = CallSite<Func<CallSite, object, string>>.Create(new MyConvertBinder(typeof(string)));
13781387
Assert.AreEqual(ssite.Target(ssite, inst), "Python");
13791388

1380-
// this call site works only if __int__ happens to return an Int32 instance
1389+
// this call site works only if __int__ happens to return an Int32 instance or a BigInteger instance that fits in 32 bits
13811390
var isite = CallSite<Func<CallSite, object, int>>.Create(new MyConvertBinder(typeof(int), 23));
13821391
Assert.AreEqual(isite.Target(isite, inst), 42);
13831392

0 commit comments

Comments
 (0)