← Winnitude! Search  ·  Reader Mode (2997 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 60 LINKS

Support ByRef arguments in event handlers by tminka · Pull Request #1364 · pythonnet/pythonnet

Repository navigation

Support ByRef arguments in event handlers - #1364


Merged
lostmsu merged 1 commit into pythonnet:master from tminka:byref Jan 28, 2021
Merged

Support ByRef arguments in event handlers #1364 lostmsu merged 1 commit into pythonnet:master from tminka:byref

Conversation

tminka commented Jan 21, 2021



Copy link Copy Markdown
Contributor

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 commented Jan 21, 2021



Copy link Copy Markdown
Member

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 commented Jan 21, 2021



Copy link Copy Markdown
Contributor

Author

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

lostmsu commented Jan 21, 2021 · edited Loading



Copy link Copy Markdown
Member

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
lostmsu mentioned this pull request Jan 21, 2021 Crash on Event invocation with ref parameter #1355 Closed

tminka commented Jan 27, 2021



Copy link Copy Markdown
Contributor

Author

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 commented Jan 27, 2021 · edited Loading



Copy link Copy Markdown
Member

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.

Added the ability to implement delegates with ref and out paramet... bbc6cb7
...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.
tminka force-pushed the byref branch from d16f6ea to bbc6cb7
Compare
January 28, 2021 02:07 lostmsu approved these changes Jan 28, 2021
View reviewed changes
Comment thread src/runtime/delegatemanager.cs
}
else if (Runtime.PyTuple_Check(op) && Runtime.PyTuple_Size(op) == tupleSize)
{
int index = isVoid ? 0 : 1;
Comment thread src/runtime/delegatemanager.cs 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();
Comment thread src/runtime/delegatemanager.cs
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)");
lostmsu merged commit 063a674 into pythonnet : master Jan 28, 2021 tminka deleted the byref branch January 28, 2021 10:22 noambonnieclear mentioned this pull request Feb 11, 2021 ParameterInfo.Name needs to be checked for null before usage #1375 Merged 2 of 4 tasks

noambonnieclear commented Feb 11, 2021



Copy link Copy Markdown

So is this supposed to be released with 2.5.2?

lostmsu mentioned this pull request Feb 21, 2021 Crash when calling back through method with ref ValueType parameter #965 Closed lostmsu mentioned this pull request Jan 6, 2022 Support for byref parameters when overriding .NET methods from Python #1663 Merged 2 of 4 tasks Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

3 participants