internal static void Shutdown() { AssemblyManager.Shutdown(); Exceptions.Shutdown(); ImportHook.Shutdown(); Interop.Py_Finalize(); }
public static ModuleObject _load_clr_module(PyObject spec) { ModuleObject mod = null; using var modname = spec.GetAttr("name"); mod = ImportHook.Import(modname.ToString()); return(mod); }
public static Assembly AddReference(string name) { AssemblyManager.UpdatePath(); var origNs = AssemblyManager.GetNamespaces(); Assembly assembly = null; assembly = AssemblyManager.FindLoadedAssembly(name); if (assembly == null) { assembly = AssemblyManager.LoadAssemblyPath(name); } if (assembly == null && AssemblyManager.TryParseAssemblyName(name) is { } parsedName) { assembly = AssemblyManager.LoadAssembly(parsedName); } if (assembly == null) { assembly = AssemblyManager.LoadAssemblyFullPath(name); } if (assembly == null) { throw new FileNotFoundException($"Unable to find assembly '{name}'."); } // Classes that are not in a namespace needs an extra nudge to be found. ImportHook.UpdateCLRModuleDict(); // A bit heavyhanded, but we can't use the AssemblyManager's AssemblyLoadHandler // method because it may be called from other threads, leading to deadlocks // if it is called while Python code is executing. var currNs = AssemblyManager.GetNamespaces().Except(origNs); foreach (var ns in currNs) { ImportHook.AddNamespaceWithGIL(ns); } return(assembly); }
/// <summary> /// Scans an assembly for exported namespaces, adding them to the /// mapping of valid namespaces. Note that for a given namespace /// a.b.c.d, each of a, a.b, a.b.c and a.b.c.d are considered to /// be valid namespaces (to better match Python import semantics). /// </summary> internal static void ScanAssembly(Assembly assembly) { if (assembly.GetCustomAttribute <PyExportAttribute>()?.Export == false) { return; } // A couple of things we want to do here: first, we want to // gather a list of all of the namespaces contributed to by // the assembly. foreach (Type t in GetTypes(assembly)) { string ns = t.Namespace ?? ""; if (!namespaces.ContainsKey(ns)) { string[] names = ns.Split('.'); var s = ""; for (var n = 0; n < names.Length; n++) { s = n == 0 ? names[0] : s + "." + names[n]; if (namespaces.TryAdd(s, new ConcurrentDictionary <Assembly, string>())) { ImportHook.AddNamespace(s); } } } if (ns != null) { namespaces[ns].TryAdd(assembly, string.Empty); } if (ns != null && t.IsGenericTypeDefinition) { GenericUtil.Register(t); } } }
/// <summary> /// Initialize the runtime... /// </summary> internal static void Initialize() { if (Interop.Py_IsInitialized() == 0) { Interop.Py_Initialize(); } if (Interop.PyEval_ThreadsInitialized() == 0) { Interop.PyEval_InitThreads(); } IntPtr op; IntPtr dict; if (IsPython3) { op = Interop.PyImport_ImportModule("builtins"); dict = Interop.PyObject_GetAttrString(op, "__dict__"); } else // Python2 { dict = Interop.PyImport_GetModuleDict(); op = Interop.PyDict_GetItemString(dict, "__builtin__"); } PyNotImplemented = Interop.PyObject_GetAttrString(op, "NotImplemented"); PyBaseObjectType = Interop.PyObject_GetAttrString(op, "object"); PyModuleType = PyObject_Type(op); PyNone = Interop.PyObject_GetAttrString(op, "None"); PyTrue = Interop.PyObject_GetAttrString(op, "True"); PyFalse = Interop.PyObject_GetAttrString(op, "False"); PyBoolType = PyObject_Type(PyTrue); PyNoneType = PyObject_Type(PyNone); PyTypeType = PyObject_Type(PyNoneType); op = Interop.PyObject_GetAttrString(dict, "keys"); PyMethodType = PyObject_Type(op); XDecref(op); // For some arcane reason, builtins.__dict__.__setitem__ is *not* // a wrapper_descriptor, even though dict.__setitem__ is. // // object.__init__ seems safe, though. op = Interop.PyObject_GetAttrString(PyBaseObjectType, "__init__"); PyWrapperDescriptorType = PyObject_Type(op); XDecref(op); #if PYTHON3 XDecref(dict); #endif op = PyString_FromString("string"); PyStringType = PyObject_Type(op); XDecref(op); op = PyUnicode_FromString("unicode"); PyUnicodeType = PyObject_Type(op); XDecref(op); #if PYTHON3 op = Interop.PyBytes_FromString("bytes"); PyBytesType = PyObject_Type(op); XDecref(op); #endif op = Interop.PyTuple_New(0); PyTupleType = PyObject_Type(op); XDecref(op); op = Interop.PyList_New(0); PyListType = PyObject_Type(op); XDecref(op); op = Interop.PyDict_New(); PyDictType = PyObject_Type(op); XDecref(op); op = PyInt_FromInt32(0); PyIntType = PyObject_Type(op); XDecref(op); op = Interop.PyLong_FromLong(0); PyLongType = PyObject_Type(op); XDecref(op); op = Interop.PyFloat_FromDouble(0); PyFloatType = PyObject_Type(op); XDecref(op); #if PYTHON3 PyClassType = IntPtr.Zero; PyInstanceType = IntPtr.Zero; #elif PYTHON2 IntPtr s = Interop.PyString_FromString("_temp"); IntPtr d = Interop.PyDict_New(); IntPtr c = Interop.PyClass_New(IntPtr.Zero, d, s); PyClassType = Interop.PyObject_Type(c); IntPtr i = Interop.PyInstance_New(c, IntPtr.Zero, IntPtr.Zero); PyInstanceType = Interop.PyObject_Type(i); XDecref(s); XDecref(i); XDecref(c); XDecref(d); #endif Error = new IntPtr(-1); IntPtr dllLocal = IntPtr.Zero; if (Interop.GetDllName() != "__Internal") { if (OS.IsLinux) { dllLocal = NativeMethods_Linux.LoadLibrary(Interop.GetDllName()); } else if (OS.IsOSX) { dllLocal = NativeMethods_OSX.LoadLibrary(Interop.GetDllName()); } else if (OS.IsWindows) { dllLocal = NativeMethods_Windows.LoadLibrary(Interop.GetDllName()); } } if (OS.IsLinux) { _PyObject_NextNotImplemented = NativeMethods_Linux.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); } else if (OS.IsOSX) { _PyObject_NextNotImplemented = NativeMethods_OSX.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); } else if (OS.IsWindows) { _PyObject_NextNotImplemented = NativeMethods_Windows.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); } if (OS.IsWindows && dllLocal != IntPtr.Zero) { NativeMethods_Windows.FreeLibrary(dllLocal); } // Initialize modules that depend on the runtime class. AssemblyManager.Initialize(); PyCLRMetaType = MetaType.Initialize(); Exceptions.Initialize(); ImportHook.Initialize(); // Need to add the runtime directory to sys.path so that we // can find built-in assemblies like System.Data, et. al. string rtdir = RuntimeEnvironment.GetRuntimeDirectory(); IntPtr path = Interop.PySys_GetObject("path"); IntPtr item = PyString_FromString(rtdir); Interop.PyList_Append(path, item); XDecref(item); AssemblyManager.UpdatePath(); }
public static int _add_pending_namespaces() => ImportHook.AddPendingNamespaces();