PyEval_GetBuiltins() private méthode

private PyEval_GetBuiltins ( ) : IntPtr
Résultat IntPtr
Exemple #1
0
        public static IntPtr CreateObjectType()
        {
            IntPtr globals = Runtime.PyDict_New();

            if (Runtime.PyDict_SetItemString(globals, "__builtins__", Runtime.PyEval_GetBuiltins()) != 0)
            {
                Runtime.XDecref(globals);
                throw new PythonException();
            }
            const string code   = "class A(object): pass";
            var          resRef = Runtime.PyRun_String(code, RunFlagType.File, globals, globals);
            IntPtr       res    = resRef.DangerousGetAddress();

            if (res == IntPtr.Zero)
            {
                try
                {
                    throw new PythonException();
                }
                finally
                {
                    Runtime.XDecref(globals);
                }
            }
            resRef.Dispose();
            IntPtr A = Runtime.PyDict_GetItemString(globals, "A");

            Debug.Assert(A != IntPtr.Zero);
            Runtime.XIncref(A);
            Runtime.XDecref(globals);
            return(A);
        }
Exemple #2
0
        static void SetupImportHook()
        {
            // Create the import hook module
            var import_hook_module = Runtime.PyModule_New("clr.loader");

            // Run the python code to create the module's classes.
            var builtins = Runtime.PyEval_GetBuiltins();
            var exec     = Runtime.PyDict_GetItemString(builtins, "exec");

            using var args = NewReference.DangerousFromPointer(Runtime.PyTuple_New(2));

            var codeStr = NewReference.DangerousFromPointer(Runtime.PyString_FromString(LoaderCode));

            Runtime.PyTuple_SetItem(args, 0, codeStr);
            var mod_dict = Runtime.PyModule_GetDict(import_hook_module);

            // reference not stolen due to overload incref'ing for us.
            Runtime.PyTuple_SetItem(args, 1, mod_dict);
            Runtime.PyObject_Call(exec, args, default).Dispose();
            // Set as a sub-module of clr.
            if (Runtime.PyModule_AddObject(ClrModuleReference, "loader", import_hook_module.DangerousGetAddress()) != 0)
            {
                Runtime.XDecref(import_hook_module.DangerousGetAddress());
                throw PythonException.ThrowLastAsClrException();
            }

            // Finally, add the hook to the meta path
            var findercls      = Runtime.PyDict_GetItemString(mod_dict, "DotNetFinder");
            var finderCtorArgs = NewReference.DangerousFromPointer(Runtime.PyTuple_New(0));
            var finder_inst    = Runtime.PyObject_CallObject(findercls, finderCtorArgs);
            var metapath       = Runtime.PySys_GetObject("meta_path");

            Runtime.PyList_Append(metapath, finder_inst);
        }
Exemple #3
0
        static void SetupImportHook()
        {
            // Create the import hook module
            using var import_hook_module = Runtime.PyModule_New("clr.loader");
            BorrowedReference mod_dict = Runtime.PyModule_GetDict(import_hook_module.BorrowOrThrow());

            Debug.Assert(mod_dict != null);

            // Run the python code to create the module's classes.
            var builtins = Runtime.PyEval_GetBuiltins();
            var exec     = Runtime.PyDict_GetItemString(builtins, "exec");

            using var args = Runtime.PyTuple_New(2);
            PythonException.ThrowIfIsNull(args);
            using var codeStr = Runtime.PyString_FromString(LoaderCode);
            Runtime.PyTuple_SetItem(args.Borrow(), 0, codeStr.StealOrThrow());

            // reference not stolen due to overload incref'ing for us.
            Runtime.PyTuple_SetItem(args.Borrow(), 1, mod_dict);
            Runtime.PyObject_Call(exec, args.Borrow(), default).Dispose();
            // Set as a sub-module of clr.
            if (Runtime.PyModule_AddObject(ClrModuleReference, "loader", import_hook_module.Steal()) != 0)
            {
                throw PythonException.ThrowLastAsClrException();
            }

            // Finally, add the hook to the meta path
            var findercls = Runtime.PyDict_GetItemString(mod_dict, "DotNetFinder");

            using var finderCtorArgs = Runtime.PyTuple_New(0);
            using var finder_inst    = Runtime.PyObject_CallObject(findercls, finderCtorArgs.Borrow());
            var metapath = Runtime.PySys_GetObject("meta_path");

            PythonException.ThrowIfIsNotZero(Runtime.PyList_Append(metapath, finder_inst.BorrowOrThrow()));
        }
Exemple #4
0
        internal static void SetupExceptionHack()
        {
            ns_exc = ClassManager.GetClass(typeof(Exception)).pyHandle;
            cache  = new Hashtable();

            string code =
                "import exceptions\n" +
                "class Exception(exceptions.Exception):\n" +
                "    _class = None\n" +
                "    _inner = None\n" +
                "\n" +
                "    def __init__(self, *args, **kw):\n" +
                "        inst = self.__class__._class(*args, **kw)\n" +
                "        self.__dict__['_inner'] = inst\n" +
                "        exceptions.Exception.__init__(self, *args, **kw)\n" +
                "\n" +
                "    def __getattr__(self, name, _marker=[]):\n" +
                "        inner = self.__dict__['_inner']\n" +
                "        v = getattr(inner, name, _marker)\n" +
                "        if v is not _marker:\n" +
                "            return v\n" +
                "        v = self.__dict__.get(name, _marker)\n" +
                "        if v is not _marker:\n" +
                "            return v\n" +
                "        raise AttributeError(name)\n" +
                "\n" +
                "    def __setattr__(self, name, value):\n" +
                "        inner = self.__dict__['_inner']\n" +
                "        setattr(inner, name, value)\n" +
                "\n" +
                "    def __str__(self):\n" +
                "        inner = self.__dict__.get('_inner')\n" +
                "        msg = getattr(inner, 'Message', '')\n" +
                "        st = getattr(inner, 'StackTrace', '')\n" +
                "        st = st and '\\n' + st or ''\n" +
                "        return msg + st\n" +
                "\n";

            IntPtr dict = Runtime.PyDict_New();

            IntPtr builtins = Runtime.PyEval_GetBuiltins();

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

            IntPtr namestr = Runtime.PyString_FromString("System");

            Runtime.PyDict_SetItemString(dict, "__name__", namestr);
            Runtime.Decref(namestr);

            Runtime.PyDict_SetItemString(dict, "__file__", Runtime.PyNone);
            Runtime.PyDict_SetItemString(dict, "__doc__", Runtime.PyNone);

            IntPtr flag = Runtime.Py_file_input;
            IntPtr done = Runtime.PyRun_String(code, flag, dict, dict);

            os_exc = Runtime.PyDict_GetItemString(dict, "Exception");
            Runtime.PyObject_SetAttrString(os_exc, "_class", ns_exc);
            Runtime.PyErr_Clear();
        }
