10000 Add traceback information to exception by testrunner123 · Pull Request #545 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Add traceback information to exception #545

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 5 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Changed

- Reattach python exception traceback information (#545)

### Fixed

- Fixed secondary PythonEngine.Initialize call, all sensitive static variables now reseted.
Expand Down Expand Up @@ -697,4 +699,4 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
[i131]: https://github.com/pythonnet/pythonnet/issues/131
[p531]: https://github.com/pythonnet/pythonnet/pull/531
[i755]: https://github.com/pythonnet/pythonnet/pull/755
[p534]: https://github.com/pythonnet/pythonnet/pull/534
[p534]: https://github.com/pythonnet/pythonnet/pull/534
5 changes: 4 additions & 1 deletion src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ public static void SetError(Exception e)
var pe = e as PythonException;
if (pe != null)
{
Runtime.PyErr_SetObject(pe.PyType, pe.PyValue);
Runtime.XIncref(pe.PyType);
Runtime.XIncref(pe.PyValue);
Runtime.XIncref(pe.PyTB);
Runtime.PyErr_Restore(pe.PyType, pe.PyValue, pe.PyTB);
return;
}

Expand Down
33 changes: 33 additions & 0 deletions src/tests/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,39 @@ def test_derived_class():
assert id(x) == id(ob)


def test_derived_traceback():
"""Test python exception traceback in class derived from managed base"""
class DerivedClass(SubClassTest):
__namespace__ = "Python.Test.traceback"

def foo(self):
print (xyzname)
return None

import sys,traceback
ob = DerivedClass()

# direct call
try:
ob.foo()
assert False
except:
e = sys.exc_info()
assert "xyzname" in str(e[1])
location = traceback.extract_tb(e[2])[-1]
assert location[2] == "foo"

# call through managed code
try:
FunctionsTest.test_foo(ob)
assert False
except:
e = sys.exc_info()
assert "xyzname" in str(e[1])
location = traceback.extract_tb(e[2])[-1]
assert location[2] == "foo"


def test_create_instance():
"""Test derived instances can be created from managed code"""
DerivedClass = derived_class_fixture(test_create_instance.__name__)
Expand Down
0