Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/runtime/Native/ITypeOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface ITypeOffsets
int nb_invert { get; }
int nb_inplace_add { get; }
int nb_inplace_subtract { get; }
int nb_index { get; }
int ob_size { get; }
int ob_type { get; }
int qualname { 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 @@ -38,6 +38,7 @@ static partial class TypeOffset
internal static int nb_invert { get; private set; }
internal static int nb_inplace_add { get; private set; }
internal static int nb_inplace_subtract { get; private set; }
internal static int nb_index { get; private set; }
internal static int ob_size { get; private set; }
internal static int ob_type { get; private set; }
internal static int qualname { get; private set; }
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,17 +604,20 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH
case TypeCode.Int32:
case TypeCode.Int64:
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertInt), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_index, new Interop.B_N(DoConvertInt), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
break;
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertUInt), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_index, 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_int, new Interop.B_N(DoConvertInt), slotsHolder);
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
break;
}
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 @@ -54,6 +54,7 @@ static OperatorMethod()

["__int__"] = new SlotDefinition("__int__", TypeOffset.nb_int),
["__float__"] = new SlotDefinition("__float__", TypeOffset.nb_float),
["__index__"] = new SlotDefinition("__float__", TypeOffset.nb_index),
Comment thread
filmor marked this conversation as resolved.
Outdated
};
ComparisonOpMap = new Dictionary<string, string>
{
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Util/OpsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal static class EnumOps<T> where T : Enum
{
[ForbidPythonThreads]
#pragma warning disable IDE1006 // Naming Styles - must match Python
public static PyInt __int__(T value)
public static PyInt __index__(T value)
Comment thread
filmor marked this conversation as resolved.
#pragma warning restore IDE1006 // Naming Styles
=> typeof(T).GetEnumUnderlyingType() == typeof(UInt64)
? new PyInt(Convert.ToUInt64(value))
Expand Down
13 changes: 10 additions & 3 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ def test_intptr_construction():
UIntPtr(v)

def test_explicit_conversion():
from operator import index
from System import (
Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean
)
Expand All @@ -730,18 +731,24 @@ def test_explicit_conversion():
assert int(Boolean(True)) == 1

for t in [UInt64, UInt32, UInt16, Byte]:
assert index(t(127)) == 127
assert int(t(127)) == 127
assert float(t(127)) == 127.0

for t in [Int64, Int32, Int16, SByte]:
assert index(t(127)) == 127
assert index(t(-127)) == -127
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
assert int(Int64(Int64.MaxValue)) == 2**63 - 1
assert int(Int64(Int64.MinValue)) == -2**63
assert int(UInt64(UInt64.MaxValue)) == 2**64 - 1

for t in [Single, Double]:
assert float(t(0.125)) == 0.125
assert int(t(123.4)) == 123
with pytest.raises(TypeError):
index(t(123.4))