Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 1 commit intopythonnet:masterfromtminka:byref
Jan 28, 2021

Conversation

tminka
Copy link
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 toAUTHORS
  • Updated theCHANGELOG

@lostmsu
Copy link
Member

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

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

@tminka
Copy link
ContributorAuthor

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

@lostmsu
Copy link
Member

lostmsu commentedJan 21, 2021
edited
Loading

Since you can't override Python assignment operator, this fix is fundamentally incompatible with the proper solution withByRef, 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

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

@tminka
Copy link
ContributorAuthor

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. Seemethodbinder.cs andstackoverflow. Therefore the "proper solution" is forhandleEventData to return a tuple of out/ref parameter values. No specialByRef type is needed.

@lostmsu
Copy link
Member

lostmsu commentedJan 27, 2021
edited
Loading

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"

@lostmsulostmsu merged commit063a674 intopythonnet:masterJan 28, 2021
@tminkatminka deleted the byref branchJanuary 28, 2021 10:22
@noambonnieclear
Copy link

So is this supposed to be released with 2.5.2?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@lostmsulostmsulostmsu approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@tminka@lostmsu@noambonnieclear

[8]ページ先頭

©2009-2025 Movatter.jp