public PythonException() : base() { Runtime.PyErr_Fetch(ref excType, ref excValue, ref excTb); Runtime.Incref(excType); Runtime.Incref(excValue); Runtime.Incref(excTb); }
public PythonException() { IntPtr gs = PythonEngine.AcquireLock(); Runtime.PyErr_Fetch(out _pyType, out _pyValue, out _pyTB); if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero) { string type; string message; Runtime.XIncref(_pyType); using (var pyType = new PyObject(_pyType)) using (PyObject pyTypeName = pyType.GetAttr("__name__")) { type = pyTypeName.ToString(); } _pythonTypeName = type; // TODO: If pyValue has a __cause__ attribute, then we could set this.InnerException to the equivalent managed exception. Runtime.XIncref(_pyValue); using (var pyValue = new PyObject(_pyValue)) { message = pyValue.ToString(); } _message = type + " : " + message; } if (_pyTB != IntPtr.Zero) { using var tb_module = PyModule.Import("traceback"); Runtime.XIncref(_pyTB); using var pyTB = new PyObject(_pyTB); using var tbList = tb_module.InvokeMethod("format_tb", pyTB); var sb = new StringBuilder(); // Reverse Python's traceback list to match the order used in C# // stacktraces foreach (var line in tbList.Reverse()) { sb.Append(line.ToString()); } _tb = sb.ToString(); } PythonEngine.ReleaseLock(gs); }
public PythonException() : base() { Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB); Runtime.Incref(_pyType); Runtime.Incref(_pyValue); Runtime.Incref(_pyTB); IntPtr gs = PythonEngine.AcquireLock(); if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero)) { string type = new PyObject(_pyType).GetAttr("__name__").ToString(); string message = Runtime.GetManagedString(_pyValue); _message = type + " : " + message; } if (_pyTB != IntPtr.Zero) { PyObject tb_module = PythonEngine.ImportModule("traceback"); _tb = tb_module.InvokeMethod("format_tb", new PyObject(_pyTB)).ToString(); } PythonEngine.ReleaseLock(gs); }
public PythonException() { IntPtr gs = PythonEngine.AcquireLock(); Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB); Runtime.XIncref(_pyType); Runtime.XIncref(_pyValue); Runtime.XIncref(_pyTB); if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero) { string type; string message; Runtime.XIncref(_pyType); using (var pyType = new PyObject(_pyType)) using (PyObject pyTypeName = pyType.GetAttr("__name__")) { type = pyTypeName.ToString(); } _pythonTypeName = type; Runtime.XIncref(_pyValue); using (var pyValue = new PyObject(_pyValue)) { message = pyValue.ToString(); } _message = type + " : " + message; } if (_pyTB != IntPtr.Zero) { PyObject tb_module = PythonEngine.ImportModule("traceback"); Runtime.XIncref(_pyTB); using (var pyTB = new PyObject(_pyTB)) { _tb = tb_module.InvokeMethod("format_tb", pyTB).ToString(); } } PythonEngine.ReleaseLock(gs); }