From f90f47b4084a651d58a75322d720a7627013212f Mon Sep 17 00:00:00 2001 From: testrunner123 Date: Mon, 18 Sep 2017 11:59:21 +0200 Subject: [PATCH 1/5] Add traceback information to exception --- src/runtime/exceptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/exceptions.cs b/src/runtime/exceptions.cs index 743b5416f..93960e81f 100644 --- a/src/runtime/exceptions.cs +++ b/src/runtime/exceptions.cs @@ -256,7 +256,7 @@ public static void SetError(Exception e) var pe = e as PythonException; if (pe != null) { - Runtime.PyErr_SetObject(pe.PyType, pe.PyValue); + Runtime.PyErr_Restore(pe.PyType, pe.PyValue, pe.PyTB); return; } From 6befab65b5821175c5ecb2d45146a3d9995e7cba Mon Sep 17 00:00:00 2001 From: testrunner123 Date: Mon, 18 Sep 2017 12:06:43 +0200 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef9358793..298f3b443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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 \ No newline at end of file +[p534]: https://github.com/pythonnet/pythonnet/pull/534 From 57769f12727e2eb455cf51f3a4c731e25172b12a Mon Sep 17 00:00:00 2001 From: testrunner123 Date: Tue, 19 Sep 2017 13:51:15 +0200 Subject: [PATCH 3/5] Traceback test --- src/tests/test_subclass.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/tests/test_subclass.py b/src/tests/test_subclass.py index 43d013c7c..0bea19bd8 100644 --- a/src/tests/test_subclass.py +++ b/src/tests/test_subclass.py @@ -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)[0] + 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)[0] + 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__) From dd5e2b696b4cbbe588128be5de099abf758eb4c5 Mon Sep 17 00:00:00 2001 From: testrunner123 Date: Tue, 19 Sep 2017 16:58:45 +0200 Subject: [PATCH 4/5] fix python 2.x issues --- src/tests/test_subclass.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/test_subclass.py b/src/tests/test_subclass.py index 0bea19bd8..ab440d429 100644 --- a/src/tests/test_subclass.py +++ b/src/tests/test_subclass.py @@ -147,7 +147,7 @@ def foo(self): except: e = sys.exc_info() assert "xyzname" in str(e[1]) - location = traceback.extract_tb(e[2], -1)[0] + location = traceback.extract_tb(e[2])[-1] assert location[2] == "foo" # call through managed code @@ -157,7 +157,7 @@ def foo(self): except: e = sys.exc_info() assert "xyzname" in str(e[1]) - location = traceback.extract_tb(e[2], -1)[0] + location = traceback.extract_tb(e[2])[-1] assert location[2] == "foo" From d80a8b93129e9d732b89959af85467623f537492 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Wed, 14 Nov 2018 15:41:49 +0100 Subject: [PATCH 5/5] Increase ref-count as documented for PyErr_Restore --- src/runtime/exceptions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/runtime/exceptions.cs b/src/runtime/exceptions.cs index 93960e81f..8bed0abfd 100644 --- a/src/runtime/exceptions.cs +++ b/src/runtime/exceptions.cs @@ -256,6 +256,9 @@ public static void SetError(Exception e) var pe = e as PythonException; if (pe != null) { + Runtime.XIncref(pe.PyType); + Runtime.XIncref(pe.PyValue); + Runtime.XIncref(pe.PyTB); Runtime.PyErr_Restore(pe.PyType, pe.PyValue, pe.PyTB); return; }