A MethodWrapper wraps a static method of a managed type, making it callable by Python as a PyCFunction object. This is currently used mainly to implement special cases like the CLR import hook.
예제 #1
0
        /// <summary>
        /// Initialization performed on startup of the Python runtime.
        /// </summary>
        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();

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

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

            py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
            if (py_import == IntPtr.Zero)
            {
                throw new PythonException();
            }

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

            if (Runtime.IsPython3)
            {
                // On Python3, PyImport_ImportModule got a new reference, so we need to decrease it.
                Runtime.Py_DecRef(mod);
            }

            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);
        }
예제 #2
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 = GetNewRefToBuiltins();

            py_import = Runtime.PyObject_GetAttrString(builtins, "__import__");
            hook      = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc");
            Runtime.PyObject_SetAttrString(builtins, "__import__", hook.ptr);
            Runtime.XDecref(hook.ptr);
            Runtime.XDecref(builtins);
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
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);
        }
예제 #6
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.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");

#if (Py_TRACE_REFS && PYTHON35)
            py_clr_module = Runtime.PyModule_FromDefAndSpec2(module_def, (IntPtr)null, 3);
#else
            py_clr_module = Runtime.PyModule_Create2(module_def, 3);
#endif

            // 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

        }
예제 #7
0
        /// <summary>
        /// Cleanup resources upon shutdown of the Python runtime.
        /// </summary>
        internal static void Shutdown()
        {
            if (Runtime.Py_IsInitialized() != 0)
            {
                Runtime.XDecref(py_clr_module);
                py_clr_module = IntPtr.Zero;

                Runtime.XDecref(root.pyHandle);
                root = null;

                hook.Dispose();
                hook = null;

                Runtime.XDecref(py_import);
                py_import = IntPtr.Zero;

                CLRModule.ResetFlags();
            }
        }
예제 #8
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();
            IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__");
            py_import = Runtime.PyObject_GetAttrString(mod, "__import__");

              hook = new MethodWrapper(typeof(ImportHook), "__import__");
              Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr);
            Runtime.Decref(hook.ptr);

            root = new CLRModule();
            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);
        }
예제 #9
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();
            IntPtr mod  = Runtime.PyDict_GetItemString(dict, "__builtin__");

            py_import = Runtime.PyObject_GetAttrString(mod, "__import__");

            hook = new MethodWrapper(typeof(ImportHook), "__import__");
            Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr);
            Runtime.Decref(hook.ptr);

            root = new CLRModule();
            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);
        }
예제 #10
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.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
        }
예제 #11
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);
        }