示例#1
0
        public void TestNoError()
        {
            // There is no PyErr to fetch
            Assert.Throws <InvalidOperationException>(() => PythonException.FetchCurrentRaw());
            var currentError = PythonException.FetchCurrentOrNullRaw();

            Assert.IsNull(currentError);
        }
示例#2
0
        public void TestPythonException_Normalize_ThrowsWhenErrorSet()
        {
            Exceptions.SetError(Exceptions.TypeError, "Error!");
            var pythonException = PythonException.FetchCurrentRaw();

            Exceptions.SetError(Exceptions.TypeError, "Another error");
            Assert.Throws <InvalidOperationException>(() => pythonException.Normalize());
            Exceptions.Clear();
        }
示例#3
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

            Exception       ex      = null;
            PythonException pyError = null;

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

                Body(enterResult);
            }
            catch (PythonException e)
            {
                ex = pyError = e;
            }
            catch (Exception e)
            {
                ex = e;
                Exceptions.SetError(e);
                pyError = PythonException.FetchCurrentRaw();
            }

            PyObject type      = pyError?.Type ?? PyObject.None;
            PyObject val       = pyError?.Value ?? PyObject.None;
            PyObject traceBack = pyError?.Traceback ?? PyObject.None;

            var exitResult = obj.InvokeMethod("__exit__", type, val, traceBack);

            if (ex != null && !exitResult.IsTrue())
            {
                throw ex;
            }
        }