Exemple #5
0
        private void SetupBuiltins(ref ScriptRuntime runtime)
        {
            // get builtins
            IntPtr builtins = CpyRuntime.PyEval_GetBuiltins();

            // Add timestamp and executuin uuid
            SetVariable(builtins, "__execid__", runtime.ExecId);
            SetVariable(builtins, "__timestamp__", runtime.ExecTimestamp);

            // set builtins
            SetVariable(builtins, "__cachedengine__", RecoveredFromCache);
            SetVariable(builtins, "__cachedengineid__", TypeId);
            SetVariable(builtins, "__scriptruntime__", runtime);

            if (runtime.UIApp != null)
            {
                SetVariable(builtins, "__revit__", runtime.UIApp);
            }
            else if (runtime.UIControlledApp != null)
            {
                SetVariable(builtins, "__revit__", runtime.UIControlledApp);
            }
            else if (runtime.App != null)
            {
                SetVariable(builtins, "__revit__", runtime.App);
            }
            else
            {
                SetVariable(builtins, "__revit__", null);
            }

            // Adding data provided by IExternalCommand.Execute
            SetVariable(builtins, "__commanddata__", runtime.ScriptRuntimeConfigs.CommandData);
            SetVariable(builtins, "__elements__", runtime.ScriptRuntimeConfigs.SelectedElements);

            // Add ui button handle
            SetVariable(builtins, "__uibutton__", runtime.UIControl);

            // Adding information on the command being executed
            SetVariable(builtins, "__commandpath__", Path.GetDirectoryName(runtime.ScriptData.ScriptPath));
            SetVariable(builtins, "__configcommandpath__", Path.GetDirectoryName(runtime.ScriptData.ConfigScriptPath));
            SetVariable(builtins, "__commandname__", runtime.ScriptData.CommandName);
            SetVariable(builtins, "__commandbundle__", runtime.ScriptData.CommandBundle);
            SetVariable(builtins, "__commandextension__", runtime.ScriptData.CommandExtension);
            SetVariable(builtins, "__commanduniqueid__", runtime.ScriptData.CommandUniqueId);
            SetVariable(builtins, "__commandcontrolid__", runtime.ScriptData.CommandControlId);
            SetVariable(builtins, "__forceddebugmode__", runtime.ScriptRuntimeConfigs.DebugMode);
            SetVariable(builtins, "__shiftclick__", runtime.ScriptRuntimeConfigs.ConfigMode);

            // Add reference to the results dictionary
            // so the command can add custom values for logging
            SetVariable(builtins, "__result__", runtime.GetResultsDictionary());

            // EVENT HOOKS BUILTINS ----------------------------------------------------------------------------------
            // set event arguments for engine
            SetVariable(builtins, "__eventsender__", runtime.ScriptRuntimeConfigs.EventSender);
            SetVariable(builtins, "__eventargs__", runtime.ScriptRuntimeConfigs.EventArgs);
        }
            public override ValueType Execute(ValueType arg)
            {
                const string code = @"
from Python.EmbeddingTest.Domain import MyClass

def test_obj_call():
    obj = MyClass()
    obj.Method()
    obj.StaticMethod()
    obj.Property = 1
    obj.Field = 10

test_obj_call()
";
                const string name = "test_domain_reload_mod";

                using (Py.GIL())
                {
                    // Create a new module
                    IntPtr module = PyRuntime.PyModule_New(name);
                    Assert.That(module != IntPtr.Zero);
                    IntPtr globals = PyRuntime.PyObject_GetAttrString(module, "__dict__");
                    Assert.That(globals != IntPtr.Zero);
                    try
                    {
                        // import builtins
                        // module.__dict__[__builtins__] = builtins
                        int res = PyRuntime.PyDict_SetItemString(globals, "__builtins__",
                                                                 PyRuntime.PyEval_GetBuiltins());
                        PythonException.ThrowIfIsNotZero(res);

                        // Execute the code in the module's scope
                        PythonEngine.Exec(code, globals);
                        // import sys
                        // modules = sys.modules
                        IntPtr modules = PyRuntime.PyImport_GetModuleDict();
                        // modules[name] = module
                        res = PyRuntime.PyDict_SetItemString(modules, name, module);
                        PythonException.ThrowIfIsNotZero(res);
                    }
                    catch
                    {
                        PyRuntime.XDecref(module);
                        throw;
                    }
                    finally
                    {
                        PyRuntime.XDecref(globals);
                    }
                    return(module);
                }
            }
Exemple #7
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);
                }
            }
        }
        /// <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);
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <remarks>
        /// Create a scope based on a Python Module.
        /// </remarks>
        internal PyScope(IntPtr 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;
            //Refcount of the variables not increase
            variables = Runtime.PyModule_GetDict(obj);
            Runtime.CheckExceptionOccurred();

            Runtime.PyDict_SetItemString(
                variables, "__builtins__",
                Runtime.PyEval_GetBuiltins()
                );
            this.Name = this.Get <string>("__name__");
        }
