public static ImportModule ( string name ) : |
||
name | string | |
return |
/// <summary> /// ImportAll Method /// </summary> /// <remarks> /// The 'import * from ..' statement in Python. /// Import all content of a scope/module of given name into the scope, scope will be looked up first. /// </remarks> public void ImportAll(string name) { PyScope scope; Manager.TryGet(name, out scope); if (scope != null) { ImportAll(scope); return; } else { PyObject module = PythonEngine.ImportModule(name); ImportAll(module); } }
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 PyObject tb_module = PythonEngine.ImportModule("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); }
/// <summary> /// Formats this PythonException object into a message as would be printed /// out via the Python console. See traceback.format_exception /// </summary> public string Format() { string res; IntPtr gs = PythonEngine.AcquireLock(); try { if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero) { IntPtr tb = _pyTB; IntPtr type = _pyType; IntPtr value = _pyValue; Runtime.XIncref(type); Runtime.XIncref(value); Runtime.XIncref(tb); Runtime.PyErr_NormalizeException(ref type, ref value, ref tb); using (PyObject pyType = new PyObject(type)) using (PyObject pyValue = new PyObject(value)) using (PyObject pyTB = new PyObject(tb)) using (PyObject tb_mod = PythonEngine.ImportModule("traceback")) { 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(); } } else { res = StackTrace; } } finally { PythonEngine.ReleaseLock(gs); } return(res); }
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); }
/// <summary> /// Import method /// </summary> /// <remarks> /// Import a scope or a module of given name, /// scope will be looked up first. /// </remarks> public dynamic Import(string name, string asname = null) { Check(); if (String.IsNullOrEmpty(asname)) { asname = name; } PyScope scope; Manager.TryGet(name, out scope); if (scope != null) { Import(scope, asname); return(scope); } else { PyObject module = PythonEngine.ImportModule(name); Import(module, asname); return(module); } }
public PythonException() { IntPtr gs = PythonEngine.AcquireLock(); Runtime.Interop.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); }
public static PyObject Import(string name) { return(PythonEngine.ImportModule(name)); }