Beispiel #1
0
        public PythonException()
        {
            IntPtr gs = PythonEngine.AcquireLock();

            Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _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)
            {
                using (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);
        }
Beispiel #2
0
        /// <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);
            }
        }
Beispiel #3
0
        /// <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);
            }
        }
Beispiel #4
0
 public static PyObject Import(string name)
 {
     return(PythonEngine.ImportModule(name));
 }