Skip to content

Commit 52cf03c

Browse files
authored
Merge pull request #1908 from filmor/implicit-float-int
Implicit float conversion in function calls
2 parents f1ef11d + 2194d6c commit 52cf03c

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/runtime/Converter.cs

Lines changed: 1 addition & 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
}

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.

tests/test_method.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,3 +1256,41 @@ def test_method_encoding():
12561256
def test_method_with_pointer_array_argument():
12571257
with pytest.raises(TypeError):
12581258
MethodTest.PointerArray([0])
1259+
1260+
def test_method_call_implicit_conversion():
1261+
1262+
class IntAnswerMixin:
1263+
# For Python >= 3.8
1264+
def __index__(self):
1265+
return 42
1266+
1267+
# For Python < 3.10
1268+
def __int__(self):
1269+
return 42
1270+
1271+
class Answer(int, IntAnswerMixin):
1272+
pass
1273+
1274+
class FloatAnswer(float, IntAnswerMixin):
1275+
def __float__(self):
1276+
return 42.0
1277+
1278+
# TODO: This should also work for integer types but due to some complexities
1279+
# in the C-API functions (some call __int__/__index__, some don't), it's not
1280+
# supported, yet.
1281+
for v in [Answer(), FloatAnswer()]:
1282+
for t in [System.Double, System.Single]:
1283+
min_value = t(t.MinValue)
1284+
compare_to = min_value.CompareTo.__overloads__[t]
1285+
1286+
assert compare_to(v) == -1
1287+
1288+
class SomeNonFloat:
1289+
def __float__(self):
1290+
return 42.0
1291+
1292+
for t in [System.Double, System.Single]:
1293+
with pytest.raises(TypeError):
1294+
min_value = t(t.MinValue)
1295+
compare_to = min_value.CompareTo.__overloads__[t]
1296+
assert compare_to(SomeNonFloat()) == -1

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy