Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be81364

Browse files
committed
(WIP) import hook in the pytohn module
1 parent 685b972 commit be81364

5 files changed

Lines changed: 47 additions & 14 deletions

File tree

pythonnet/util/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .find_libpython import find_libpython
1+
from ..find_libpython import find_libpython

pythonnet/util/import_hook.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff 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())

src/runtime/importhook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal static unsafe void Initialize()
6969

7070
// Add/create the MetaPathLoader
7171
SetupNamespaceTracking();
72-
PythonEngine.Exec(LoaderCode);
72+
PythonEngine.Exec("import pythonnet.util.import_hook;pythonnet.util.import_hook.init_import_hook()");
7373
}
7474

7575

@@ -225,7 +225,7 @@ public static unsafe NewReference GetCLRModule()
225225
/// <summary>
226226
/// The hook to import a CLR module into Python
227227
/// </summary>
228-
public static ModuleObject __import__(string modname)
228+
public static ModuleObject Import(string modname)
229229
{
230230
// Traverse the qualified module name to get the named module.
231231
// Note that if

src/runtime/moduleobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public static PyObject _LoadClrModule(PyObject spec)
598598
ModuleObject mod = null;
599599
using (var modname = spec.GetAttr("name"))
600600
{
601-
mod = ImportHook.__import__(modname.ToString());
601+
mod = ImportHook.Import(modname.ToString());
602602
}
603603
// We can't return directly a ModuleObject, because the tpHandle is
604604
// not set, but we can return a PyObject.

src/runtime/runtime.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
153153
ClassDerivedObject.Reset();
154154
TypeManager.Initialize();
155155

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);
156166
// Initialize modules that depend on the runtime class.
157167
AssemblyManager.Initialize();
158168
OperatorMethod.Initialize();
@@ -167,16 +177,6 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
167177
}
168178
Exceptions.Initialize();
169179

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);
180180
AssemblyManager.UpdatePath();
181181
}
182182

0 commit comments

Comments
 (0)