8000 Fix issues from feedback · pythonnet/pythonnet@7294078 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7294078

Browse files
committed
Fix issues from feedback
1 parent 37bc2be commit 7294078

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/embed_tests/TestPythonException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void TestPythonExceptionFormat()
7272
[Test]
7373
public void TestPythonExceptionFormatNoError()
7474
{
75-
var e = new PythonException();
76-
Assert.AreEqual("Missing exception/traceback information", e.Format());
75+
var ex = new PythonException();
76+
Assert.AreEqual(ex.StackTrace, ex.Format());
7777
}
7878

7979
[Test]
@@ -87,7 +87,7 @@ public void TestPythonExceptionFormatNoTraceback()
8787
catch (PythonException ex)
8888
{
8989
// ImportError/ModuleNotFoundError do not have a traceback when not running in a script
90-
Assert.AreEqual("Missing exception/traceback information", ex.Format());
90+
Assert.AreEqual(ex.StackTrace, ex.Format());
9191
}
9292
}
9393
}

src/runtime/pythonexception.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,36 @@ public string Format()
154154
{
155155
string res;
156156
IntPtr gs = PythonEngine.AcquireLock();
157-
if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
157+
try
158158
{
159-
Runtime.XIncref(_pyType);
160-
Runtime.XIncref(_pyValue);
161-
Runtime.XIncref(_pyTB);
162-
using (PyObject pyType = new PyObject(_pyType))
163-
using (PyObject pyValue = new PyObject(_pyValue))
164-
using (PyObject pyTB = new PyObject(_pyTB))
165-
using (PyObject tb_mod = PythonEngine.ImportModule("traceback"))
159+
if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
166160
{
167-
var buffer = new StringBuilder();
168-
var values = tb_mod.InvokeMethod("format_exception", pyType, pyValue, pyTB);
169-
Runtime.PyErr_Clear();
170-
foreach (PyObject val in values)
161+
Runtime.XIncref(_pyType);
162+
Runtime.XIncref(_pyValue);
163+
Runtime.XIncref(_pyTB);
164+
using (PyObject pyType = new PyObject(_pyType))
165+
using (PyObject pyValue = new PyObject(_pyValue))
166+
using (PyObject pyTB = new PyObject(_pyTB))
167+
using (PyObject tb_mod = PythonEngine.ImportModule("traceback"))
171168
{
172-
buffer.Append(val.ToString());
169+
var buffer = new StringBuilder();
170+
var values = tb_mod.InvokeMethod("format_exception", pyType, pyValue, pyTB);
171+
foreach (PyObject val in values)
172+
{
173+
buffer.Append(val.ToString());
174+
}
175+
res = buffer.ToString();
173176
}
174-
res = buffer.ToString();
177+
}
178+
else
179+
{
180+
res = StackTrace;
175181
}
176182
}
177-
else
183+
finally
178184
{
179-
res = "Missing exception/traceback information";
185+
PythonEngine.ReleaseLock(gs);
180186
}
181-
PythonEngine.ReleaseLock(gs);
182187
return res;
183188
}
184189

0 commit comments

Comments
 (0)
0