Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit52cf03c

Browse files
authored
Merge pull request#1908 from filmor/implicit-float-int
Implicit float conversion in function calls
2 parentsf1ef11d +2194d6c commit52cf03c

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
gototype_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
internalstaticNewReferencePyInt_FromInt64(longvalue)=>PyLong_FromLongLong(value);
11031103

1104-
internalstaticboolPyLong_Check(BorrowedReferenceob)
1105-
{
1106-
returnPyObject_TYPE(ob)==PyLongType;
1107-
}
1108-
11091104
internalstaticNewReferencePyLong_FromLongLong(longvalue)=>Delegates.PyLong_FromLongLong(value);
11101105

11111106

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

11471142
internalstaticboolPyFloat_Check(BorrowedReferenceob)
1148-
{
1149-
returnPyObject_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
deftest_method_with_pointer_array_argument():
12571257
withpytest.raises(TypeError):
12581258
MethodTest.PointerArray([0])
1259+
1260+
deftest_method_call_implicit_conversion():
1261+
1262+
classIntAnswerMixin:
1263+
# For Python >= 3.8
1264+
def__index__(self):
1265+
return42
1266+
1267+
# For Python < 3.10
1268+
def__int__(self):
1269+
return42
1270+
1271+
classAnswer(int,IntAnswerMixin):
1272+
pass
1273+
1274+
classFloatAnswer(float,IntAnswerMixin):
1275+
def__float__(self):
1276+
return42.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+
forvin [Answer(),FloatAnswer()]:
1282+
fortin [System.Double,System.Single]:
1283+
min_value=t(t.MinValue)
1284+
compare_to=min_value.CompareTo.__overloads__[t]
1285+
1286+
assertcompare_to(v)==-1
1287+
1288+
classSomeNonFloat:
1289+
def__float__(self):
1290+
return42.0
1291+
1292+
fortin [System.Double,System.Single]:
1293+
withpytest.raises(TypeError):
1294+
min_value=t(t.MinValue)
1295+
compare_to=min_value.CompareTo.__overloads__[t]
1296+
assertcompare_to(SomeNonFloat())==-1

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp