PyImport_ImportModule() private method

private PyImport_ImportModule ( string name ) : IntPtr
name string
return IntPtr
Esempio n. 1
0
        //===================================================================
        // Initialization performed on startup of the Python runtime.
        //===================================================================

        internal static void Initialize()
        {
            // Initialize the Python <--> CLR module hook. We replace the
            // built-in Python __import__ with our own. This isn't ideal,
            // but it provides the most "Pythonic" way of dealing with CLR
            // modules (Python doesn't provide a way to emulate packages).
            IntPtr dict = Runtime.PyImport_GetModuleDict();

#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
            IntPtr mod = Runtime.PyImport_ImportModule("builtins");
            py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
#else
            IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__");
            py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
#endif
            hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc");
            Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr);
            Runtime.XDecref(hook.ptr);

            root = new CLRModule();

#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
            // create a python module with the same methods as the clr module-like object
            module_def    = ModuleDefOffset.AllocModuleDef("clr");
            py_clr_module = Runtime.PyModule_Create2(module_def, 3);

            // both dicts are borrowed references
            IntPtr 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(mod_dict, clr_dict);
            Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module);
            Runtime.PyDict_SetItemString(dict, "clr", py_clr_module);
#else
            Runtime.XIncref(root.pyHandle); // we are using the module two times
            Runtime.PyDict_SetItemString(dict, "CLR", root.pyHandle);
            Runtime.PyDict_SetItemString(dict, "clr", root.pyHandle);
#endif
        }
Esempio n. 2
0
        /// <summary>
        /// Initialization performed on startup of the Python runtime.
        /// </summary>
        internal static void Initialize()
        {
            // Console.WriteLine("----------IMPORT HOOK INITIALIzE");
            // Initialize the Python <--> CLR module hook. We replace the
            // built-in Python __import__ with our own. This isn't ideal,
            // but it provides the most "Pythonic" way of dealing with CLR
            // modules (Python doesn't provide a way to emulate packages).
            IntPtr dict = Runtime.PyImport_GetModuleDict();

            IntPtr mod = Runtime.IsPython3
                ? Runtime.PyImport_ImportModule("builtins")
                : Runtime.PyDict_GetItemString(dict, "__builtin__");

            py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
            hook      = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc");
            Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr);
            Runtime.XDecref(hook.ptr);

            root = new CLRModule();

#if PYTHON3
            // create a python module with the same methods as the clr module-like object
            InitializeModuleDef();
            py_clr_module = Runtime.PyModule_Create2(module_def, 3);

            // both dicts are borrowed references
            IntPtr 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(mod_dict, clr_dict);
#elif PYTHON2
            Runtime.XIncref(root.pyHandle); // we are using the module two times
            py_clr_module = root.pyHandle;  // Alias handle for PY2/PY3
#endif
            Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module);
            Runtime.PyDict_SetItemString(dict, "clr", py_clr_module);
        }
Esempio n. 3
0
        static Converter()
        {
            nfi         = NumberFormatInfo.InvariantInfo;
            objectType  = typeof(Object);
            stringType  = typeof(String);
            int16Type   = typeof(Int16);
            int32Type   = typeof(Int32);
            int64Type   = typeof(Int64);
            singleType  = typeof(Single);
            doubleType  = typeof(Double);
            decimalType = typeof(Decimal);
            flagsType   = typeof(FlagsAttribute);
            boolType    = typeof(Boolean);
            typeType    = typeof(Type);

            IntPtr decimalMod = Runtime.PyImport_ImportModule("decimal");

            if (decimalMod == IntPtr.Zero)
            {
                throw new PythonException();
            }

            IntPtr dateTimeMod = Runtime.PyImport_ImportModule("datetime");

            if (dateTimeMod == IntPtr.Zero)
            {
                throw new PythonException();
            }

            decimalCtor = Runtime.PyObject_GetAttrString(decimalMod, "Decimal");
            if (decimalCtor == IntPtr.Zero)
            {
                throw new PythonException();
            }

            dateTimeCtor = Runtime.PyObject_GetAttrString(dateTimeMod, "datetime");
            if (dateTimeCtor == IntPtr.Zero)
            {
                throw new PythonException();
            }

            timeSpanCtor = Runtime.PyObject_GetAttrString(dateTimeMod, "timedelta");
            if (timeSpanCtor == IntPtr.Zero)
            {
                throw new PythonException();
            }

            IntPtr tzInfoMod = PythonEngine.ModuleFromString("custom_tzinfo",
                                                             "from datetime import timedelta, tzinfo\n" +
                                                             "class GMT(tzinfo):\n" +
                                                             "    def __init__(self, hours, minutes):\n" +
                                                             "        self.hours = hours\n" +
                                                             "        self.minutes = minutes\n" +
                                                             "    def utcoffset(self, dt):\n" +
                                                             "        return timedelta(hours=self.hours, minutes=self.minutes)\n" +
                                                             "    def tzname(self, dt):\n" +
                                                             "        return \"GMT {0:00}:{1:00}\".format(self.hours, self.minutes)\n" +
                                                             "    def dst (self, dt):\n" +
                                                             "        return timedelta(0)\n").Handle;

            tzInfoCtor = Runtime.PyObject_GetAttrString(tzInfoMod, "GMT");
            if (tzInfoCtor == IntPtr.Zero)
            {
                throw new PythonException();
            }
        }