10000 remove needsResolution hack · pythonnet/pythonnet@e42ec3b · GitHub
[go: up one dir, main page]

Skip to content

Commit e42ec3b

Browse files
committed
remove needsResolution hack
fixes #1523
1 parent 0fdce52 commit e42ec3b

File tree

3 files changed

+14
-26
lines changed

3 files changed

+14
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ One must now either use enum members (e.g. `MyEnum.Option`), or use enum constru
5252
- `PythonException.Restore` no longer clears `PythonException` instance.
5353
- Replaced the old `__import__` hook hack with a PEP302-style Meta Path Loader
5454
- BREAKING: Names of .NET types (e.g. `str(__class__)`) changed to better support generic types
55+
- BREAKING: overload resolution will no longer prefer basic types. Instead, first matching overload will
56+
be chosen.
5557

5658
### Fixed
5759

src/embed_tests/TestOperator.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using NUnit.Framework;
22

33
using Python.Runtime;
4+
using Python.Runtime.Codecs;
45

6+
using System;
57
using System.Linq;
68
using System.Reflection;
79

@@ -212,21 +214,19 @@ public OperableObject(int num)
212214
return (a.Num >= b);
213215
}
214216

215-
public static bool operator >=(OperableObject a, PyObject b)
217+
public static bool operator >=(OperableObject a, (int, int) b)
216218
{
217219
using (Py.GIL())
218220
{
219-
// Assuming b is a tuple, take the first element.
220-
int bNum = b[0].As<int>();
221+
int bNum = b.Item1;
221222
return a.Num >= bNum;
222223
}
223224
}
224-
public static bool operator <=(OperableObject a, PyObject b)
225+
public static bool operator <=(OperableObject a, (int, int) b)
225226
{
226227
using (Py.GIL())
227228
{
228-
// Assuming b is a tuple, take the first element.
229-
int bNum = b[0].As<int>();
229+
int bNum = b.Item1;
230230
return a.Num <= bNum;
231231
}
232232
}
@@ -421,6 +421,7 @@ public void ForwardOperatorOverloads()
421421
[Test]
422422
public void TupleComparisonOperatorOverloads()
423423
{
424+
TupleCodec<ValueTuple>.Register();
424425
string name = string.Format("{0}.{1}",
425426
typeof(OperableObject).DeclaringType.Name,
426427
typeof(OperableObject).Name);

src/runtime/methodbinder.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
433433
pi = pi.Take(1).ToArray();
434434
}
435435
int outs;
436-
var margs = TryConvertArguments(pi, paramsArray, args, pynargs, kwargDict, defaultArgList,
437-
needsResolution: _methods.Length > 1, // If there's more than one possible match.
438-
outs: out outs);
436+
var margs = TryConvertArguments(pi, paramsArray, args, pynargs, kwargDict, defaultArgList, outs: out outs);
439437
if (margs == null)
440438
{
441439
var mismatchCause = PythonException.FetchCurrent();
@@ -612,7 +610,6 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
612610
IntPtr args, int pyArgCount,
613611
Dictionary<string, IntPtr> kwargDict,
614612
ArrayList defaultArgList,
615-
bool needsResolution,
616613
out int outs)
617614
{
618615
outs = 0;
@@ -653,7 +650,7 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
653650
}
654651

655652
bool isOut;
656-
if (!TryConvertArgument(op, parameter.ParameterType, needsResolution, out margs[paramIndex], out isOut))
653+
if (!TryConvertArgument(op, parameter.ParameterType, out margs[paramIndex], out isOut))
657654
{
658655
return null;
659656
}
@@ -681,16 +678,15 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
681678
/// </summary>
682679
/// <param name="op">Pointer to the Python argument object.</param>
683680
/// <param name="parameterType">That parameter's managed type.</param>
684-
/// <param name="needsResolution">If true, there are multiple overloading methods that need resolution.</param>
685681
/// <param name="arg">Converted argument.</param>
686682
/// <param name="isOut">Whether the CLR type is passed by reference.</param>
687683
/// <returns>true on success</returns>
688-
static bool TryConvertArgument(IntPtr op, Type parameterType, bool needsResolution,
684+
static bool TryConvertArgument(IntPtr op, Type parameterType,
689685
out object arg, out bool isOut)
690686
{
691687
arg = null;
692688
isOut = false;
693-
var clrtype = TryComputeClrArgumentType(parameterType, op, needsResolution: needsResolution);
689+
var clrtype = TryComputeClrArgumentType(parameterType, op);
694690
if (clrtype == null)
695691
{
696692
return false;
@@ -710,25 +706,14 @@ static bool TryConvertArgument(IntPtr op, Type parameterType, bool needsResoluti
710706
/// </summary>
711707
/// <param name="parameterType">The parameter's managed type.</param>
712708
/// <param name="argument">Pointer to the Python argument object.</param>
713-
/// <param name="needsResolution">If true, there are multiple overloading methods that need resolution.</param>
714709
/// <returns>null if conversion is not possible</returns>
715-
static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool needsResolution)
710+
static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument)
716711
{
717712
// this logic below handles cases when multiple overloading methods
718713
// are ambiguous, hence comparison between Python and CLR types
719714
// is necessary
720715
Type clrtype = null;
721716
IntPtr pyoptype;
722-
if (needsResolution)
723-
{
724-
// HACK: each overload should be weighted in some way instead
725-
pyoptype = Runtime.PyObject_Type(argument);
726-
if (pyoptype != IntPtr.Zero)
727-
{
728-
clrtype = Converter.GetTypeByAlias(pyoptype);
729-
}
730-
Runtime.XDecref(pyoptype);
731-
}
732717

733718
if (clrtype != null)
734719
{

0 commit comments

Comments
 (0)
0