static void SetupImportHook() { // Create the import hook module var import_hook_module = Runtime.PyModule_New("clr.loader"); // 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 = NewReference.DangerousFromPointer(Runtime.PyTuple_New(2)); var codeStr = NewReference.DangerousFromPointer(Runtime.PyString_FromString(LoaderCode)); Runtime.PyTuple_SetItem(args, 0, codeStr); var mod_dict = Runtime.PyModule_GetDict(import_hook_module); // reference not stolen due to overload incref'ing for us. Runtime.PyTuple_SetItem(args, 1, mod_dict); Runtime.PyObject_Call(exec, args, default).Dispose(); // Set as a sub-module of clr. if (Runtime.PyModule_AddObject(ClrModuleReference, "loader", import_hook_module.DangerousGetAddress()) != 0) { Runtime.XDecref(import_hook_module.DangerousGetAddress()); throw PythonException.ThrowLastAsClrException(); } // Finally, add the hook to the meta path var findercls = Runtime.PyDict_GetItemString(mod_dict, "DotNetFinder"); var finderCtorArgs = NewReference.DangerousFromPointer(Runtime.PyTuple_New(0)); var finder_inst = Runtime.PyObject_CallObject(findercls, finderCtorArgs); var metapath = Runtime.PySys_GetObject("meta_path"); Runtime.PyList_Append(metapath, finder_inst); }
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())); }
/// <summary> /// Initialization performed on startup of the Python runtime. /// </summary> internal static void Initialize() { InitImport(); // Initialize the clr module and tell Python about it. 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 IntPtr dict = Runtime.PyImport_GetModuleDict(); Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module); Runtime.PyDict_SetItemString(dict, "clr", py_clr_module); }
//=================================================================== // 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.Incref(py_mod_dict); using (PyDict mod_dict = new PyDict(py_mod_dict)) { Runtime.Incref(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.Incref(attr.pyHandle); using (PyObject obj = new PyObject(attr.pyHandle)) { mod_dict.SetItem(s, obj); } } } } } } Runtime.Incref(py_clr_module); return(py_clr_module); #else Runtime.Incref(root.pyHandle); return(root.pyHandle); #endif }
/// <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); }
/// <summary> /// Return the clr python module (new reference) /// </summary> public static unsafe NewReference GetCLRModule(BorrowedReference fromList = default) { root.InitializePreload(); // update the module dictionary with the contents of the root dictionary root.LoadNames(); BorrowedReference py_mod_dict = Runtime.PyModule_GetDict(ClrModuleReference); using (var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference)) { Runtime.PyDict_Update(py_mod_dict, clr_dict); } // find any items from the from list and get them from the root if they're not // already in the module dictionary if (fromList != null) { if (Runtime.PyTuple_Check(fromList)) { using var mod_dict = new PyDict(py_mod_dict); using var from = new PyTuple(fromList); foreach (PyObject item in from) { if (mod_dict.HasKey(item)) { continue; } var s = item.AsManagedObject(typeof(string)) as string; if (s == null) { continue; } ManagedType attr = root.GetAttribute(s, true); if (attr == null) { continue; } Runtime.XIncref(attr.pyHandle); using (var obj = new PyObject(attr.pyHandle)) { mod_dict.SetItem(s, obj); } } } } Runtime.XIncref(py_clr_module); return(NewReference.DangerousFromPointer(py_clr_module)); }
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); }
/// <summary> /// ImportAll Method /// </summary> /// <remarks> /// Import all variables of the module into this scope. /// </remarks> public void ImportAll(PyObject module) { if (Runtime.PyObject_Type(module.obj) != Runtime.PyModuleType) { throw new PyScopeException("object is not a module"); } var module_dict = Runtime.PyModule_GetDict(module.Reference); int result = Runtime.PyDict_Update(VarsRef, module_dict); if (result < 0) { throw PythonException.ThrowLastAsClrException(); } }
/// <summary> /// ImportAll Method /// </summary> /// <remarks> /// Import all variables of the module into this scope. /// </remarks> public void ImportAll(PyObject module) { if (Runtime.PyObject_Type(module.obj) != Runtime.PyModuleType) { throw new PyScopeException("object is not a module"); } var module_dict = Runtime.PyModule_GetDict(module.obj); int result = Runtime.PyDict_Update(variables, module_dict); if (result < 0) { throw new PythonException(); } }
/// <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); Runtime.CheckExceptionOccurred(); Runtime.PyDict_SetItemString( variables, "__builtins__", Runtime.PyEval_GetBuiltins() ); this.Name = this.Get <string>("__name__"); }
/// <summary> /// Constructor /// </summary> /// <remarks> /// Create a scope based on a Python Module. /// </remarks> internal PyScope(IntPtr ptr, PyScopeManager manager) { if (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); if (variables == IntPtr.Zero) { throw new PythonException(); } Runtime.PyDict_SetItemString( variables, "__builtins__", Runtime.PyEval_GetBuiltins() ); this.Name = this.Get <string>("__name__"); }
/// <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__"); }
/// <summary> /// Initialization performed on startup of the Python runtime. /// </summary> internal static unsafe void Initialize() { // Initialize the clr module and tell Python about it. root = new CLRModule(); // create a python module with the same methods as the clr module-like object py_clr_module = Runtime.PyModule_New("clr").DangerousMoveToPointer(); // both dicts are borrowed references BorrowedReference mod_dict = Runtime.PyModule_GetDict(ClrModuleReference); using var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference); Runtime.PyDict_Update(mod_dict, clr_dict); BorrowedReference dict = Runtime.PyImport_GetModuleDict(); Runtime.PyDict_SetItemString(dict, "CLR", ClrModuleReference); Runtime.PyDict_SetItemString(dict, "clr", ClrModuleReference); SetupNamespaceTracking(); SetupImportHook(); }
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)); }
/// <summary> /// Initialization performed on startup of the Python runtime. /// </summary> internal static unsafe void Initialize() { InitImport(); // Initialize the clr module and tell Python about it. root = new CLRModule(); // 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 BorrowedReference mod_dict = Runtime.PyModule_GetDict(ClrModuleReference); BorrowedReference clr_dict = *Runtime._PyObject_GetDictPtr(root.ObjectReference); Runtime.PyDict_Update(mod_dict, clr_dict); BorrowedReference dict = Runtime.PyImport_GetModuleDict(); Runtime.PyDict_SetItemString(dict, "CLR", ClrModuleReference); Runtime.PyDict_SetItemString(dict, "clr", ClrModuleReference); }
//=================================================================== // 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 }
/// <summary> /// Initialize Method /// </summary> /// <remarks> /// Initialize the Python runtime. It is safe to call this method /// more than once, though initialization will only happen on the /// first call. It is *not* necessary to hold the Python global /// interpreter lock (GIL) to call this method. /// initSigs can be set to 1 to do default python signal configuration. This will override the way signals are handled by the application. /// </remarks> public static void Initialize(IEnumerable <string> args, bool setSysArgv = true, bool initSigs = false) { if (!initialized) { // Creating the delegateManager MUST happen before Runtime.Initialize // is called. If it happens afterwards, DelegateManager's CodeGenerator // throws an exception in its ctor. This exception is eaten somehow // during an initial "import clr", and the world ends shortly thereafter. // This is probably masking some bad mojo happening somewhere in Runtime.Initialize(). delegateManager = new DelegateManager(); Runtime.Initialize(initSigs); initialized = true; Exceptions.Clear(); // Make sure we clean up properly on app domain unload. AppDomain.CurrentDomain.DomainUnload += OnDomainUnload; // Remember to shut down the runtime. AddShutdownHandler(Runtime.Shutdown); // The global scope gets used implicitly quite early on, remember // to clear it out when we shut down. AddShutdownHandler(PyScopeManager.Global.Clear); if (setSysArgv) { Py.SetArgv(args); } // register the atexit callback (this doesn't use Py_AtExit as the C atexit // callbacks are called after python is fully finalized but the python ones // are called while the python engine is still running). string code = "import atexit, clr\n" + "atexit.register(clr._AtExit)\n"; PythonEngine.Exec(code); // Load the clr.py resource into the clr module IntPtr clr = Python.Runtime.ImportHook.GetCLRModule(); IntPtr clr_dict = Runtime.PyModule_GetDict(clr); var locals = new PyDict(); try { IntPtr module = Runtime.PyImport_AddModule("clr._extras"); IntPtr module_globals = Runtime.PyModule_GetDict(module); IntPtr builtins = Runtime.PyEval_GetBuiltins(); Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins); Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream("clr.py")) using (var reader = new StreamReader(stream)) { // add the contents of clr.py to the module string clr_py = reader.ReadToEnd(); Exec(clr_py, module_globals, locals.Handle); } // add the imported module to the clr module, and copy the API functions // and decorators into the main clr module. Runtime.PyDict_SetItemString(clr_dict, "_extras", module); foreach (PyObject key in locals.Keys()) { if (!key.ToString().StartsWith("_") || key.ToString().Equals("__version__")) { PyObject value = locals[key]; Runtime.PyDict_SetItem(clr_dict, key.Handle, value.Handle); value.Dispose(); } key.Dispose(); } } finally { locals.Dispose(); } } }
/// <summary> /// Initialize Method /// </summary> /// /// <remarks> /// Initialize the Python runtime. It is safe to call this method /// more than once, though initialization will only happen on the /// first call. It is *not* necessary to hold the Python global /// interpreter lock (GIL) to call this method. /// </remarks> public static void Initialize() { if (!initialized) { // Creating the delegateManager MUST happen before Runtime.Initialize // is called. If it happens afterwards, DelegateManager's CodeGenerator // throws an exception in its ctor. This exception is eaten somehow // during an initial "import clr", and the world ends shortly thereafter. // This is probably masking some bad mojo happening somewhere in Runtime.Initialize(). delegateManager = new DelegateManager(); Runtime.Initialize(); initialized = true; Exceptions.Clear(); // register the atexit callback (this doesn't use Py_AtExit as the C atexit // callbacks are called after python is fully finalized but the python ones // are called while the python engine is still running). string code = "import atexit, clr\n" + "atexit.register(clr._AtExit)\n"; PyObject r = PythonEngine.RunString(code); if (r != null) { r.Dispose(); } // Load the clr.py resource into the clr module IntPtr clr = Python.Runtime.ImportHook.GetCLRModule(); IntPtr clr_dict = Runtime.PyModule_GetDict(clr); PyDict locals = new PyDict(); try { IntPtr module = Runtime.PyImport_AddModule("clr._extras"); IntPtr module_globals = Runtime.PyModule_GetDict(module); IntPtr builtins = Runtime.PyEval_GetBuiltins(); Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins); var assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream("clr.py")) using (StreamReader reader = new StreamReader(stream)) { // add the contents of clr.py to the module string clr_py = reader.ReadToEnd(); PyObject result = RunString(clr_py, module_globals, locals.Handle); if (null == result) { throw new PythonException(); } result.Dispose(); } // add the imported module to the clr module, and copy the API functions // and decorators into the main clr module. Runtime.PyDict_SetItemString(clr_dict, "_extras", module); foreach (PyObject key in locals.Keys()) { if (!key.ToString().StartsWith("_")) { PyObject value = locals[key]; Runtime.PyDict_SetItem(clr_dict, key.Handle, value.Handle); value.Dispose(); } key.Dispose(); } } finally { locals.Dispose(); } } }