Skip to content

Explicit float and int conversion for builtin types #1904

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

Merged
merged 3 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Use direct conversion functions and add more tests
  • Loading branch information
filmor committed Aug 11, 2022
commit 3cbdfdc6856783d23bc096409bf00f25aaa7bdee
39 changes: 24 additions & 15 deletions src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,28 @@ static NewReference DoConvert(BorrowedReference ob)
return python.NewReferenceOrNull();
}

static NewReference DoConvertInt(BorrowedReference ob)
{
var self = (CLRObject)GetManagedObject(ob)!;
return Runtime.PyLong_FromLongLong(Convert.ToInt64(self.inst));
}

static NewReference DoConvertUInt(BorrowedReference ob)
{
var self = (CLRObject)GetManagedObject(ob)!;
return Runtime.PyLong_FromUnsignedLongLong(Convert.ToUInt64(self.inst));
}

static NewReference DoConvertBooleanInt(BorrowedReference ob)
{
var self = (CLRObject)GetManagedObject(ob)!;
using var python = ((bool)self.inst ? 1 : 0).ToPython();
return python.NewReferenceOrNull();
return Runtime.PyInt_FromInt32((bool)self.inst ? 1 : 0);
}

static NewReference DoConvertIntFloat(BorrowedReference ob)
static NewReference DoConvertFloat(BorrowedReference ob)
{
var self = (CLRObject)GetManagedObject(ob)!;
using var python = (Convert.ToDouble(self.inst)).ToPython();
return python.NewReferenceOrNull();
return Runtime.PyFloat_FromDouble(Convert.ToDouble(self.inst));
}

static IEnumerable<MethodInfo> GetCallImplementations(Type type)
Expand Down Expand Up @@ -589,24 +599,23 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH
switch (Type.GetTypeCode(type.Value))
{
case TypeCode.Boolean:
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertBooleanInt), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertIntFloat), slotsHolder);
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertInt), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
break;

case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvert), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertIntFloat), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertUInt), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
break;
case TypeCode.Double:
case TypeCode.Single:
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvert), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
break;
}
}
Expand Down
13 changes: 12 additions & 1 deletion tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,11 +726,22 @@ def test_explicit_conversion():
)
from System import Double, Single

assert int(Boolean(False)) == 0
assert int(Boolean(True)) == 1

for t in [Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte]:
for t in [UInt64, UInt32, UInt16, Byte]:
assert int(t(127)) == 127
assert float(t(127)) == 127.0

for t in [Int64, Int32, Int16, SByte]:
assert int(t(127)) == 127
assert int(t(-127)) == -127
assert float(t(127)) == 127.0
assert float(t(-127)) == -127.0

assert int(Int64.MaxValue) == 2**63 - 1
assert int(Int64.MinValue) == -2**63
assert int(UInt64.MaxValue) == 2**64 - 1

for t in [Single, Double]:
assert float(t(0.125)) == 0.125