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

Rename ConvertToLong to ConvertToInt #1403

Merged
merged 5 commits into from
Apr 18, 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
3 changes: 1 addition & 2 deletions Src/IronPython.Modules/_csv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,7 @@ public void writerow(CodeContext/*!*/ context, object sequence) {
object field = e.Current;
var quoted = _dialect.quoting switch {
QUOTE_NONNUMERIC => !(PythonOps.CheckingConvertToFloat(field) ||
PythonOps.CheckingConvertToInt(field) ||
PythonOps.CheckingConvertToLong(field)),
PythonOps.CheckingConvertToInt(field)),
QUOTE_ALL => true,
_ => false,
};
Expand Down
6 changes: 4 additions & 2 deletions Src/IronPython/Runtime/Binding/MetaUserObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private DynamicMetaObject TryPythonConversion(DynamicMetaObjectBinder conversion
(x) => x);
} else if (type == typeof(BigInteger)) {
if (!typeof(Extensible<BigInteger>).IsAssignableFrom(LimitType)) {
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToLong",
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToInt",
() => FallbackConvert(conversion),
(x) => Ast.Call(null, typeof(PythonOps).GetMethod(nameof(PythonOps.ConvertIntToBigInt)), x)); // GH #52
}
Expand All @@ -192,7 +192,9 @@ private DynamicMetaObject TryPythonConversion(DynamicMetaObjectBinder conversion
}
break;
case TypeCode.Int32:
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToInt");
return MakeConvertRuleForCall(conversion, type, this, "__int__", "ConvertToInt",
() => FallbackConvert(conversion),
(x) => Ast.Call(null, typeof(PythonOps).GetMethod(nameof(PythonOps.ConvertIntToInt32)), x)); // GH #52
case TypeCode.Double:
return MakeConvertRuleForCall(conversion, type, this, "__float__", "ConvertToFloat");
case TypeCode.Boolean:
Expand Down
49 changes: 21 additions & 28 deletions Src/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2816,55 +2816,58 @@ public static object ConvertToPythonPrimitive(object value) {
return value switch
{
float f => (double)f,
double d => d,
sbyte sb => (int)sb,
byte b => (int)b,
char c => (int)c,
short s => (int)s,
ushort us => (int)us,
int i => i,
uint ui => (BigInteger)ui,
long l => (BigInteger)l,
ulong ul => (BigInteger)ul,
BigInteger bi => bi,
bool b => b,
// no conversion needed for: double, int, BigInteger, bool
_ => value,
};
}

public static object? ConvertFloatToComplex(object value) {
if (value == null) {
return null;
}

if (value is double d) return new Complex(d, 0.0);
if (value is Extensible<double> ed) return new Complex(ed.Value, 0.0);
throw new InvalidOperationException();
return value switch {
null => null,
double d => new Complex(d, 0.0),
Extensible<double> ed => new Complex(ed.Value, 0.0),
_ => throw new InvalidOperationException(),
};
}

public static object? ConvertIntToBigInt(object? value) {
return value switch {
null => null,
int i => new BigInteger(i),
BigInteger bi => bi,
BigInteger => value,
Extensible<BigInteger> ebi => ebi.Value,
_ => throw new InvalidOperationException(),
};
}

internal static bool CheckingConvertToInt(object value) {
return value is int || value is BigInteger || value is Extensible<BigInteger>;
public static object? ConvertIntToInt32(object? value) {
return value switch {
null => null,
int => value,
BigInteger bi => (int)bi,
Extensible<BigInteger> ebi => (int)ebi.Value,
_ => throw new InvalidOperationException(),
};
}

internal static bool CheckingConvertToLong(object value) {
return CheckingConvertToInt(value);
internal static bool CheckingConvertToInt(object value) {
return value is int || value is BigInteger || value is Extensible<BigInteger>;
}

internal static bool CheckingConvertToFloat(object value) {
return value is double || (value != null && value is Extensible<double>);
return value is double || value is Extensible<double>;
}

internal static bool CheckingConvertToComplex(object value) {
return value is Complex || value is Extensible<Complex> || CheckingConvertToInt(value) || CheckingConvertToFloat(value);
return value is Complex || value is Extensible<Complex>;
}

internal static bool CheckingConvertToString(object value) {
Expand All @@ -2880,11 +2883,6 @@ public static bool CheckingConvertToBool(object value) {
return value;
}

public static object? NonThrowingConvertToLong(object value) {
if (!CheckingConvertToInt(value)) return null;
return value;
}

public static object? NonThrowingConvertToFloat(object value) {
if (!CheckingConvertToFloat(value)) return null;
return value;
Expand Down Expand Up @@ -2920,11 +2918,6 @@ public static object ThrowingConvertToComplex(object value) {
return value;
}

public static object ThrowingConvertToLong(object value) {
if (!CheckingConvertToLong(value)) throw TypeError(" __int__ returned non-int (type {0})", PythonOps.GetPythonTypeName(value));
return value;
}

public static object ThrowingConvertToString(object value) {
if (!CheckingConvertToString(value)) throw TypeError(" __str__ returned non-str (type {0})", PythonOps.GetPythonTypeName(value));
return value;
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/PythonList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ public void sort(CodeContext/*!*/ context,
key = arg.Value;
break;
case "reverse":
if (!PythonOps.CheckingConvertToBool(arg.Value) && !PythonOps.CheckingConvertToInt(arg.Value)) {
if (!PythonOps.CheckingConvertToBool(arg.Value) && !PythonOps.CheckingConvertToInt(arg.Value)) { // Python 3.8: PythonOps.TryToIndex
throw PythonOps.TypeErrorForTypeMismatch("integer", arg.Value);
}
reverse = Convert.ToBoolean(arg.Value);
Expand Down
15 changes: 12 additions & 3 deletions Src/IronPythonTest/EngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,9 @@ def __setitem__(self, index, value):

SomeDelegate = somecallable

class ns_bigint(ns):
def __int__(self): return (42).ToBigInteger()

class ns_getattr(object):
ClassVal = 'ClassVal'

Expand Down Expand Up @@ -1192,10 +1195,14 @@ def __setitem__(self, index, value):

SomeDelegate = somecallable

class os_bigint(os):
def __int__(self): return (42).ToBigInteger()

class plain_os:
pass

class plain_ns(object): pass
class plain_ns(object):
pass

class os_getattr:
ClassVal = 'ClassVal'
Expand Down Expand Up @@ -1242,12 +1249,14 @@ def Invokable(*args, **kwargs):
if not clr.IsNetCoreApp and not clr.IsMono:
controlinst = control()
nsinst = ns()
nsinstbig = ns_bigint()
iterable = IterableObject()
iterableos = IterableObjectOs()
plainnsinst = plain_ns()
nsmethod = nsinst.NsMethod
alinst = MyArrayList()
osinst = os()
osinstbig = os_bigint()
plainosinst = plain_os()
os_getattrinst = os_getattr()
ns_getattrinst = ns_getattr()
Expand All @@ -1269,7 +1278,7 @@ def Invokable(*args, **kwargs):
var indexableObjects = new object[] { scope.GetVariable("nsinst"), scope.GetVariable("osinst") };
var unindexableObjects = new object[] { scope.GetVariable("TestFunc"), scope.GetVariable("ns_getattrinst"), scope.GetVariable("somecallable") }; // scope.GetVariable("plainosinst"),
var invokableObjects = new object[] { scope.GetVariable("Invokable"), scope.GetVariable("nsinst"), scope.GetVariable("osinst"), scope.GetVariable("nsmethod"), };
var convertableObjects = new object[] { scope.GetVariable("nsinst"), scope.GetVariable("osinst") };
var convertableObjects = new object[] { scope.GetVariable("nsinst"), scope.GetVariable("osinst"), scope.GetVariable("nsinstbig"), scope.GetVariable("osinstbig") };
var unconvertableObjects = new object[] { scope.GetVariable("plainnsinst"), scope.GetVariable("plainosinst") };
var iterableObjects = new object[] { scope.GetVariable("iterable"), scope.GetVariable("iterableos") };

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

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

Expand Down