Exemple #10
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));
        }
Exemple #11
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <remarks>
 /// Create a scope based on a Python Module.
 /// </remarks>
 internal PyScope(IntPtr ptr, PyScopeManager manager)
 {
     if (Runtime.PyObject_Type(ptr) != Runtime.PyModuleType)
     {
         throw new PyScopeException("object is not a module");
     }
     Manager = manager ?? PyScopeManager.Global;
     obj     = ptr;
     //Refcount of the variables not increase
     variables = Runtime.PyModule_GetDict(obj);
     if (variables == IntPtr.Zero)
     {
         throw new PythonException();
     }
     Runtime.PyDict_SetItemString(
         variables, "__builtins__",
         Runtime.PyEval_GetBuiltins()
         );
     this.Name = this.Get <string>("__name__");
 }
Exemple #12
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__");
        }
Exemple #13
0
        public static IntPtr CreateObjectType()
        {
            using var globals = NewReference.DangerousFromPointer(Runtime.PyDict_New());
            if (Runtime.PyDict_SetItemString(globals, "__builtins__", Runtime.PyEval_GetBuiltins()) != 0)
            {
                globals.Dispose();
                throw new PythonException();
            }
            const string code = "class A(object): pass";

            using var resRef = Runtime.PyRun_String(code, RunFlagType.File, globals, globals);
            if (resRef.IsNull())
            {
                globals.Dispose();
                throw new PythonException();
            }
            resRef.Dispose();
            BorrowedReference A = Runtime.PyDict_GetItemString(globals, "A");

            Debug.Assert(!A.IsNull);
            return(new NewReference(A).DangerousMoveToPointer());
        }
Exemple #14
0
        /// <summary>
        /// Initialize Method
        /// </summary>
        /// <remarks>
        /// Initialize the Python runtime. It is safe to call this method
        /// more than once, though initialization will only happen on the
        /// first call. It is *not* necessary to hold the Python global
        /// interpreter lock (GIL) to call this method.
        /// initSigs can be set to 1 to do default python signal configuration. This will override the way signals are handled by the application.
        /// </remarks>
        public static void Initialize(IEnumerable <string> args, bool setSysArgv = true, bool initSigs = false)
        {
            if (!initialized)
            {
                // Creating the delegateManager MUST happen before Runtime.Initialize
                // is called. If it happens afterwards, DelegateManager's CodeGenerator
                // throws an exception in its ctor.  This exception is eaten somehow
                // during an initial "import clr", and the world ends shortly thereafter.
                // This is probably masking some bad mojo happening somewhere in Runtime.Initialize().
                delegateManager = new DelegateManager();
                Runtime.Initialize(initSigs);
                initialized = true;
                Exceptions.Clear();

                // Make sure we clean up properly on app domain unload.
                AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;

                // Remember to shut down the runtime.
                AddShutdownHandler(Runtime.Shutdown);

                // The global scope gets used implicitly quite early on, remember
                // to clear it out when we shut down.
                AddShutdownHandler(PyScopeManager.Global.Clear);

                if (setSysArgv)
                {
                    Py.SetArgv(args);
                }

                // register the atexit callback (this doesn't use Py_AtExit as the C atexit
                // callbacks are called after python is fully finalized but the python ones
                // are called while the python engine is still running).
                string code =
                    "import atexit, clr\n" +
                    "atexit.register(clr._AtExit)\n";
                PythonEngine.Exec(code);

                // Load the clr.py resource into the clr module
                IntPtr clr      = Python.Runtime.ImportHook.GetCLRModule();
                IntPtr clr_dict = Runtime.PyModule_GetDict(clr);

                var locals = new PyDict();
                try
                {
                    IntPtr module         = Runtime.PyImport_AddModule("clr._extras");
                    IntPtr module_globals = Runtime.PyModule_GetDict(module);
                    IntPtr builtins       = Runtime.PyEval_GetBuiltins();
                    Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins);

                    Assembly assembly = Assembly.GetExecutingAssembly();
                    using (Stream stream = assembly.GetManifestResourceStream("clr.py"))
                        using (var reader = new StreamReader(stream))
                        {
                            // add the contents of clr.py to the module
                            string clr_py = reader.ReadToEnd();
                            Exec(clr_py, module_globals, locals.Handle);
                        }

                    // add the imported module to the clr module, and copy the API functions
                    // and decorators into the main clr module.
                    Runtime.PyDict_SetItemString(clr_dict, "_extras", module);
                    foreach (PyObject key in locals.Keys())
                    {
                        if (!key.ToString().StartsWith("_") || key.ToString().Equals("__version__"))
                        {
                            PyObject value = locals[key];
                            Runtime.PyDict_SetItem(clr_dict, key.Handle, value.Handle);
                            value.Dispose();
                        }
                        key.Dispose();
                    }
                }
                finally
                {
                    locals.Dispose();
                }
            }
        }
