-
Notifications
You must be signed in to change notification settings - Fork 751
Support ByRef arguments in event handlers #1364
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
Conversation
This still does not allow python to change the value under the memory location passed by reference? Ideally, a special Python |
Perhaps, but your scenario is not the one in #1355 . This PR at least prevents crashes. |
Since you can't override Python assignment operator, this fix is fundamentally incompatible with the proper solution with 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 |
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 |
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: retValueIndex
Exceptions.RaiseTypeError($"The Python function returned a tuple where element {i} was not {t.GetElementType()} (the out parameter type)"); | ||
throw new PythonException(); |
There was a problem hiding this comment.
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)"); |
There was a problem hiding this comment.
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"
So is this supposed to be released with 2.5.2? |
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.
AUTHORS
CHANGELOG