PyErr_Fetch() private method

private PyErr_Fetch ( IntPtr &ob, IntPtr &val, IntPtr &tb ) : void
ob IntPtr
val IntPtr
tb IntPtr
return void
Exemplo n.º 1
0
 public PythonException() : base()
 {
     Runtime.PyErr_Fetch(ref excType, ref excValue, ref excTb);
     Runtime.Incref(excType);
     Runtime.Incref(excValue);
     Runtime.Incref(excTb);
 }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }