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

Implicit float conversion in function calls#1908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
filmor merged 2 commits intopythonnet:releasefromfilmor:implicit-float-int
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
2 changes: 1 addition & 1 deletionsrc/runtime/Converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -710,7 +710,7 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
{
if (Runtime.Is32Bit)
{
if (!Runtime.PyLong_Check(value))
if (!Runtime.PyInt_Check(value))
{
goto type_error;
}
Expand Down
9 changes: 1 addition & 8 deletionssrc/runtime/Runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1101,11 +1101,6 @@ internal static bool PyBool_Check(BorrowedReference ob)

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

internal static bool PyLong_Check(BorrowedReference ob)
{
return PyObject_TYPE(ob) == PyLongType;
}

internal static NewReference PyLong_FromLongLong(long value) => Delegates.PyLong_FromLongLong(value);


Expand DownExpand Up@@ -1145,9 +1140,7 @@ internal static NewReference PyLong_FromString(string value, int radix)
}

internal static bool PyFloat_Check(BorrowedReference ob)
{
return PyObject_TYPE(ob) == PyFloatType;
}
=> PyObject_TypeCheck(ob, PyFloatType);

/// <summary>
/// Return value: New reference.
Expand Down
38 changes: 38 additions & 0 deletionstests/test_method.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1256,3 +1256,41 @@ def test_method_encoding():
def test_method_with_pointer_array_argument():
with pytest.raises(TypeError):
MethodTest.PointerArray([0])

def test_method_call_implicit_conversion():

class IntAnswerMixin:
# For Python >= 3.8
def __index__(self):
return 42

# For Python < 3.10
def __int__(self):
return 42

class Answer(int, IntAnswerMixin):
pass

class FloatAnswer(float, IntAnswerMixin):
def __float__(self):
return 42.0

# TODO: This should also work for integer types but due to some complexities
# in the C-API functions (some call __int__/__index__, some don't), it's not
# supported, yet.
for v in [Answer(), FloatAnswer()]:
for t in [System.Double, System.Single]:
min_value = t(t.MinValue)
compare_to = min_value.CompareTo.__overloads__[t]

assert compare_to(v) == -1

class SomeNonFloat:
def __float__(self):
return 42.0

for t in [System.Double, System.Single]:
with pytest.raises(TypeError):
min_value = t(t.MinValue)
compare_to = min_value.CompareTo.__overloads__[t]
assert compare_to(SomeNonFloat()) == -1

[8]ページ先頭

©2009-2025 Movatter.jp