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
Next Next commit
Explicit float and int conversion for builtin types
  • Loading branch information
filmor committed Aug 10, 2022
commit bf1ca889f84bddea770a2495617cce681aee3357
1 change: 1 addition & 0 deletions src/runtime/Native/ITypeOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface ITypeOffsets
int nb_true_divide { get; }
int nb_and { get; }
int nb_int { get; }
int nb_float { get; }
int nb_or { get; }
int nb_xor { get; }
int nb_lshift { get; }
Expand Down
1 change: 1 addition & 0 deletions src/runtime/Native/TypeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static partial class TypeOffset
internal static int nb_or { get; private set; }
internal static int nb_xor { get; private set; }
internal static int nb_int { get; private set; }
internal static int nb_float { get; private set; }
internal static int nb_lshift { get; private set; }
internal static int nb_rshift { get; private set; }
internal static int nb_remainder { get; private set; }
Expand Down
25 changes: 25 additions & 0 deletions src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,13 @@ static NewReference tp_call_impl(BorrowedReference ob, BorrowedReference args, B
return callBinder.Invoke(ob, args, kw);
}

static NewReference DoConvert(BorrowedReference ob)
{
var self = (CLRObject)GetManagedObject(ob)!;
using var python = self.inst.ToPython();
return python.NewReferenceOrNull();
}

static IEnumerable<MethodInfo> GetCallImplementations(Type type)
=> type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.Name == "__call__");
Expand Down Expand Up @@ -564,6 +571,24 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH
{
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.mp_length, new Interop.B_P(MpLengthSlot.impl), slotsHolder);
}

switch (Type.GetTypeCode(type.Value))
{
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);
break;
case TypeCode.Double:
case TypeCode.Single:
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvert), slotsHolder);
break;
}
}

public virtual bool HasCustomNew() => this.GetType().GetMethod("tp_new") is not null;
Expand Down
1 change: 1 addition & 0 deletions src/runtime/Types/OperatorMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static OperatorMethod()
["op_UnaryPlus"] = new SlotDefinition("__pos__", TypeOffset.nb_positive),

["__int__"] = new SlotDefinition("__int__", TypeOffset.nb_int),
["__float__"] = new SlotDefinition("__float__", TypeOffset.nb_float),
};
ComparisonOpMap = new Dictionary<string, string>
{
Expand Down
9 changes: 9 additions & 0 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,12 @@ def test_intptr_construction():
with pytest.raises(OverflowError):
UIntPtr(v)

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

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

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