예제 #1
0
        /// <summary>
        /// Eval method
        /// </summary>
        /// <remarks>
        /// Evaluate a Python expression and return the result as a PyObject
        /// or null if an exception is raised.
        /// </remarks>
        public PyObject Eval(string code, PyDict locals = null)
        {
            Check();
            BorrowedReference _locals = locals == null ? VarsRef : locals.Reference;

            NewReference reference = Runtime.PyRun_String(
                code, RunFlagType.Eval, VarsRef, _locals
                );

            PythonException.ThrowIfIsNull(reference);
            return(reference.MoveToPyObject());
        }
예제 #2
0
파일: pyscope.cs 프로젝트: zjuice/pythonnet
        /// <summary>
        /// Eval method
        /// </summary>
        /// <remarks>
        /// Evaluate a Python expression and return the result as a PyObject
        /// or null if an exception is raised.
        /// </remarks>
        public PyObject Eval(string code, PyDict locals = null)
        {
            Check();
            IntPtr _locals = locals == null ? variables : locals.obj;

            NewReference reference = Runtime.PyRun_String(
                code, RunFlagType.Eval, variables, _locals
                );

            PythonException.ThrowIfIsNull(reference);
            return(reference.MoveToPyObject());
        }
예제 #3
0
        /// <summary>
        /// Internal RunString Method.
        /// </summary>
        /// <remarks>
        /// Run a string containing Python code. Returns the result of
        /// executing the code string as a PyObject instance, or null if
        /// an exception was raised.
        /// </remarks>
        internal static PyObject RunString(string code, IntPtr?globals, IntPtr?locals, RunFlagType flag)
        {
            var borrowedGlobals = true;

            if (globals == null)
            {
                globals = Runtime.PyEval_GetGlobals();
                if (globals == IntPtr.Zero)
                {
                    globals = Runtime.PyDict_New();
                    Runtime.PyDict_SetItemString(
                        globals.Value, "__builtins__",
                        Runtime.PyEval_GetBuiltins()
                        );
                    borrowedGlobals = false;
                }
            }

            if (locals == null)
            {
                locals = globals;
            }

            try
            {
                NewReference result = Runtime.PyRun_String(
                    code, (IntPtr)flag, globals.Value, locals.Value
                    );

                try
                {
                    Runtime.CheckExceptionOccurred();

                    return(result.MoveToPyObject());
                }
                finally
                {
                    result.Dispose();
                }
            }
            finally
            {
                if (!borrowedGlobals)
                {
                    Runtime.XDecref(globals.Value);
                }
            }
        }
예제 #4
0
        public bool MoveNext()
        {
            NewReference next = Runtime.PyIter_Next(Reference);

            if (next.IsNull())
            {
                if (Exceptions.ErrorOccurred())
                {
                    throw new PythonException();
                }

                // stop holding the previous object, if there was one
                _current = null;
                return(false);
            }

            _current = next.MoveToPyObject();
            return(true);
        }
예제 #5
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <remarks>
        /// Create a scope based on a Python Module.
        /// </remarks>
        internal PyScope(ref NewReference ptr, PyScopeManager manager)
        {
            if (!Runtime.PyType_IsSubtype(Runtime.PyObject_TYPE(ptr), Runtime.PyModuleType))
            {
                throw new PyScopeException("object is not a module");
            }
            Manager = manager ?? PyScopeManager.Global;
            obj     = ptr.MoveToPyObject();
            //Refcount of the variables not increase
            variables = Runtime.PyModule_GetDict(Reference).DangerousGetAddress();
            PythonException.ThrowIfIsNull(variables);

            int res = Runtime.PyDict_SetItem(
                VarsRef, PyIdentifier.__builtins__,
                Runtime.PyEval_GetBuiltins()
                );

            PythonException.ThrowIfIsNotZero(res);
            this.Name = this.Get <string>("__name__");
        }
예제 #6
0
        /// <summary>
        /// Eval method
        /// </summary>
        /// <remarks>
        /// Evaluate a Python expression and return the result as a PyObject
        /// or null if an exception is raised.
        /// </remarks>
        public PyObject Eval(string code, PyDict locals = null)
        {
            Check();
            IntPtr _locals = locals == null ? variables : locals.obj;
            var    flag    = (IntPtr)Runtime.Py_eval_input;

            NewReference reference = Runtime.PyRun_String(
                code, flag, variables, _locals
                );

            try
            {
                Runtime.CheckExceptionOccurred();
                return(reference.MoveToPyObject());
            }
            finally
            {
                reference.Dispose();
            }
        }