Skip to content
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
Next Next commit
Add conversions for int->float and bool->(int,float)
  • Loading branch information
filmor committed Aug 11, 2022
commit 881b2b24e6a8e5ace4b99b78dbf569b2303e7a88
20 changes: 20 additions & 0 deletions src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ static NewReference DoConvert(BorrowedReference ob)
return python.NewReferenceOrNull();
Comment thread
filmor marked this conversation as resolved.
}

static NewReference DoConvertBooleanInt(BorrowedReference ob)
{
var self = (CLRObject)GetManagedObject(ob)!;
using var python = ((bool)self.inst ? 1 : 0).ToPython();
Comment thread
filmor marked this conversation as resolved.
Outdated
return python.NewReferenceOrNull();
}

static NewReference DoConvertIntFloat(BorrowedReference ob)
{
var self = (CLRObject)GetManagedObject(ob)!;
using var python = (Convert.ToDouble(self.inst)).ToPython();
Comment thread
filmor marked this conversation as resolved.
Outdated
return python.NewReferenceOrNull();
}

static IEnumerable<MethodInfo> GetCallImplementations(Type type)
=> type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.Name == "__call__");
Expand Down Expand Up @@ -574,6 +588,11 @@ 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);
break;

case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
Expand All @@ -583,6 +602,7 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH
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);
break;
case TypeCode.Double:
case TypeCode.Single:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,16 @@ def test_intptr_construction():
UIntPtr(v)

def test_explicit_conversion():
from System import Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte
from System import (
Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean
)
from System import Double, Single

assert int(Boolean(True)) == 1

for t in [Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte]:
assert int(t(127)) == 127
assert float(t(127)) == 127.0
Comment thread
filmor marked this conversation as resolved.

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