Provides a managed interface to exceptions thrown by the Python runtime.
Inheritance: System.Exception
示例#1
0
 /// <summary>
 /// PyList Constructor
 /// </summary>
 /// <remarks>
 /// Creates a new empty Python list object.
 /// </remarks>
 public PyList() : base(Runtime.PyList_New(0))
 {
     if (obj == IntPtr.Zero)
     {
         throw PythonException.ThrowLastAsClrException();
     }
 }
示例#2
0
        protected override void OnSave(InterDomainContext context)
        {
            base.OnSave(context);
            System.Diagnostics.Debug.Assert(dict == GetObjectDict(pyHandle));
            foreach (var attr in cache.Values)
            {
                Runtime.XIncref(attr.pyHandle);
            }
            // Decref twice in tp_clear, equilibrate them.
            Runtime.XIncref(dict);
            Runtime.XIncref(dict);
            // destroy the cache(s)
            foreach (var pair in cache)
            {
                if ((Runtime.PyDict_DelItemString(DictRef, pair.Key) == -1) &&
                    (Exceptions.ExceptionMatches(Exceptions.KeyError)))
                {
                    // Trying to remove a key that's not in the dictionary
                    // raises an error. We don't care about it.
                    Runtime.PyErr_Clear();
                }
                else if (Exceptions.ErrorOccurred())
                {
                    throw PythonException.ThrowLastAsClrException();
                }
                pair.Value.DecrRefCount();
            }

            cache.Clear();
        }
示例#3
0
        /// <summary>
        /// AsTuple Method
        /// </summary>
        /// <remarks>
        /// Convert a Python object to a Python tuple if possible, raising
        /// a PythonException if the conversion is not possible. This is
        /// equivalent to the Python expression "tuple(object)".
        /// </remarks>
        public static PyTuple AsTuple(PyObject value)
        {
            IntPtr op = Runtime.PySequence_Tuple(value.obj);

            PythonException.ThrowIfIsNull(op);
            return(new PyTuple(op));
        }
