Skip to content

Fixed #296 Converter support for PyObject #297

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

Merged
merged 7 commits into from
Dec 16, 2016
Merged

Fixed #296 Converter support for PyObject #297

merged 7 commits into from
Dec 16, 2016

Conversation

rmadsen-ks
Copy link
Contributor

Work in progress.

Note, testing has not been done, except for exploratory style. How is testing supposed to be done?

@den-run-ai
Copy link
Contributor

@rmadsen-ks you can look at callbacktest.cs and test_callback.py for testing this.

@den-run-ai
Copy link
Contributor

@rmadsen-ks do you agree to transition from Zope to MIT license?

@rmadsen-ks
Copy link
Contributor Author

@denfromufa, Test passed. I have added a test verifying that the fix does what it should.

I agree to transition to MIT license.

@rmadsen-ks rmadsen-ks changed the title [WIP] Fixed #296 Converter support for PyObject Fixed #296 Converter support for PyObject Nov 30, 2016
@@ -6,7 +6,7 @@
import System
import six
from Python.Test import ClassTest
from System.Collections import Hashtable
from System.Collections import Hashtable
Copy link
Contributor

Choose a reason for hiding this comment

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

where is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was not needed by me, but it seems I had added a windows line-ending. Its fixed now.

@den-run-ai
Copy link
Contributor

how about garbage collection, is CLR or Python responsible? If Python, then I do not see Decref, so it would leak memory?

@rmadsen-ks
Copy link
Contributor Author

rmadsen-ks commented Dec 2, 2016

I have tested callbacks rapidly and the ref count does not increase permanently. Neither does it seem to leak when many new objects are made. I have tried creating 50M objects with no noticable leak.

I should not need to Decref for the following reason:

As the comment in pyobject.cs says, the PyObject assumes ownership. So if we want to convert a python object to a PyObject using ToManagedValue, we must increase the refcount when we are giving it to the constructor of PyObject. So I implemented that part. The PyObject decreases the ref count when the garbage collector cleans it up, which happens eventually. So when a new C# PyObject has been created from a python object, it will eventually be cleaned up by the .NET GC. Is some cases using(){} can also be used to give more predictable memory performance.

In the same way, if we want to convert C# PyObject to a python object, we increase the ref count, because now Python has an extra reference to it, so it needs ownership. Python will clean that up on its own when it goes out of scope.

@den-run-ai
Copy link
Contributor

@tonyroberts @vmuriart @filmor please review!


//==========================================================================
// Tests calling from Python into C# and back into Python using a PyObject.
// SelfCallbackTest should be inherited from a Python class.
Copy link
Member

Choose a reason for hiding this comment

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

Very minor nitpick, but shouldn't that read "inherited by a Python class"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment has been changed.

@den-run-ai
Copy link
Contributor

@rmadsen-ks this pull request does NOT resolve the issue described in #94:

dta@dta-Inspiron-N5050 ~/Downloads $ sudo pip install pythonnet-fix-develop-296-convert-PyObject.zip -U --force
Processing ./pythonnet-fix-develop-296-convert-PyObject.zip
Installing collected packages: pythonnet
  Found existing installation: pythonnet 2.2.0
    Uninstalling pythonnet-2.2.0:
      Successfully uninstalled pythonnet-2.2.0
  Running setup.py install for pythonnet ... done
Successfully installed pythonnet-2.2.0
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
dta@dta-Inspiron-N5050 ~/Downloads $ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System import Object, Array
>>> import sys
>>> a = Array[Object]([sys]) ; print a[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: value cannot be converted to Object

@rmadsen-ks
Copy link
Contributor Author

Hi, I'm not suggesting that it does. You want #94 fixed as part of this?

@rmadsen-ks
Copy link
Contributor Author

@denfromufa, is there anything I need to do to move this further or do I need to find another solution?

@den-run-ai
Copy link
Contributor

@rmadsen-ks your pull request is completely valid.

@filmor
Copy link
Member

filmor commented Dec 16, 2016

To me this also looks fine, but why do you introduce a new RefCount property? Either remove it or add tests for it, after that we can merge.

@den-run-ai den-run-ai merged commit 566898a into pythonnet:master Dec 16, 2016
@rmadsen-ks
Copy link
Contributor Author

@filmor, I found it useful for verifying that I was not leaking reference counts. Without it, it is hard to get out the reference cound from C# as the NativeMethods are internal. It is not critical for me, so we can remove it.

@den-run-ai
Copy link
Contributor

@rmadsen-ks can you please open a new PR? I made a mistake and merged this pull request without seeing @filmor concern.

@rmadsen-ks
Copy link
Contributor Author

It is here #306

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