PyEval_GetGlobals() private method

private PyEval_GetGlobals ( ) : IntPtr
return IntPtr
Esempio n. 1
0
        /// <summary>
        /// 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>
        public static PyObject RunString(
            string code, IntPtr?globals = null, IntPtr?locals = null
            )
        {
            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;
                }
            }

            var borrowedLocals = true;

            if (locals == null)
            {
                locals         = Runtime.PyDict_New();
                borrowedLocals = false;
            }

            var flag = (IntPtr)257; /* Py_file_input */

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

                Py.Throw();

                return(new PyObject(result));
            }
            finally
            {
                if (!borrowedLocals)
                {
                    Runtime.XDecref(locals.Value);
                }
                if (!borrowedGlobals)
                {
                    Runtime.XDecref(globals.Value);
                }
            }
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 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>

        public static PyObject RunString(string code)
        {
            IntPtr globals = Runtime.PyEval_GetGlobals();
            IntPtr locals  = Runtime.PyDict_New();

            IntPtr builtins = Runtime.PyEval_GetBuiltins();

            Runtime.PyDict_SetItemString(locals, "__builtins__", builtins);

            IntPtr flag   = (IntPtr)257; /* Py_file_input */
            IntPtr result = Runtime.PyRun_String(code, flag, globals, locals);

            Runtime.Decref(locals);
            if (result == IntPtr.Zero)
            {
                return(null);
            }
            return(new PyObject(result));
        }