Skip to content

Commit c9afb7a

Browse files
author
dse
committed
PythonTypeName property added to the PythonException object.
1 parent cd21891 commit c9afb7a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/embed_tests/TestPythonException.cs

+14
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,19 @@ public void TestNoError()
4040
var e = new PythonException(); // There is no PyErr to fetch
4141
Assert.AreEqual("", e.Message);
4242
}
43+
44+
[Test]
45+
public void TestPythonErrorTypeName()
46+
{
47+
try
48+
{
49+
var module = PythonEngine.ImportModule("really____unknown___module");
50+
Assert.Fail("Unknown module should not be loaded");
51+
}
52+
catch (PythonException ex)
53+
{
54+
Assert.AreEqual(ex.PythonTypeName, "ModuleNotFoundError");
55+
}
56+
}
4357
}
4458
}

src/runtime/pythonexception.cs

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class PythonException : System.Exception
1313
private IntPtr _pyTB = IntPtr.Zero;
1414
private string _tb = "";
1515
private string _message = "";
16+
private string _pythonTypeName = "";
1617
private bool disposed = false;
1718

1819
public PythonException()
@@ -33,6 +34,8 @@ public PythonException()
3334
type = pyTypeName.ToString();
3435
}
3536

37+
_pythonTypeName = type;
38+
3639
Runtime.XIncref(_pyValue);
3740
using (var pyValue = new PyObject(_pyValue))
3841
{
@@ -132,6 +135,13 @@ public override string StackTrace
132135
get { return _tb; }
133136
}
134137

138+
/// <summary>
139+
/// Python error type name.
140+
/// </summary>
141+
public string PythonTypeName
142+
{
143+
get { return _pythonTypeName; }
144+
}
135145

136146
/// <summary>
137147
/// Dispose Method

0 commit comments

Comments
 (0)