Skip to content

Commit 4133927

Browse files
authored
Ensure that param-array matching works correctly (#1304)
We can match n Python parameters to exactly n+1 C# parameters of which the last one is a param-array. Fixes #1302. There are still two cases that are not covered.
1 parent 24c5af3 commit 4133927

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

src/runtime/methodbinder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
512512
{
513513
if(arrayStart == paramIndex)
514514
{
515-
op = HandleParamsArray(args, arrayStart, pyArgCount, out isNewReference);
515+
op = HandleParamsArray(args, arrayStart, pyArgCount, out isNewReference);
516516
}
517517
else
518518
{
@@ -652,7 +652,7 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
652652
{
653653
match = true;
654654
}
655-
else if (positionalArgumentCount < parameters.Length)
655+
else if (positionalArgumentCount < parameters.Length && (!paramsArray || positionalArgumentCount == parameters.Length - 1))
656656
{
657657
// every parameter past 'positionalArgumentCount' must have either
658658
// a corresponding keyword argument or a default parameter
@@ -677,7 +677,7 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
677677
defaultArgList.Add(parameters[v].GetDefaultValue());
678678
defaultsNeeded++;
679679
}
680-
else if(!paramsArray)
680+
else if (!paramsArray)
681681
{
682682
match = false;
683683
}

src/testing/constructortests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,22 @@ public SubclassConstructorTest(Exception v)
4848
value = v;
4949
}
5050
}
51+
52+
public class MultipleConstructorsTest
53+
{
54+
public string value;
55+
public Type[] type;
56+
57+
public MultipleConstructorsTest()
58+
{
59+
value = "";
60+
type = new Type[1] { null };
61+
}
62+
63+
public MultipleConstructorsTest(string s, params Type[] tp)
64+
{
65+
value = s;
66+
type = tp;
67+
}
68+
}
5169
}

src/testing/methodtest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,16 @@ public static string DefaultParamsWithOverloading(int a = 5, int b = 6, int c =
703703
{
704704
return $"{a}{b}{c}{d}XXX";
705705
}
706+
707+
public static string ParamsArrayOverloaded(int i = 1)
708+
{
709+
return "without params-array";
710+
}
711+
712+
public static string ParamsArrayOverloaded(int i, params int[] paramsArray)
713+
{
714+
return "with params-array";
715+
}
706716
}
707717

708718

src/tests/test_constructors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ class Sub(System.Exception):
4444
instance = Sub()
4545
ob = SubclassConstructorTest(instance)
4646
assert isinstance(ob.value, System.Exception)
47+
48+
49+
def test_multiple_constructor():
50+
from Python.Test import MultipleConstructorsTest
51+
import System
52+
53+
# Test parameterless
54+
ob = MultipleConstructorsTest()
55+
assert ob.value == ""

src/tests/test_method.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,3 +1188,36 @@ def test_keyword_arg_method_resolution():
11881188
ob = MethodArityTest()
11891189
assert ob.Foo(1, b=2) == "Arity 2"
11901190

1191+
def test_params_array_overload():
1192+
res = MethodTest.ParamsArrayOverloaded()
1193+
assert res == "without params-array"
1194+
1195+
res = MethodTest.ParamsArrayOverloaded(1)
1196+
assert res == "without params-array"
1197+
1198+
res = MethodTest.ParamsArrayOverloaded(i=1)
1199+
assert res == "without params-array"
1200+
1201+
res = MethodTest.ParamsArrayOverloaded(1, 2)
1202+
assert res == "with params-array"
1203+
1204+
res = MethodTest.ParamsArrayOverloaded(1, 2, 3)
1205+
assert res == "with params-array"
1206+
1207+
res = MethodTest.ParamsArrayOverloaded(1, paramsArray=[])
1208+
assert res == "with params-array"
1209+
1210+
res = MethodTest.ParamsArrayOverloaded(1, i=1)
1211+
assert res == "with params-array"
1212+
1213+
res = MethodTest.ParamsArrayOverloaded(1, 2, 3, i=1)
1214+
assert res == "with params-array"
1215+
1216+
# These two cases are still incorrectly failing:
1217+
1218+
# res = MethodTest.ParamsArrayOverloaded(1, 2, i=1)
1219+
# assert res == "with params-array"
1220+
1221+
# res = MethodTest.ParamsArrayOverloaded(paramsArray=[], i=1)
1222+
# assert res == "with params-array"
1223+

0 commit comments

Comments
 (0)