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

            Assert.IsNull(currentError);
        }
Esempio n. 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();
        }
Esempio n. 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;
            }
        }