8000 Fill nb_index slot for integer types (#1907) · pythonnet/pythonnet@bd48dc1 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd48dc1

Browse files
authored
Fill nb_index slot for integer types (#1907)
1 parent 0909021 commit bd48dc1

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

src/runtime/Native/ITypeOffsets.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface ITypeOffsets
3131
int nb_invert { get; }
3232
int nb_inplace_add { get; }
3333
int nb_inplace_subtract { get; }
34+
int nb_index { get; }
3435
int ob_size { get; }
3536
int ob_type { get; }
3637
int qualname { get; }

src/runtime/Native/TypeOffset.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static partial class TypeOffset
3838
internal static int nb_invert { get; private set; }
3939
internal static int nb_inplace_add { get; private set; }
4040
internal static int nb_inplace_subtract { get; private set; }
41+
internal static int nb_index { get; private set; }
4142
internal static int ob_size { get; private set; }
4243
internal static int ob_type { get; private set; }
4344
internal static int qualname { get; private set; }

src/runtime/Types/ClassBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,17 +604,20 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH
604604
case TypeCode.Int32:
605605
case TypeCode.Int64:
606606
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertInt), slotsHolder);
607+
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_index, new Interop.B_N(DoConvertInt), slotsHolder);
607608
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
608609
break;
609610
case TypeCode.Byte:
610611
case TypeCode.UInt16:
611612
case TypeCode.UInt32:
612613
case TypeCode.UInt64:
613614
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertUInt), slotsHolder);
615+
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_index, new Interop.B_N(DoConvertUInt), slotsHolder);
614616
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
615617
break;
616618
case TypeCode.Double:
617619
case TypeCode.Single:
620+
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertInt), slotsHolder);
618621
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder);
619622
break;
620623
}

src/runtime/Types/OperatorMethod.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static OperatorMethod()
5454

5555
["__int__"] = new SlotDefinition("__int__", TypeOffset.nb_int),
5656
["__float__"] = new SlotDefinition("__float__", TypeOffset.nb_float),
57+
["__index__"] = new SlotDefinition("__index__", TypeOffset.nb_index),
5758
};
5859
ComparisonOpMap = new Dictionary<string, string>
5960
{

src/runtime/Util/OpsHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ internal static class EnumOps<T> where T : Enum
8282
{
8383
[ForbidPythonThreads]
8484
#pragma warning disable IDE1006 // Naming Styles - must match Python
85-
public static PyInt __int__(T value)
85+
public static PyInt __index__(T value)
8686
#pragma warning restore IDE1006 // Naming Styles
8787
=> typeof(T).GetEnumUnderlyingType() == typeof(UInt64)
8888
? new PyInt(Convert.ToUInt64(value))
8989
: new PyInt(Convert.ToInt64(value));
90+
[ForbidPythonThreads]
91+
#pragma warning disable IDE1006 // Naming Styles - must match Python
92+
public static PyInt __int__(T value) => __index__(value);
93+
#pragma warning restore IDE1006 // Naming Styles
9094
}
9195
}

tests/test_conversion.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ def test_intptr_construction():
721721
UIntPtr(v)
722722

723723
def test_explicit_conversion():
724+
from operator import index
724725
from System import (
725726
Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean
726727
)
@@ -730,18 +731,24 @@ def test_explicit_conversion():
730731
assert int(Boolean(True)) == 1
731732

732733
for t in [UInt64, UInt32, UInt16, Byte]:
734+
assert index(t(127)) == 127
733735
assert int(t(127)) == 127
734736
assert float(t(127)) == 127.0
735737

736738
for t in [Int64, Int32, Int16, SByte]:
739+
assert index(t(127)) == 127
740+
assert index(t(-127)) == -127
737741
assert int(t(127)) == 127
738742
assert int(t(-127)) == -127
739743
assert float(t(127)) == 127.0
740744
assert float(t(-127)) == -127.0
741745

742-
assert int(Int64.MaxValue) == 2**63 - 1
743-
assert int(Int64.MinValue) == -2**63
744-
assert int(UInt64.MaxValue) == 2**64 - 1
746+
assert int(Int64(Int64.MaxValue)) == 2**63 - 1
747+
assert int(Int64(Int64.MinValue)) == -2**63
748+
assert int(UInt64(UInt64.MaxValue)) == 2**64 - 1
745749

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

0 commit comments

Comments
 (0)
0