Beispiel #1
0
        //===================================================================
        // Return the clr python module (new reference)
        //===================================================================
        public static IntPtr GetCLRModule(IntPtr? fromList = null)
        {
            root.InitializePreload();
            #if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
            // update the module dictionary with the contents of the root dictionary
            root.LoadNames();
            IntPtr py_mod_dict = Runtime.PyModule_GetDict(py_clr_module);
            IntPtr clr_dict = Runtime._PyObject_GetDictPtr(root.pyHandle); // PyObject**
            clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr));
            Runtime.PyDict_Update(py_mod_dict, clr_dict);

            // find any items from the fromlist and get them from the root if they're not
            // aleady in the module dictionary
            if (fromList != null && fromList != IntPtr.Zero) {
                if (Runtime.PyTuple_Check(fromList.GetValueOrDefault()))
                {
                    Runtime.XIncref(py_mod_dict);
                    using(PyDict mod_dict = new PyDict(py_mod_dict)) {
                        Runtime.XIncref(fromList.GetValueOrDefault());
                        using (PyTuple from = new PyTuple(fromList.GetValueOrDefault())) {
                            foreach (PyObject item in from) {
                                if (mod_dict.HasKey(item))
                                    continue;

                                string s = item.AsManagedObject(typeof(string)) as string;
                                if (null == s)
                                    continue;

                                ManagedType attr = root.GetAttribute(s, true);
                                if (null == attr)
                                    continue;

                                Runtime.XIncref(attr.pyHandle);
                                using (PyObject obj = new PyObject(attr.pyHandle)) {
                                    mod_dict.SetItem(s, obj);
                                }
                            }
                        }
                    }
                }
            }

            Runtime.XIncref(py_clr_module);
            return py_clr_module;
            #else
            Runtime.XIncref(root.pyHandle);
            return root.pyHandle;
            #endif
        }