Skip to content

Improve "No constructor matches given arguments" message with more details #1143

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020
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
11 changes: 10 additions & 1 deletion src/runtime/constructorbinder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using System.Text;

namespace Python.Runtime
{
Expand Down Expand Up @@ -93,7 +94,15 @@ internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info)

if (binding == null)
{
Exceptions.SetError(Exceptions.TypeError, "no constructor matches given arguments");
var errorMessage = new StringBuilder("No constructor matches given arguments");
if (info != null && info.IsConstructor && info.DeclaringType != null)
{
errorMessage.Append(" for ").Append(info.DeclaringType.Name);
}

errorMessage.Append(": ");
AppendArgumentTypes(to: errorMessage, args);
Exceptions.SetError(Exceptions.TypeError, errorMessage.ToString());
return null;
}
}
Expand Down
59 changes: 36 additions & 23 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,40 @@ internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase i
return Invoke(inst, args, kw, info, null);
}

protected static void AppendArgumentTypes(StringBuilder to, IntPtr args)
{
long argCount = Runtime.PyTuple_Size(args);
to.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)
{
to.Append(Runtime.GetManagedString(description));
Copy link
Member

Choose a reason for hiding this comment

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

If GetManagedString fails, you leak description

Copy link
Member

Choose a reason for hiding this comment

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

Ah, but I see that is not your doing but was already in the code...

Copy link
Member Author

Choose a reason for hiding this comment

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

@filmor it is still my doing :-D

Runtime.XDecref(description);
}
}
finally
{
Runtime.XDecref(type);
}
}
}

if (argIndex + 1 < argCount)
to.Append(", ");
}
to.Append(')');
}

internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
{
Binding binding = Bind(inst, args, kw, info, methodinfo);
Expand All @@ -613,29 +647,8 @@ internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase i
value.Append($" for {methodinfo[0].Name}");
}

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(')');
value.Append(": ");
AppendArgumentTypes(to: value, args);
Exceptions.SetError(Exceptions.TypeError, value.ToString());
return IntPtr.Zero;
}
Expand Down