Skip to content

Commit 5db80c3

Browse files
committed
Fix PyFloat_Check to check for subclasses, drop PyLong_Check
1 parent c4325dd commit 5db80c3

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/runtime/Converter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
710710
{
711711
if (Runtime.Is32Bit)
712712
{
713-
if (!Runtime.PyLong_Check(value))
713+
if (!Runtime.PyInt_Check(value))
714714
{
715715
goto type_error;
716716
}
@@ -777,6 +777,10 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
777777

778778
case TypeCode.Single:
779779
{
780+
if (!Runtime.PyFloat_Check(value) && !Runtime.PyInt_Check(value))
781+
{
782+
goto type_error;
783+
}
780784
double num = Runtime.PyFloat_AsDouble(value);
781785
if (Exceptions.ErrorOccurred())
782786
{
@@ -795,6 +799,10 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
795799

796800
case TypeCode.Double:
797801
{
802+
if (!Runtime.PyFloat_Check(value) && !Runtime.PyInt_Check(value))
803+
{
804+
goto type_error;
805+
}
798806
double num = Runtime.PyFloat_AsDouble(value);
799807
if (Exceptions.ErrorOccurred())
800808
{

src/runtime/Runtime.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,11 +1101,6 @@ internal static bool PyBool_Check(BorrowedReference ob)
11011101

11021102
internal static NewReference PyInt_FromInt64(long value) => PyLong_FromLongLong(value);
11031103

1104-
internal static bool PyLong_Check(BorrowedReference ob)
1105-
{
1106-
return PyObject_TYPE(ob) == PyLongType;
1107-
}
1108-
11091104
internal static NewReference PyLong_FromLongLong(long value) => Delegates.PyLong_FromLongLong(value);
11101105

11111106

@@ -1145,9 +1140,7 @@ internal static NewReference PyLong_FromString(string value, int radix)
11451140
}
11461141

11471142
internal static bool PyFloat_Check(BorrowedReference ob)
1148-
{
1149-
return PyObject_TYPE(ob) == PyFloatType;
1150-
}
1143+
=> PyObject_TypeCheck(ob, PyFloatType);
11511144

11521145
/// <summary>
11531146
/// Return value: New reference.

0 commit comments

Comments
 (0)