Exemple #15
0
        /// <summary>
        /// Initialize Method
        /// </summary>
        ///
        /// <remarks>
        /// Initialize the Python runtime. It is safe to call this method
        /// more than once, though initialization will only happen on the
        /// first call. It is *not* necessary to hold the Python global
        /// interpreter lock (GIL) to call this method.
        /// </remarks>
        public static void Initialize()
        {
            if (!initialized)
            {
                // Creating the delegateManager MUST happen before Runtime.Initialize
                // is called. If it happens afterwards, DelegateManager's CodeGenerator
                // throws an exception in its ctor.  This exception is eaten somehow
                // during an initial "import clr", and the world ends shortly thereafter.
                // This is probably masking some bad mojo happening somewhere in Runtime.Initialize().
                delegateManager = new DelegateManager();
                Runtime.Initialize();
                initialized = true;
                Exceptions.Clear();

                // register the atexit callback (this doesn't use Py_AtExit as the C atexit
                // callbacks are called after python is fully finalized but the python ones
                // are called while the python engine is still running).
                string code =
                    "import atexit, clr\n" +
                    "atexit.register(clr._AtExit)\n";
                PyObject r = PythonEngine.RunString(code);
                if (r != null)
                {
                    r.Dispose();
                }

                // Load the clr.py resource into the clr module
                IntPtr clr      = Python.Runtime.ImportHook.GetCLRModule();
                IntPtr clr_dict = Runtime.PyModule_GetDict(clr);

                PyDict locals = new PyDict();
                try
                {
                    IntPtr module         = Runtime.PyImport_AddModule("clr._extras");
                    IntPtr module_globals = Runtime.PyModule_GetDict(module);
                    IntPtr builtins       = Runtime.PyEval_GetBuiltins();
                    Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins);

                    var assembly = Assembly.GetExecutingAssembly();
                    using (Stream stream = assembly.GetManifestResourceStream("clr.py"))
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            // add the contents of clr.py to the module
                            string   clr_py = reader.ReadToEnd();
                            PyObject result = RunString(clr_py, module_globals, locals.Handle);
                            if (null == result)
                            {
                                throw new PythonException();
                            }
                            result.Dispose();
                        }

                    // add the imported module to the clr module, and copy the API functions
                    // and decorators into the main clr module.
                    Runtime.PyDict_SetItemString(clr_dict, "_extras", module);
                    foreach (PyObject key in locals.Keys())
                    {
                        if (!key.ToString().StartsWith("_"))
                        {
                            PyObject value = locals[key];
                            Runtime.PyDict_SetItem(clr_dict, key.Handle, value.Handle);
                            value.Dispose();
                        }
                        key.Dispose();
                    }
                }
                finally
                {
                    locals.Dispose();
                }
            }
        }