8000 Refactoring of MethodBinder.Bind by lostmsu · Pull Request #829 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Refactoring of MethodBinder.Bind #829

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 6 commits into from
Mar 19, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
extracted TryConvertArgument from MethodBinder.TryConvertArguments
  • Loading branch information
lostmsu committed Mar 12, 2019
commit 1dadc8a472862e97a5a66011d3ffcc3bb0df2150
46 changes: 30 additions & 16 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,43 +371,57 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
continue;
}

var parameter = pi[paramIndex];
IntPtr op = (arrayStart == paramIndex)
// map remaining Python arguments to a tuple since
// the managed function accepts it - hopefully :]
? Runtime.PyTuple_GetSlice(args, arrayStart, pyArgCount)
: Runtime.PyTuple_GetItem(args, paramIndex);

var parameter = pi[paramIndex];

var clrtype = TryComputeClrArgumentType(parameter.ParameterType, op, needsResolution: needsResolution);
if (clrtype == null)
bool isOut;
if (!TryConvertArgument(op, parameter.ParameterType, needsResolution, out margs[paramIndex], out isOut))
{
return null;
}

if (parameter.IsOut || clrtype.IsByRef)
{
outs++;
}

object arg;
if (!Converter.ToManaged(op, clrtype, out arg, false))
{
Exceptions.Clear();
return null;
}
if (arrayStart == paramIndex)
{
// TODO: is this a bug? Should this happen even if the conversion fails?
// GetSlice() creates a new reference but GetItem()
// returns only a borrow reference.
Copy link
Member

Choose a reason for hiding this comment

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

This indeed looks like a bug to me, the code has to also run before return null.

Runtime.XDecref(op);
}
margs[paramIndex] = arg;

if (parameter.IsOut || isOut)
{
outs++;
}
}

return margs;
}

static bool TryConvertArgument(IntPtr op, Type parameterType, bool needsResolution,
out object arg, out bool isOut)
{
arg = null;
isOut = false;
var clrtype = TryComputeClrArgumentType(parameterType, op, needsResolution: needsResolution);
if (clrtype == null)
{
return false;
}

if (!Converter.ToManaged(op, clrtype, out arg, false))
{
Exceptions.Clear();
return false;
}

isOut = clrtype.IsByRef;
return true;
}

static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool needsResolution)
{
// this logic below handles cases when multiple overloading methods
Expand Down
0