示例#4
0
        private static IntPtr FromString(string s)
        {
            IntPtr val = Runtime.PyString_FromString(s);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#5
0
        private static IntPtr FromDouble(double value)
        {
            IntPtr val = Runtime.PyLong_FromDouble(value);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#6
0
        /// <summary>
        /// Reloads the module, and returns the updated object
        /// </summary>
        public PyModule Reload()
        {
            NewReference op = Runtime.PyImport_ReloadModule(this.Reference);

            PythonException.ThrowIfIsNull(op);
            return(new PyModule(ref op));
        }
示例#7
0
 /// <summary>
 /// Shortcut for (pointer == NULL) -&gt; throw PythonException
 /// </summary>
 /// <param name="pointer">Pointer to a Python object</param>
 internal static void ErrorCheck(BorrowedReference pointer)
 {
     if (pointer.IsNull)
     {
         throw PythonException.ThrowLastAsClrException();
     }
 }
示例#8
0
        /// <summary>
        /// Set the 'args' slot on a python exception object that wraps
        /// a CLR exception. This is needed for pickling CLR exceptions as
        /// BaseException_reduce will only check the slots, bypassing the
        /// __getattr__ implementation, and thus dereferencing a NULL
        /// pointer.
        /// </summary>
        internal static void SetArgsAndCause(BorrowedReference ob, Exception e)
        {
            IntPtr args;

            if (!string.IsNullOrEmpty(e.Message))
            {
                args = Runtime.PyTuple_New(1);
                IntPtr msg = Runtime.PyString_FromString(e.Message);
                Runtime.PyTuple_SetItem(args, 0, msg);
            }
            else
            {
                args = Runtime.PyTuple_New(0);
            }

            using var argsTuple = NewReference.DangerousFromPointer(args);

            if (Runtime.PyObject_SetAttrString(ob, "args", argsTuple) != 0)
            {
                throw PythonException.ThrowLastAsClrException();
            }

            if (e.InnerException != null)
            {
                // Note: For an AggregateException, InnerException is only the first of the InnerExceptions.
                using var cause = CLRObject.GetReference(e.InnerException);
                Runtime.PyException_SetCause(ob, cause.Steal());
            }
        }
示例#9
0
 /// <summary>
 /// Shortcut for (pointer == NULL or ErrorOccurred()) -&gt; throw PythonException
 /// </summary>
 internal static void ErrorOccurredCheck(IntPtr pointer)
 {
     if (pointer == IntPtr.Zero || ErrorOccurred())
     {
         throw PythonException.ThrowLastAsClrException();
     }
 }
示例#10
0
        private static IntPtr FromLong(long value)
        {
            IntPtr val = Runtime.PyInt_FromInt64(value);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#11
0
        private static IntPtr FromInt(int value)
        {
            IntPtr val = Runtime.PyLong_FromLong(value);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#12
0
 private void Exec(string code, BorrowedReference _globals, BorrowedReference _locals)
 {
     using NewReference reference = Runtime.PyRun_String(
               code, RunFlagType.File, _globals, _locals
               );
     PythonException.ThrowIfIsNull(reference);
 }
示例#13
0
        private static IntPtr FromULong(ulong value)
        {
            IntPtr val = Runtime.PyLong_FromUnsignedLongLong(value);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#14
0
        static void SetupImportHook()
        {
            // Create the import hook module
            using var import_hook_module = Runtime.PyModule_New("clr.loader");
            BorrowedReference mod_dict = Runtime.PyModule_GetDict(import_hook_module.BorrowOrThrow());

            Debug.Assert(mod_dict != null);

            // Run the python code to create the module's classes.
            var builtins = Runtime.PyEval_GetBuiltins();
            var exec     = Runtime.PyDict_GetItemString(builtins, "exec");

            using var args = Runtime.PyTuple_New(2);
            PythonException.ThrowIfIsNull(args);
            using var codeStr = Runtime.PyString_FromString(LoaderCode);
            Runtime.PyTuple_SetItem(args.Borrow(), 0, codeStr.StealOrThrow());

            // reference not stolen due to overload incref'ing for us.
            Runtime.PyTuple_SetItem(args.Borrow(), 1, mod_dict);
            Runtime.PyObject_Call(exec, args.Borrow(), default).Dispose();
            // Set as a sub-module of clr.
            if (Runtime.PyModule_AddObject(ClrModuleReference, "loader", import_hook_module.Steal()) != 0)
            {
                throw PythonException.ThrowLastAsClrException();
            }

            // Finally, add the hook to the meta path
            var findercls = Runtime.PyDict_GetItemString(mod_dict, "DotNetFinder");

            using var finderCtorArgs = Runtime.PyTuple_New(0);
            using var finder_inst    = Runtime.PyObject_CallObject(findercls, finderCtorArgs.Borrow());
            var metapath = Runtime.PySys_GetObject("meta_path");

            PythonException.ThrowIfIsNotZero(Runtime.PyList_Append(metapath, finder_inst.BorrowOrThrow()));
        }
示例#15
0
        private static IntPtr FromString(string s)
        {
            IntPtr val = Runtime.PyUnicode_FromUnicode(s, s.Length);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#16
0
        static void SetupImportHook()
        {
            // Create the import hook module
            var import_hook_module = Runtime.PyModule_New("clr.loader");

            // Run the python code to create the module's classes.
            var builtins = Runtime.PyEval_GetBuiltins();
            var exec     = Runtime.PyDict_GetItemString(builtins, "exec");

            using var args = NewReference.DangerousFromPointer(Runtime.PyTuple_New(2));

            var codeStr = NewReference.DangerousFromPointer(Runtime.PyString_FromString(LoaderCode));

            Runtime.PyTuple_SetItem(args, 0, codeStr);
            var mod_dict = Runtime.PyModule_GetDict(import_hook_module);

            // reference not stolen due to overload incref'ing for us.
            Runtime.PyTuple_SetItem(args, 1, mod_dict);
            Runtime.PyObject_Call(exec, args, default).Dispose();
            // Set as a sub-module of clr.
            if (Runtime.PyModule_AddObject(ClrModuleReference, "loader", import_hook_module.DangerousGetAddress()) != 0)
            {
                Runtime.XDecref(import_hook_module.DangerousGetAddress());
                throw PythonException.ThrowLastAsClrException();
            }

            // Finally, add the hook to the meta path
            var findercls      = Runtime.PyDict_GetItemString(mod_dict, "DotNetFinder");
            var finderCtorArgs = NewReference.DangerousFromPointer(Runtime.PyTuple_New(0));
            var finder_inst    = Runtime.PyObject_CallObject(findercls, finderCtorArgs);
            var metapath       = Runtime.PySys_GetObject("meta_path");

            Runtime.PyList_Append(metapath, finder_inst);
        }
示例#17
0
        private static IntPtr FromString(string value)
        {
            IntPtr val = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#18
0
        /// <summary>
        /// AsLong Method
        /// </summary>
        /// <remarks>
        /// Convert a Python object to a Python long if possible, raising
        /// a PythonException if the conversion is not possible. This is
        /// equivalent to the Python expression "long(object)".
        /// </remarks>
        public static PyLong AsLong(PyObject value)
        {
            IntPtr op = Runtime.PyNumber_Long(value.obj);

            PythonException.ThrowIfIsNull(op);
            return(new PyLong(op));
        }
示例#19
0
 /// <summary>
 /// TryGet Method
 /// </summary>
 /// <remarks>
 /// Returns the value of the variable, local variable first.
 /// If the variable does not exist, return null.
 /// </remarks>
 public bool TryGet(string name, out PyObject value)
 {
     Check();
     using (var pyKey = new PyString(name))
     {
         if (Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0)
         {
             IntPtr op = Runtime.PyObject_GetItem(variables, pyKey.obj);
             if (op == IntPtr.Zero)
             {
                 throw PythonException.ThrowLastAsClrException();
             }
             if (op == Runtime.PyNone)
             {
                 Runtime.XDecref(op);
                 value = null;
                 return(true);
             }
             value = new PyObject(op);
             return(true);
         }
         else
         {
             value = null;
             return(false);
         }
     }
 }
示例#20
0
        /// <summary>
        /// AsFloat Method
        /// </summary>
        /// <remarks>
        /// Convert a Python object to a Python float if possible, raising
        /// a PythonException if the conversion is not possible. This is
        /// equivalent to the Python expression "float(object)".
        /// </remarks>
        public static PyFloat AsFloat(PyObject value)
        {
            IntPtr op = Runtime.PyNumber_Float(value.obj);

            PythonException.ThrowIfIsNull(op);
            return(new PyFloat(op));
        }
示例#21
0
        unsafe internal PyBuffer(PyObject exporter, PyBUF flags)
        {
            _view = new Py_buffer();

            if (Runtime.PyObject_GetBuffer(exporter.Handle, ref _view, (int)flags) < 0)
            {
                throw PythonException.ThrowLastAsClrException();
            }

            _exporter = exporter;

            var intPtrBuf = new IntPtr[_view.ndim];

            if (_view.shape != IntPtr.Zero)
            {
                Marshal.Copy(_view.shape, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr));
                Shape = intPtrBuf.Select(x => (long)x).ToArray();
            }

            if (_view.strides != IntPtr.Zero)
            {
                Marshal.Copy(_view.strides, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr));
                Strides = intPtrBuf.Select(x => (long)x).ToArray();
            }

            if (_view.suboffsets != IntPtr.Zero)
            {
                Marshal.Copy(_view.suboffsets, intPtrBuf, 0, (int)_view.len * sizeof(IntPtr));
                SubOffsets = intPtrBuf.Select(x => (long)x).ToArray();
            }
        }
示例#22
0
        /// <summary>
        /// ReloadModule Method
        /// </summary>
        /// <remarks>
        /// Given a PyObject representing a previously loaded module, reload
        /// the module.
        /// </remarks>
        public static PyObject ReloadModule(PyObject module)
        {
            IntPtr op = Runtime.PyImport_ReloadModule(module.Handle);

            PythonException.ThrowIfIsNull(op);
            return(new PyObject(op));
        }
示例#23
0
        internal static IntPtr FromLong(long value)
        {
            IntPtr val = Runtime.PyLong_FromLongLong(value);

            PythonException.ThrowIfIsNull(val);
            return(val);
        }
示例#24
0
        /// <summary>
        /// Given a module or package name, import the
        /// module and return the resulting module object as a <see cref="PyModule"/>.
        /// </summary>
        /// <param name="name">Fully-qualified module or package name</param>
        public static PyModule Import(string name)
        {
            NewReference op = Runtime.PyImport_ImportModule(name);

            PythonException.ThrowIfIsNull(op);
            return(new PyModule(ref op));
        }
示例#25
0
        /// <summary>
        /// ImportModule Method
        /// </summary>
        /// <remarks>
        /// Given a fully-qualified module or package name, import the
        /// module and return the resulting module object as a PyObject
        /// or null if an exception is raised.
        /// </remarks>
        public static PyObject ImportModule(string name)
        {
            IntPtr op = Runtime.PyImport_ImportModule(name);

            PythonException.ThrowIfIsNull(op);
            return(new PyObject(op));
        }
示例#26
0
        /// <summary>
        /// SetError Method
        /// </summary>
        ///
        /// <remarks>
        /// Sets the current Python exception given a CLR exception
        /// object. The CLR exception instance is wrapped as a Python
        /// object, allowing it to be handled naturally from Python.
        /// </remarks>

        public static void SetError(Exception e)
        {
            // Because delegates allow arbitrary nestings of Python calling
            // managed calling Python calling... etc. it is possible that we
            // might get a managed exception raised that is a wrapper for a
            // Python exception. In that case we'd rather have the real thing.

            PythonException pe = e as PythonException;

            if (pe != null)
            {
                Runtime.PyErr_SetObject(pe.PyType, pe.PyValue);
                return;
            }

            IntPtr op = CLRObject.GetInstHandle(e);

            // XXX - hack to raise a compatible old-style exception ;(
            if (Runtime.wrap_exceptions)
            {
                op = GetExceptionInstanceWrapper(op);
            }
            IntPtr etype = Runtime.PyObject_GetAttrString(op, "__class__");

            Runtime.PyErr_SetObject(etype, op);
            Runtime.Decref(etype);
            Runtime.Decref(op);
        }
示例#27
0
        public static void With(PyObject obj, Action <dynamic> Body)
        {
            // Behavior described here:
            // https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers

            IntPtr          type      = Runtime.PyNone;
            IntPtr          val       = Runtime.PyNone;
            IntPtr          traceBack = Runtime.PyNone;
            PythonException ex        = null;

            try
            {
                PyObject enterResult = obj.InvokeMethod("__enter__");

                Body(enterResult);
            }
            catch (PythonException e)
            {
                ex        = e;
                type      = ex.PyType.Coalesce(type);
                val       = ex.PyValue.Coalesce(val);
                traceBack = ex.PyTB.Coalesce(traceBack);
            }

            Runtime.XIncref(type);
            Runtime.XIncref(val);
            Runtime.XIncref(traceBack);
            var exitResult = obj.InvokeMethod("__exit__", new PyObject(type), new PyObject(val), new PyObject(traceBack));

            if (ex != null && !exitResult.IsTrue())
            {
                throw ex;
            }
        }
示例#28
0
        /// <summary>
        /// Utility method to allocate a type object &amp; do basic initialization.
        /// </summary>
        internal static IntPtr AllocateTypeObject(string name, IntPtr metatype)
        {
            IntPtr type = Runtime.PyType_GenericAlloc(metatype, 0);

            PythonException.ThrowIfIsNull(type);
            // Clr type would not use __slots__,
            // and the PyMemberDef after PyHeapTypeObject will have other uses(e.g. type handle),
            // thus set the ob_size to 0 for avoiding slots iterations.
            Marshal.WriteIntPtr(type, TypeOffset.ob_size, IntPtr.Zero);

            // Cheat a little: we'll set tp_name to the internal char * of
            // the Python version of the type name - otherwise we'd have to
            // allocate the tp_name and would have no way to free it.
            IntPtr temp = Runtime.PyUnicode_FromString(name);
            IntPtr raw  = Runtime.PyUnicode_AsUTF8(temp);

            Marshal.WriteIntPtr(type, TypeOffset.tp_name, raw);
            Marshal.WriteIntPtr(type, TypeOffset.name, temp);

            Runtime.XIncref(temp);
            Marshal.WriteIntPtr(type, TypeOffset.qualname, temp);
            temp = type + TypeOffset.nb_add;
            Marshal.WriteIntPtr(type, TypeOffset.tp_as_number, temp);

            temp = type + TypeOffset.sq_length;
            Marshal.WriteIntPtr(type, TypeOffset.tp_as_sequence, temp);

            temp = type + TypeOffset.mp_length;
            Marshal.WriteIntPtr(type, TypeOffset.tp_as_mapping, temp);

            temp = type + TypeOffset.bf_getbuffer;
            Marshal.WriteIntPtr(type, TypeOffset.tp_as_buffer, temp);
            return(type);
        }
示例#29
0
        public static PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File)
        {
            var    flag = (int)mode;
            IntPtr ptr  = Runtime.Py_CompileString(code, filename, flag);

            PythonException.ThrowIfIsNull(ptr);
            return(new PyObject(ptr));
        }
示例#30
0
 internal static IntPtr ErrorCheckIfNull(IntPtr pointer)
 {
     if (pointer == IntPtr.Zero && ErrorOccurred())
     {
         throw PythonException.ThrowLastAsClrException();
     }
     return(pointer);
 }
 public void TestNoError()
 {
     PythonException e = new PythonException(); //There is no PyErr to fetch
     Assert.AreEqual("", e.Message);
 }
示例#32
0
        public object TrueDispatch(ArrayList args) {
            MethodInfo method = dtype.GetMethod("Invoke");
            ParameterInfo[] pi = method.GetParameters();
            IntPtr pyargs = Runtime.PyTuple_New(pi.Length);
            Type rtype = method.ReturnType;

            for (int i = 0; i < pi.Length; i++) {
                // Here we own the reference to the Python value, and we
                // give the ownership to the arg tuple.
                IntPtr arg = Converter.ToPython(args[i], pi[i].ParameterType);
                Runtime.PyTuple_SetItem(pyargs, i, arg);
            }

            IntPtr op = Runtime.PyObject_Call(target, pyargs, IntPtr.Zero);
            Runtime.Decref(pyargs);

            if (op == IntPtr.Zero) {
                PythonException e = new PythonException();
                throw e;
            }

            if (rtype == typeof(void)) {
                return null;
            }

            Object result = null;
            if (!Converter.ToManaged(op, rtype, out result, false)) {
                string s = "could not convert Python result to " +
                           rtype.ToString();
                Runtime.Decref(op);
                throw new ConversionException(s);
            }

            Runtime.Decref(op);
            return result;
        }