public static AllocModuleDef ( string modulename ) : |
||
modulename | string | |
리턴 |
internal static void InitializeModuleDef() { if (module_def == IntPtr.Zero) { module_def = ModuleDefOffset.AllocModuleDef("clr"); } }
//=================================================================== // 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.Decref(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.Incref(root.pyHandle); // we are using the module two times Runtime.PyDict_SetItemString(dict, "CLR", root.pyHandle); Runtime.PyDict_SetItemString(dict, "clr", root.pyHandle); #endif }