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> /// Append Method /// </summary> /// <remarks> /// Append an item to the list object. /// </remarks> public void Append(PyObject item) { int r = Runtime.PyList_Append(this.Reference, item.obj); if (r < 0) { throw new PythonException(); } }
/// <summary> /// Append Method /// </summary> /// <remarks> /// Append an item to the list object. /// </remarks> public void Append(PyObject item) { int r = Runtime.PyList_Append(this.Reference, new BorrowedReference(item.obj)); if (r < 0) { throw PythonException.ThrowLastAsClrException(); } }
/// <summary> /// Preloads all currently-known names for the module namespace. This /// can be called multiple times, to add names from assemblies that /// may have been loaded since the last call to the method. /// </summary> public void LoadNames() { ManagedType m = null; foreach (string name in AssemblyManager.GetNames(_namespace)) { cache.TryGetValue(name, out m); if (m != null) { continue; } BorrowedReference attr = Runtime.PyDict_GetItemString(DictRef, name); // If __dict__ has already set a custom property, skip it. if (!attr.IsNull) { continue; } if (GetAttribute(name, true) != null) { // if it's a valid attribute, add it to __all__ var pyname = Runtime.PyString_FromString(name); try { if (Runtime.PyList_Append(new BorrowedReference(__all__), new BorrowedReference(pyname)) != 0) { throw PythonException.ThrowLastAsClrException(); } } finally { Runtime.XDecref(pyname); } } } }