| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 685b972 commit be81364
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1 +1 @@ | |||
| 1 | - from .find_libpython import find_libpython | ||
| 1 | + from ..find_libpython import find_libpython | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + import importlib.abc | ||
| 2 | + import sys | ||
| 3 | + | ||
| 4 | + class DotNetLoader(importlib.abc.Loader): | ||
| 5 | + | ||
| 6 | + def __init__(self): | ||
| 7 | + super(DotNetLoader, self).__init__() | ||
| 8 | + | ||
| 9 | + @classmethod | ||
| 10 | + def exec_module(klass, mod): | ||
| 11 | + # This method needs to exist. | ||
| 12 | + pass | ||
| 13 | + | ||
| 14 | + @classmethod | ||
| 15 | + def create_module(klass, spec): | ||
| 16 | + import clr | ||
| 17 | + return clr._LoadClrModule(spec) | ||
| 18 | + | ||
| 19 | + class DotNetFinder(importlib.abc.MetaPathFinder): | ||
| 20 | + | ||
| 21 | + def __init__(self): | ||
| 22 | + super(DotNetFinder, self).__init__() | ||
| 23 | + | ||
| 24 | + @classmethod | ||
| 25 | + def find_spec(klass, fullname, paths=None, target=None): | ||
| 26 | + import clr | ||
| 27 | + if (hasattr(clr, '_availableNamespaces') and fullname in clr._availableNamespaces): | ||
| 28 | + return importlib.machinery.ModuleSpec(fullname, DotNetLoader(), is_package=True) | ||
| 29 | + return None | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + def init_import_hook(): | ||
| 33 | + sys.meta_path.append(DotNetFinder()) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,7 +69,7 @@ internal static unsafe void Initialize() | |||
| 69 | 69 | ||
| 70 | 70 | // Add/create the MetaPathLoader | |
| 71 | 71 | SetupNamespaceTracking(); | |
| 72 | - PythonEngine.Exec(LoaderCode); | ||
| 72 | + PythonEngine.Exec("import pythonnet.util.import_hook;pythonnet.util.import_hook.init_import_hook()"); | ||
| 73 | 73 | } | |
| 74 | 74 | ||
| 75 | 75 | ||
@@ -225,7 +225,7 @@ public static unsafe NewReference GetCLRModule() | |||
| 225 | 225 | /// <summary> | |
| 226 | 226 | /// The hook to import a CLR module into Python | |
| 227 | 227 | /// </summary> | |
| 228 | - public static ModuleObject __import__(string modname) | ||
| 228 | + public static ModuleObject Import(string modname) | ||
| 229 | 229 | { | |
| 230 | 230 | // Traverse the qualified module name to get the named module. | |
| 231 | 231 | // Note that if | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -598,7 +598,7 @@ public static PyObject _LoadClrModule(PyObject spec) | |||
| 598 | 598 | ModuleObject mod = null; | |
| 599 | 599 | using (var modname = spec.GetAttr("name")) | |
| 600 | 600 | { | |
| 601 | - mod = ImportHook.__import__(modname.ToString()); | ||
| 601 | + mod = ImportHook.Import(modname.ToString()); | ||
| 602 | 602 | } | |
| 603 | 603 | // We can't return directly a ModuleObject, because the tpHandle is | |
| 604 | 604 | // not set, but we can return a PyObject. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -153,6 +153,16 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd | |||
| 153 | 153 | ClassDerivedObject.Reset(); | |
| 154 | 154 | TypeManager.Initialize(); | |
| 155 | 155 | ||
| 156 | + // Need to add the runtime directory to sys.path so that we | ||
| 157 | + // can find built-in assemblies like System.Data, et. al. | ||
| 158 | + string rtdir = RuntimeEnvironment.GetRuntimeDirectory(); | ||
| 159 | + IntPtr path = PySys_GetObject("path").DangerousGetAddress(); | ||
| 160 | + IntPtr item = PyString_FromString(rtdir); | ||
| 161 | + if (PySequence_Contains(path, item) == 0) | ||
| 162 | + { | ||
| 163 | + PyList_Append(new BorrowedReference(path), item); | ||
| 164 | + } | ||
| 165 | + XDecref(item); | ||
| 156 | 166 | // Initialize modules that depend on the runtime class. | |
| 157 | 167 | AssemblyManager.Initialize(); | |
| 158 | 168 | OperatorMethod.Initialize(); | |
@@ -167,16 +177,6 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd | |||
| 167 | 177 | } | |
| 168 | 178 | Exceptions.Initialize(); | |
| 169 | 179 | ||
| 170 | - // Need to add the runtime directory to sys.path so that we | ||
| 171 | - // can find built-in assemblies like System.Data, et. al. | ||
| 172 | - string rtdir = RuntimeEnvironment.GetRuntimeDirectory(); | ||
| 173 | - IntPtr path = PySys_GetObject("path").DangerousGetAddress(); | ||
| 174 | - IntPtr item = PyString_FromString(rtdir); | ||
| 175 | - if (PySequence_Contains(path, item) == 0) | ||
| 176 | - { | ||
| 177 | - PyList_Append(new BorrowedReference(path), item); | ||
| 178 | - } | ||
| 179 | - XDecref(item); | ||
| 180 | 180 | AssemblyManager.UpdatePath(); | |
| 181 | 181 | } | |
| 182 | 182 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments