Skip to content

Improve method binding #974

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

Closed
wants to merge 11 commits into from
Closed

Improve method binding #974

wants to merge 11 commits into from

Conversation

koubaa
Copy link
Contributor

@koubaa koubaa commented Oct 21, 2019

What does this implement/fix? Explain your changes.

Two method binding changes:
Support passing lists/tuples to methods which take IEnumerable, ICollection, IList
Support passing functions to methods which take Action, Func, Task

Does this close any currently open issues?

Not that I am aware of.

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

change enumerable method to non-static
add tuple test for ienumerable
@koubaa
Copy link
Contributor Author

koubaa commented Dec 1, 2019

@filmor @lostmsu @amos402 I am still working on the implementation here (the ToAction method throws a memory corruption error in py_action.Invoke) but would appreciate some preliminary review to make sure I'm headed in the right direction. Thanks!

@koubaa koubaa changed the title Add test for ienumerable method Improve method binding Dec 1, 2019
@koubaa
Copy link
Contributor Author

koubaa commented Dec 3, 2019

allow_threads in methodbinder.cs Invoke is true which is causing the memory corruption. If I set it to false there is no corruption so I need to look into why. This looks like it may be new code

Copy link
Member

@lostmsu lostmsu left a comment

Choose a reason for hiding this comment

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

I honestly think we'd be better off not introducing additional automatic conversions at this moment.

Comment on lines 948 to 956
Action action = () => {
PyObject py_action = new PyObject(value);
var py_args = new PyObject[0];
var py_result = py_action.Invoke(py_args);

//Discard the result since this is being converted to an Action
py_result.Dispose();
py_action.Dispose();
};
Copy link
Member

Choose a reason for hiding this comment

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

The object pointed to by value can be collected between the construction of this action, and the moment it is invoked. It would lead to a memory corruption.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had forgotten to push the latest change set, please check again

Copy link
Contributor Author

@koubaa koubaa left a comment

Choose a reason for hiding this comment

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

I honestly think we'd be better off not introducing additional automatic conversions at this moment.

That's an acceptable position. I'll bring this PR up at tomorrow's meeting and will like to hear from the group on how to proceed

@amos402
Copy link
Member

amos402 commented Dec 5, 2019

Not suggesting this readonly implicit conversion, what if I want to use like this

void AddElem(IList<int> lst)
{
    lst.add(10);
}
def func():
    lst = [1, 2, 3]
    AddElem(lst)
    # I want [1,2,3,10] actually

I suggest implement some subclasses from List<> so you can track the changes of the collection and apply them to the Python's list
Also the test didn't check any outputs, it only certify the runtime has no exceptions.

@koubaa
Copy link
Contributor Author

koubaa commented Dec 8, 2019

@amos402 see the latest changes. I'm dealing with some crash/exception in native code but I think I'm going in the direction that you are proposing.

@koubaa
Copy link
Contributor Author

koubaa commented Dec 8, 2019

@lostmsu since we last spoke I looked into codec and it looks like string encoding, so I'm not sure how it could apply to method binding. What is your preferred way to support non-automatic conversions for me to consider/try?

Runtime.XIncref(iterObject);
}
~IterableWrapper() {
Runtime.XDecref(iterObject);
Copy link
Member

Choose a reason for hiding this comment

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

You can't use XDecref directly in destructor, see Runtime.PyObject's destructor, you can just refer a PyObject, it can let you have no need concern about about the release.

@lostmsu
Copy link
Member

lostmsu commented Dec 20, 2019

Opened #1022 for my take on generic approach.

@koubaa
Copy link
Contributor Author

koubaa commented Mar 11, 2020

This is superseded by #1084 and #1080

@koubaa koubaa closed this Mar 11, 2020
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.

3 participants