Пример #1
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);

            Py.Throw();
            return(new PyObject(op));
        }
Пример #2
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);

            Py.Throw();
            return(new PyObject(op));
        }
Пример #3
0
 public static void SetArgv(IEnumerable <string> argv)
 {
     using (GIL())
     {
         string[] arr = argv.ToArray();
         Runtime.PySys_SetArgvEx(arr.Length, arr, 0);
         Py.Throw();
     }
 }
Пример #4
0
        /// <summary>
        /// ModuleFromString Method
        /// </summary>
        /// <remarks>
        /// Given a string module name and a string containing Python code,
        /// execute the code in and return a module of the given name.
        /// </remarks>
        public static PyObject ModuleFromString(string name, string code)
        {
            IntPtr c = Runtime.Py_CompileString(code, "none", (IntPtr)257);

            Py.Throw();
            IntPtr m = Runtime.PyImport_ExecCodeModule(name, c);

            Py.Throw();
            return(new PyObject(m));
        }
Пример #5
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);
                }
            }
        }