Exemplo n.º 1
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
            {
                var module = PyModule.Import(name);
                ImportAll(module);
            }
        }
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
        /// <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 = PyModule.Import("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);
        }
Exemplo n.º 4
0
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

        /// <summary>
        /// Initialization performed on startup of the Python runtime.
        /// </summary>
        internal static void Initialize()
        {
            string exceptionsModuleName = "builtins";
            exceptions_module = PyModule.Import(exceptionsModuleName);
            warnings_module = PyModule.Import("warnings");
            Type type = typeof(Exceptions);
            foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                using var op = Runtime.PyObject_GetAttrString(exceptions_module.obj, fi.Name);
                if ([email protected]())
                {
                    fi.SetValue(type, op.MoveToPyObject());
                }
                else
                {
                    fi.SetValue(type, null);
                    DebugUtil.Print($"Unknown exception: {fi.Name}");
                }
            }
            Runtime.PyErr_Clear();
        }
Exemplo n.º 5
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
            {
                var module = PyModule.Import(name);
                Import(module, asname);
                return(module);
            }
        }