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

Fix #1523#1524

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

Closed
slide wants to merge3 commits intopythonnet:masterfromslide:int_parameter_method_resolution
Closed
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
16 changes: 13 additions & 3 deletionssrc/runtime/converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,8 @@ private Converter()
private static Type int64Type;
private static Type boolType;
private static Type typeType;
private static Type uint64Type;
private static Type uint32Type;

static Converter()
{
Expand All@@ -43,26 +45,34 @@ static Converter()
decimalType = typeof(Decimal);
boolType = typeof(Boolean);
typeType = typeof(Type);
uint64Type = typeof(UInt64);
uint32Type = typeof(UInt32);
}


/// <summary>
/// Given a builtin Python type, return the corresponding CLR type.
/// </summary>
internal static Type GetTypeByAlias(IntPtr op)
internal static Type GetTypeByAlias(IntPtr op, Type preferred=null)
{
if (op == Runtime.PyStringType)
return stringType;

if (op == Runtime.PyUnicodeType)
return stringType;

if (op == Runtime.PyIntType)
if (op == Runtime.PyIntType && (preferred == null || preferred == int32Type))
return int32Type;

if (op == Runtime.PyLongType)
if (op == Runtime.PyIntType && (preferred == null || preferred == uint32Type))
return uint32Type;

if (op == Runtime.PyLongType && (preferred == null || preferred == int64Type))
return int64Type;

if (op == Runtime.PyLongType && (preferred == null || preferred == uint64Type))
return uint64Type;

if (op == Runtime.PyFloatType)
return doubleType;

Expand Down
11 changes: 10 additions & 1 deletionsrc/runtime/methodbinder.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -330,13 +330,15 @@ public MatchedMethod(int kwargsMatched, int defaultsNeeded, object[] margs, int
ManagedArgs = margs;
Outs = outs;
Method = mb;
HasPyObjectParameter = mb.GetParameters().Any(x => x.ParameterType == typeof(PyObject));
}

public int KwargsMatched { get; }
public int DefaultsNeeded { get; }
public object[] ManagedArgs { get; }
public int Outs { get; }
public MethodBase Method { get; }
public bool HasPyObjectParameter { get; }
}

private readonly struct MismatchedMethod
Expand DownExpand Up@@ -470,8 +472,15 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
var matchedMethod = new MatchedMethod(kwargsMatched, defaultsNeeded, margs, outs, mi);
argMatchedMethods.Add(matchedMethod);
}

if (argMatchedMethods.Count > 0)
{
// order the matched methods such that PyObject overrides are last, we only care about
// this when there is more than one arg matched method
if (argMatchedMethods.Count > 1)
{
argMatchedMethods = argMatchedMethods.OrderBy(x => x.HasPyObjectParameter).ToList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What is the scenario? Why is this change needed?

}
var bestKwargMatchCount = argMatchedMethods.Max(x => x.KwargsMatched);
var fewestDefaultsRequired = argMatchedMethods.Where(x => x.KwargsMatched == bestKwargMatchCount).Min(x => x.DefaultsNeeded);

Expand DownExpand Up@@ -725,7 +734,7 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool
pyoptype = Runtime.PyObject_Type(argument);
if (pyoptype != IntPtr.Zero)
{
clrtype = Converter.GetTypeByAlias(pyoptype);
clrtype = Converter.GetTypeByAlias(pyoptype, parameterType);
}
Runtime.XDecref(pyoptype);
}
Expand Down
17 changes: 15 additions & 2 deletionssrc/testing/conversiontest.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -42,7 +42,7 @@ public ConversionTest()

}



public interface ISpam
{
Expand All@@ -63,7 +63,7 @@ public string GetValue()
return value;
}
}

public class UnicodeString
{
public string value = "안녕";
Expand All@@ -78,4 +78,17 @@ public override string ToString()
return value;
}
}

public class MethodResolutionInt
{
public IEnumerable<byte> MethodA(ulong address, int size)
{
return new byte[10];
}

public int MethodA(string dummy, ulong address, int size)
{
return 0;
}
}
}
14 changes: 13 additions & 1 deletiontests/test_conversion.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,7 @@
import pytest

import System
from Python.Test import ConversionTest, UnicodeString
from Python.Test import ConversionTest, UnicodeString, MethodResolutionInt
from Python.Runtime import PyObjectConversions
from Python.Runtime.Codecs import RawProxyEncoder

Expand DownExpand Up@@ -681,3 +681,15 @@ def CanEncode(self, clr_type):
l = ob.ListField
l.Add(42)
assert ob.ListField.Count == 1

def test_int_param_resolution_required():
"""Test resolution of `int` parameters when resolution is needed"""

mri = MethodResolutionInt()
data = list(mri.MethodA(0x1000, 10))
assert len(data) == 10
assert data[0] == 0

data = list(mri.MethodA(0x100000000, 10))
assert len(data) == 10
assert data[0] == 0

[8]ページ先頭

©2009-2025 Movatter.jp