Skip to content

exception message passed from Python to .NET correctly #84

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 1 commit into from
Closed

exception message passed from Python to .NET correctly #84

wants to merge 1 commit into from

Conversation

den-run-ai
Copy link
Contributor

#82

@tonyroberts
Copy link
Contributor

Hi,

you need to be careful with reference counting when creating Python objects like this. Here you've created a new Python string object (_PyValueStr) but its reference count doesn't get decremented and so that object will never be deallocated.

If you use the PyObject .net class instead, in a using block, that will ensure it gets cleaned up correctly - for example the following should solve your problem without leaking any objects

        if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero))
        {
            using (PyObject pyType = new PyObject(_pyType))
            using (PyObject pyTypeName = pyType.GetAttr("__name__"))
            using (PyObject pyValue = new PyObject(_pyValue))
            {
                    string type = pyTypeName.ToString();
                    string value = pyValue.ToString();
                    _message = type + " : " + value;
            }
        }

The ToString method on PyObject does the string conversion, whereas Runtime.GetManagedString will only return a string of the Python object is a Python string.

Could be worth adding a unit test too, since now we have CI builds set up for most of the supported configurations.

cheers,
Tony

@den-run-ai
Copy link
Contributor Author

Is this where the unit test is going to?

https://github.com/pythonnet/pythonnet/blob/master/src/tests/test_exceptions.py

@tonyroberts
Copy link
Contributor

Hi,

yes that seems like a good place to put it,

cheers,
Tony

@tonyroberts
Copy link
Contributor

This has been implemented in #88, so I think this one can be closed now. Please feel free to re-open if there's still an issue.

thanks!
Tony

@den-run-ai
Copy link
Contributor Author

Did the unit test get implemented too?

@tonyroberts
Copy link
Contributor

Nope :( If you have one left over from your earlier work please feel free to open another PR.

thanks,
Tony

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.

2 participants