Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Changed

- Added argument types information to "No method matches given arguments" message

### Fixed

## [2.4.0][]
Expand Down
35 changes: 35 additions & 0 deletions src/embed_tests/TestCallbacks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest {
using Runtime = Python.Runtime.Runtime;

public class TestCallbacks {
[OneTimeSetUp]
public void SetUp() {
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose() {
PythonEngine.Shutdown();
}

[Test]
public void TestNoOverloadException() {
int passed = 0;
var aFunctionThatCallsIntoPython = new Action<int>(value => passed = value);
using (Py.GIL()) {
dynamic callWith42 = PythonEngine.Eval("lambda f: f([42])");
var error = Assert.Throws<PythonException>(() => callWith42(aFunctionThatCallsIntoPython.ToPython()));
Assert.AreEqual("TypeError", error.PythonTypeName);
string expectedArgTypes = Runtime.IsPython2
? "(<type 'list'>)"
: "(<class 'list'>)";
StringAssert.EndsWith(expectedArgTypes, error.Message);
}
}
}
}
31 changes: 28 additions & 3 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Reflection;
using System.Text;

namespace Python.Runtime
{
Expand Down Expand Up @@ -555,12 +556,36 @@ internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase i

if (binding == null)
{
var value = "No method matches given arguments";
var value = new StringBuilder("No method matches given arguments");
if (methodinfo != null && methodinfo.Length > 0)
{
value += $" for {methodinfo[0].Name}";
value.Append($" for {methodinfo[0].Name}");
}
Exceptions.SetError(Exceptions.TypeError, value);

long argCount = Runtime.PyTuple_Size(args);
value.Append(": (");
for(long argIndex = 0; argIndex < argCount; argIndex++) {
var arg = Runtime.PyTuple_GetItem(args, argIndex);
if (arg != IntPtr.Zero) {
var type = Runtime.PyObject_Type(arg);
if (type != IntPtr.Zero) {
try {
var description = Runtime.PyObject_Unicode(type);
if (description != IntPtr.Zero) {
value.Append(Runtime.GetManagedString(description));
Runtime.XDecref(description);
}
} finally {
Runtime.XDecref(type);
}
}
}

if (argIndex + 1 < argCount)
value.Append(", ");
}
value.Append(')');
Exceptions.SetError(Exceptions.TypeError, value.ToString());
return IntPtr.Zero;
}

Expand Down