Skip to content

Modernize import hook #1369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ One must now either use enum members (e.g. `MyEnum.Option`), or use enum constru
- .NET and Python exceptions are preserved when crossing Python/.NET boundary
- BREAKING: custom encoders are no longer called for instances of `System.Type`
- `PythonException.Restore` no longer clears `PythonException` instance.
- Replaced the old `__import__` hook hack with a PEP302-style Meta Path Loader

### Fixed

Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public void TestPythonException_Normalize_ThrowsWhenErrorSet()
var pythonException = PythonException.FetchCurrentRaw();
Exceptions.SetError(Exceptions.TypeError, "Another error");
Assert.Throws<InvalidOperationException>(() => pythonException.Normalize());
Exceptions.Clear();
}
}
}
2 changes: 1 addition & 1 deletion src/embed_tests/pyimport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void SetUp()
Assert.IsFalse(str == IntPtr.Zero);
BorrowedReference path = Runtime.Runtime.PySys_GetObject("path");
Assert.IsFalse(path.IsNull);
Runtime.Runtime.PyList_Append(path, str);
Runtime.Runtime.PyList_Append(path, new BorrowedReference(str));
Runtime.Runtime.XDecref(str);
}

Expand Down
10 changes: 9 additions & 1 deletion src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ internal class AssemblyManager
// modified from event handlers below, potentially triggered from different .NET threads
private static ConcurrentQueue<Assembly> assemblies;
internal static List<string> pypath;

private AssemblyManager()
{
}
Expand Down Expand Up @@ -312,6 +311,15 @@ public static bool IsValidNamespace(string name)
return !string.IsNullOrEmpty(name) && namespaces.ContainsKey(name);
}

/// <summary>
/// Returns an IEnumerable<string> containing the namepsaces exported
/// by loaded assemblies in the current app domain.
/// </summary>
public static IEnumerable<string> GetNamespaces ()
{
return namespaces.Keys;
}

/// <summary>
/// Returns list of assemblies that declare types in a given namespace
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ internal static IntPtr ToPython(object value, Type type)
return ClassDerivedObject.ToPython(pyderived);
}

// ModuleObjects are created in a way that their wrapping them as
// a CLRObject fails, the ClassObject has no tpHandle. Return the
// pyHandle as is, do not convert.
if (value is ModuleObject modobj)
{
var handle = modobj.pyHandle;
Runtime.XIncref(handle);
return handle;
}

// hmm - from Python, we almost never care what the declared
// type is. we'd rather have the object bound to the actual
// implementing class.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/extensiontype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
{
message = "readonly attribute";
}
Exceptions.SetError(Exceptions.TypeError, message);
Exceptions.SetError(Exceptions.AttributeError, message);
return -1;
}

Expand Down
Loading