Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a321daa
(WIP) modernize the import hook
BadSingleton Jan 15, 2021
279b535
Add the loaded namespaces tracking
BadSingleton Jan 15, 2021
f92e95b
cleanup and changelog entry
BadSingleton Jan 15, 2021
afffc18
Fix a bug where clr wasn't in sys.modules after reload
BadSingleton Jan 18, 2021
d821c0f
Further refinements to setattr logic on ModuleObjects
BadSingleton Jan 19, 2021
e469a8a
fixups, add docs
BadSingleton Jan 20, 2021
685b972
merge fixup
BadSingleton Mar 4, 2021
be81364
(WIP) import hook in the pytohn module
BadSingleton Mar 10, 2021
73958ed
Revert "(WIP) import hook in the pytohn module"
BadSingleton Apr 12, 2021
e71a0ef
Import hook as a module, take 2
BadSingleton Apr 12, 2021
2af066d
fixup! Merge remote-tracking branch 'origin/master' into modernize-im…
BadSingleton Apr 12, 2021
bb490bf
fixup! fixup! Merge remote-tracking branch 'origin/master' into moder…
BadSingleton Apr 12, 2021
31ea876
Create a clr.loader module
BadSingleton Jun 1, 2021
c02d5c6
Test to test if the test passes
BadSingleton Jun 2, 2021
970a189
fix new exception usage
BadSingleton Jun 2, 2021
059605b
kick the build because I can't repro
BadSingleton Jun 2, 2021
ff170e9
Add Namespaces to the import hook only through AddReference
BadSingleton Jun 2, 2021
63de923
Review changes, update API usage
BadSingleton Jun 8, 2021
624f7e3
Merge remote-tracking branch 'upstream/master' into modernize-import-…
BadSingleton Jun 14, 2021
bd7e745
make PyModule_AddObject in line with CPython
BadSingleton Jun 14, 2021
46a85fe
take care of stragglers
BadSingleton Jun 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
(WIP) import hook in the pytohn module
  • Loading branch information
BadSingleton committed Jun 2, 2021
commit be8136448e1220f2a3ea93f7243c2f4a8eaa6237
2 changes: 1 addition & 1 deletion pythonnet/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .find_libpython import find_libpython
from ..find_libpython import find_libpython
33 changes: 33 additions & 0 deletions pythonnet/util/import_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import importlib.abc
import sys

class DotNetLoader(importlib.abc.Loader):

def __init__(self):
super(DotNetLoader, self).__init__()

@classmethod
def exec_module(klass, mod):
# This method needs to exist.
pass

@classmethod
def create_module(klass, spec):
import clr
return clr._LoadClrModule(spec)

class DotNetFinder(importlib.abc.MetaPathFinder):

def __init__(self):
super(DotNetFinder, self).__init__()

@classmethod
def find_spec(klass, fullname, paths=None, target=None):
import clr
if (hasattr(clr, '_availableNamespaces') and fullname in clr._availableNamespaces):
return importlib.machinery.ModuleSpec(fullname, DotNetLoader(), is_package=True)
return None


def init_import_hook():
sys.meta_path.append(DotNetFinder())
4 changes: 2 additions & 2 deletions src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal static unsafe void Initialize()

// Add/create the MetaPathLoader
SetupNamespaceTracking();
PythonEngine.Exec(LoaderCode);
PythonEngine.Exec("import pythonnet.util.import_hook;pythonnet.util.import_hook.init_import_hook()");
}


Expand Down Expand Up @@ -225,7 +225,7 @@ public static unsafe NewReference GetCLRModule()
/// <summary>
/// The hook to import a CLR module into Python
/// </summary>
public static ModuleObject __import__(string modname)
public static ModuleObject Import(string modname)
{
// Traverse the qualified module name to get the named module.
// Note that if
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public static PyObject _LoadClrModule(PyObject spec)
ModuleObject mod = null;
using (var modname = spec.GetAttr("name"))
{
mod = ImportHook.__import__(modname.ToString());
mod = ImportHook.Import(modname.ToString());
}
// We can't return directly a ModuleObject, because the tpHandle is
// not set, but we can return a PyObject.
Expand Down
20 changes: 10 additions & 10 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
ClassDerivedObject.Reset();
TypeManager.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 = PySys_GetObject("path").DangerousGetAddress();
IntPtr item = PyString_FromString(rtdir);
if (PySequence_Contains(path, item) == 0)
{
PyList_Append(new BorrowedReference(path), item);
}
XDecref(item);
// Initialize modules that depend on the runtime class.
AssemblyManager.Initialize();
OperatorMethod.Initialize();
Expand All @@ -167,16 +177,6 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
}
Exceptions.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 = PySys_GetObject("path").DangerousGetAddress();
IntPtr item = PyString_FromString(rtdir);
if (PySequence_Contains(path, item) == 0)
{
PyList_Append(new BorrowedReference(path), item);
}
XDecref(item);
AssemblyManager.UpdatePath();
}

Expand Down