8000 Add Format method to pythonexception by slide · Pull Request #1031 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Add Format method to pythonexception #1031

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 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix issues from feedback
  • Loading branch information
slide committed Apr 7, 2020
commit 7294078192549f3891e165fb53117081c2a8b11f
6 changes: 3 additions & 3 deletions src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public void TestPythonExceptionFormat()
[Test]
public void TestPythonExceptionFormatNoError()
{
var e = new PythonException();
Assert.AreEqual("Missing exception/traceback information", e.Format());
var ex = new PythonException();
Assert.AreEqual(ex.StackTrace, ex.Format());
}

[Test]
Expand All @@ -87,7 +87,7 @@ public void TestPythonExceptionFormatNoTraceback()
catch (PythonException ex)
{
// ImportError/ModuleNotFoundError do not have a traceback when not running in a script
Assert.AreEqual("Missing exception/traceback information", ex.Format());
Assert.AreEqual(ex.StackTrace, ex.Format());
}
}
}
Expand Down
39 changes: 22 additions & 17 deletions src/runtime/pythonexception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,36 @@ public string Format()
{
string res;
IntPtr gs = PythonEngine.AcquireLock();
if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
try
{
Runtime.XIncref(_pyType);
Runtime.XIncref(_pyValue);
Runtime.XIncref(_pyTB);
using (PyObject pyType = new PyObject(_pyType))
using (PyObject pyValue = new PyObject(_pyValue))
using (PyObject pyTB = new PyObject(_pyTB))
using (PyObject tb_mod = PythonEngine.ImportModule("traceback"))
if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
{
var buffer = new StringBuilder();
var values = tb_mod.InvokeMethod("format_exception", pyType, pyValue, pyTB);
Runtime.PyErr_Clear();
foreach (PyObject val in values)
Runtime.XIncref(_pyType);
Runtime.XIncref(_pyValue);
Runtime.XIncref(_pyTB);
using (PyObject pyType = new PyObject(_pyType))
using (PyObject pyValue = new PyObject(_pyValue))
using (PyObject pyTB = new PyObject(_pyTB))
using (PyObject tb_mod = PythonEngine.ImportModule("traceback"))
{
buffer.Append(val.ToString());
var buffer = new StringBuilder();
var values = tb_mod.InvokeMethod("format_exception", pyType, pyValue, pyTB);
foreach (PyObject val in values)
{
buffer.Append(val.ToString());
}
res = buffer.ToString();
}
res = buffer.ToString();
}
else
{
res = StackTrace;
}
}
else
finally
{
res = "Missing exception/traceback information";
PythonEngine.ReleaseLock(gs);
}
PythonEngine.ReleaseLock(gs);
return res;
}

Expand Down
0