FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improve "No constructor matches given arguments" message with more details by lostmsu · Pull Request #1143 · pythonnet/pythonnet · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 10 additions & 1 deletion src/runtime/constructorbinder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If GetManagedString fails, you leak description

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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

Back | FazBrowse Home | New Git URL