Skip to content
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

Support ByRef arguments in event handlers #1364

Merged
merged 1 commit into from Jan 28, 2021
Merged

Conversation

tminka
Copy link
Contributor

@tminka tminka commented Jan 21, 2021

What does this implement/fix? Explain your changes.

When an event handler has a ByRef argument, extra logic is needed in DelegateManager.GetDispatcher or else the program will access invalid memory and crash.

Does this close any currently open issues?

#1355

Any other comments?

...

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@lostmsu
Copy link
Member

lostmsu commented Jan 21, 2021

This still does not allow python to change the value under the memory location passed by reference?

Ideally, a special Python ByRef type should be constructed to hold the value until function exits, then the caller should check if Python updated the value in the ByRef instance, and update the value pointed by reference accordingly.

@tminka
Copy link
Contributor Author

tminka commented Jan 21, 2021

Perhaps, but your scenario is not the one in #1355 . This PR at least prevents crashes.

@lostmsu
Copy link
Member

lostmsu commented Jan 21, 2021

Since you can't override Python assignment operator, this fix is fundamentally incompatible with the proper solution with ByRef, so will have to be replaced by 3.0.0, otherwise it will be a breaking change.

Even if we don't implement updating the value right now, to avoid making breaking changes later the value has to be wrapped into some Python object instance.

E.g. in the linked issue the handler code should be

def handleEventData(data):
    print("=== event invoked. data: {} ===".format(data.Value)) # NOTE .Value

@tminka
Copy link
Contributor Author

tminka commented Jan 27, 2021

When Python calls a .NET method with ref arguments, it returns a tuple of out/ref parameters, and if there's a return value it is the first in the tuple. See methodbinder.cs and stackoverflow. Therefore the "proper solution" is for handleEventData to return a tuple of out/ref parameter values. No special ByRef type is needed.

@lostmsu
Copy link
Member

lostmsu commented Jan 27, 2021

Good point. Still for forward compatibility we must at least check Python returns a tuple of the correct size, or better yet actually implement the marshaling of updated variables back to the event.

…ers in Python, by returning the modified parameter values in a tuple.

BREAKING: MethodBinder omits a void return type when returning a tuple of out parameters.
DelegateManager unpacks a tuple of out parameters from Python (reversing the logic in MethodBinder) and sets the out parameters of the delegate.
}
else if (Runtime.PyTuple_Check(op) && Runtime.PyTuple_Size(op) == tupleSize)
{
int index = isVoid ? 0 : 1;
Copy link
Member

Choose a reason for hiding this comment

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

NIT: retValueIndex

Comment on lines +346 to +347
Exceptions.RaiseTypeError($"The Python function returned a tuple where element {i} was not {t.GetElementType()} (the out parameter type)");
throw new PythonException();
Copy link
Member

Choose a reason for hiding this comment

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

This is my only concern which I am not sure about. I think this should be a .NET exception class. What do you think?

IntPtr item0 = Runtime.PyTuple_GetItem(op, 0);
if (!Converter.ToManaged(item0, rtype, out object result0, true))
{
Exceptions.RaiseTypeError($"The Python function returned a tuple where element 0 was not {rtype} (the return type)");
Copy link
Member

Choose a reason for hiding this comment

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

NIT: "was not" -> "is not convertible to"

@lostmsu lostmsu merged commit 063a674 into pythonnet:master Jan 28, 2021
@tminka tminka deleted the byref branch January 28, 2021 10:22
@noambonnieclear
Copy link

So is this supposed to be released with 2.5.2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants