Skip to content

Commit 49dc328

Browse files
committed
Never let bind-failure message construction throw
1 parent 1943ff5 commit 49dc328

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

src/runtime/MethodBinder.cs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,38 +1017,48 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
10171017
if (!Exceptions.ErrorOccurred())
10181018
{
10191019
var value = new StringBuilder("No method matches given arguments");
1020-
// Use the snake_case name Python callers use, matching the hinted signatures below.
1021-
if (methodinfo != null && methodinfo.Length > 0)
1020+
try
10221021
{
1023-
value.Append($" for {MethodSignatureFormatter.SnakeCaseName(methodinfo[0])}");
1024-
}
1025-
else if (list.Count > 0)
1026-
{
1027-
value.Append($" for {MethodSignatureFormatter.SnakeCaseName(list[0].MethodBase)}");
1028-
}
1022+
// Use the snake_case name Python callers use, matching the hinted signatures below.
1023+
if (methodinfo != null && methodinfo.Length > 0)
1024+
{
1025+
value.Append($" for {MethodSignatureFormatter.SnakeCaseName(methodinfo[0])}");
1026+
}
1027+
else if (list.Count > 0)
1028+
{
1029+
value.Append($" for {MethodSignatureFormatter.SnakeCaseName(list[0].MethodBase)}");
1030+
}
10291031

1030-
value.Append(": ");
1031-
AppendArgumentTypes(to: value, args);
1032-
1033-
// The argument types echo above covers positional args only; name the first
1034-
// unknown kwarg (if any) so a misspelled keyword argument is visible.
1035-
AppendUnexpectedKeywordArgument(value, kw, info);
1036-
1037-
// List the candidate overloads so the caller can see what was
1038-
// expected (e.g. that an int overload exists when a float was
1039-
// passed). Applies to every "no match" case, not just numeric ones.
1040-
var candidates = methodinfo != null && methodinfo.Length > 0
1041-
? methodinfo.Cast<MethodBase>()
1042-
: list?.Select(m => m.MethodBase);
1043-
var overloads = MethodSignatureFormatter.FormatOverloads(candidates);
1044-
if (overloads.Length > 0)
1045-
{
1046-
// The kwarg hint may already end the sentence with a question mark.
1047-
if (value[value.Length - 1] != '?')
1032+
value.Append(": ");
1033+
AppendArgumentTypes(to: value, args);
1034+
1035+
// The argument types echo above covers positional args only; name the first
1036+
// unknown kwarg (if any) so a misspelled keyword argument is visible.
1037+
AppendUnexpectedKeywordArgument(value, kw, info);
1038+
1039+
// List the candidate overloads so the caller can see what was
1040+
// expected (e.g. that an int overload exists when a float was
1041+
// passed). Applies to every "no match" case, not just numeric ones.
1042+
var candidates = methodinfo != null && methodinfo.Length > 0
1043+
? methodinfo.Cast<MethodBase>()
1044+
: list?.Select(m => m.MethodBase);
1045+
var overloads = MethodSignatureFormatter.FormatOverloads(candidates);
1046+
if (overloads.Length > 0)
10481047
{
1049-
value.Append('.');
1048+
// The kwarg hint may already end the sentence with a question mark.
1049+
if (value[value.Length - 1] != '?')
1050+
{
1051+
value.Append('.');
1052+
}
1053+
value.Append(' ').Append(overloads);
10501054
}
1051-
value.Append(' ').Append(overloads);
1055+
}
1056+
catch
1057+
{
1058+
// The details above are best-effort diagnostics over arbitrary caller
1059+
// input; an exception here would escape the tp_call slot into CPython
1060+
// and mask the bind failure. Raise with whatever was appended so far.
1061+
Exceptions.Clear();
10521062
}
10531063

10541064
Exceptions.RaiseTypeError(value.ToString());

0 commit comments

Comments
 (0)