Exemplo n.º 1
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()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// PyTuple Constructor
        /// </summary>
        /// <remarks>
        /// Creates a new PyTuple from an array of PyObject instances.
        /// <para />
        /// See caveats about PyTuple_SetItem:
        /// https://www.coursehero.com/file/p4j2ogg/important-exceptions-to-this-rule-PyTupleSetItem-and-PyListSetItem-These/
        /// </remarks>
        public PyTuple(PyObject[] items)
        {
            int count = items.Length;

            obj = Runtime.PyTuple_New(count);
            for (var i = 0; i < count; i++)
            {
                IntPtr ptr = items[i].obj;
                Runtime.XIncref(ptr);
                int res = Runtime.PyTuple_SetItem(obj, i, ptr);
                PythonException.ThrowIfIsNotZero(res);
            }
        }
Exemplo n.º 3
0
        public void SetBuiltins(PyDict builtins)
        {
            if (builtins == null || builtins.IsNone())
            {
                throw new ArgumentNullException(nameof(builtins));
            }

            BorrowedReference globals = Runtime.PyModule_GetDict(this.Reference);

            PythonException.ThrowIfIsNull(globals);
            int rc = Runtime.PyDict_SetItemString(globals, "__builtins__", builtins.Reference);

            PythonException.ThrowIfIsNotZero(rc);
        }
Exemplo n.º 4
0
        private static void AddObjPtrToSet(IntPtr set, IntPtr obj)
        {
            var p = PyLong_FromVoidPtr(obj);

            XIncref(obj);
            try
            {
                int res = PySet_Add(set, p);
                PythonException.ThrowIfIsNotZero(res);
            }
            finally
            {
                XDecref(p);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Restore the __import__ hook.
        /// </summary>
        static void RestoreImport()
        {
            IntPtr builtins = Runtime.GetBuiltins();

            int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, py_import);

            PythonException.ThrowIfIsNotZero(res);
            Runtime.XDecref(py_import);
            py_import = IntPtr.Zero;

            hook.Release();
            hook = null;

            Runtime.XDecref(builtins);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Restore the __import__ hook.
        /// </summary>
        static void RestoreImport()
        {
            IntPtr builtins = GetNewRefToBuiltins();

            int res = Runtime.PyObject_SetAttrString(builtins, "__import__", py_import);

            PythonException.ThrowIfIsNotZero(res);
            Runtime.XDecref(py_import);
            py_import = IntPtr.Zero;

            hook.Release();
            hook = null;

            Runtime.XDecref(builtins);
        }
Exemplo n.º 7
0
        private static void RestoreDotNetModules(Dictionary <PyString, PyObject> modules)
        {
            var pyMoudles = Runtime.PyImport_GetModuleDict();

            foreach (var item in modules)
            {
                var moduleName = item.Key;
                var module     = item.Value;
                int res        = Runtime.PyDict_SetItem(pyMoudles, moduleName, module);
                PythonException.ThrowIfIsNotZero(res);
                item.Key.Dispose();
                item.Value.Dispose();
            }
            modules.Clear();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initialize just the __import__ hook itself.
        /// </summary>
        static void InitImport()
        {
            // We replace the built-in Python __import__ with our own: first
            // look in CLR modules, then if we don't find any call the default
            // Python __import__.
            IntPtr builtins = Runtime.GetBuiltins();

            py_import = Runtime.PyObject_GetAttr(builtins, PyIdentifier.__import__);
            PythonException.ThrowIfIsNull(py_import);

            hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc");
            int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, hook.ptr);

            PythonException.ThrowIfIsNotZero(res);

            Runtime.XDecref(builtins);
        }
Exemplo n.º 9
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__");
        }
Exemplo n.º 10
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);
            PythonException.ThrowIfIsNull(variables);

            int res = Runtime.PyDict_SetItemString(
                variables, "__builtins__",
                Runtime.PyEval_GetBuiltins()
                );

            PythonException.ThrowIfIsNotZero(res);
            this.Name = this.Get <string>("__name__");
        }
Exemplo n.º 11
0
        private static PyModule Create(string name, string filename = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            NewReference op = Runtime.PyModule_New(name);

            PythonException.ThrowIfIsNull(op);

            if (filename != null)
            {
                BorrowedReference globals = Runtime.PyModule_GetDict(op);
                PythonException.ThrowIfIsNull(globals);
                int rc = Runtime.PyDict_SetItemString(globals, "__file__", filename.ToPython().Reference);
                PythonException.ThrowIfIsNotZero(rc);
            }

            return(new PyModule(ref op));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Restore the __import__ hook.
        /// </summary>
        static void RestoreImport()
        {
            IntPtr builtins = Runtime.GetBuiltins();

            IntPtr existing = Runtime.PyObject_GetAttr(builtins, PyIdentifier.__import__);

            Runtime.XDecref(existing);
            if (existing != hook.ptr)
            {
                throw new NotSupportedException("Unable to restore original __import__.");
            }

            int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, py_import);

            PythonException.ThrowIfIsNotZero(res);
            Runtime.XDecref(py_import);
            py_import = IntPtr.Zero;

            hook.Release();
            hook = null;

            Runtime.XDecref(builtins);
        }
Exemplo n.º 13
0
        public static void Save()
        {
            if (!PySys_GetObject("dummy_gc").IsNull)
            {
                throw new Exception("Runtime State set already");
            }

            IntPtr objs = IntPtr.Zero;

            if (ShouldRestoreObjects)
            {
                objs = PySet_New(IntPtr.Zero);
                foreach (var obj in PyGCGetObjects())
                {
                    AddObjPtrToSet(objs, obj);
                }
            }

            var modules = PySet_New(IntPtr.Zero);

            foreach (var name in GetModuleNames())
            {
                int res = PySet_Add(modules, name);
                PythonException.ThrowIfIsNotZero(res);
            }


            var dummyGCHead = PyMem_Malloc(Marshal.SizeOf(typeof(PyGC_Head)));

            unsafe
            {
                var head = (PyGC_Head *)dummyGCHead;
                head->gc.gc_next = dummyGCHead;
                head->gc.gc_prev = dummyGCHead;
                head->gc.gc_refs = IntPtr.Zero;
            }
            {
                var pyDummyGC = PyLong_FromVoidPtr(dummyGCHead);
                int res       = PySys_SetObject("dummy_gc", pyDummyGC);
                PythonException.ThrowIfIsNotZero(res);
                XDecref(pyDummyGC);

                try
                {
                    res = PySys_SetObject("initial_modules", modules);
                    PythonException.ThrowIfIsNotZero(res);
                }
                finally
                {
                    XDecref(modules);
                }

                if (ShouldRestoreObjects)
                {
                    AddObjPtrToSet(objs, modules);
                    try
                    {
                        res = PySys_SetObject("initial_objs", objs);
                        PythonException.ThrowIfIsNotZero(res);
                    }
                    finally
                    {
                        XDecref(objs);
                    }
                }
            }
        }