Skip to content

Commit ea61b03

Browse files
authored
Merge pull request #1369 from Unity-Technologies/modernize-import-hook
Modernize import hook
2 parents 12027ad + 46a85fe commit ea61b03

File tree

13 files changed

+301
-223
lines changed

13 files changed

+301
-223
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ One must now either use enum members (e.g. `MyEnum.Option`), or use enum constru
5050
- .NET and Python exceptions are preserved when crossing Python/.NET boundary
5151
- BREAKING: custom encoders are no longer called for instances of `System.Type`
5252
- `PythonException.Restore` no longer clears `PythonException` instance.
53+
- Replaced the old `__import__` hook hack with a PEP302-style Meta Path Loader
5354

5455
### Fixed
5556

src/embed_tests/TestPythonException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public void TestPythonException_Normalize_ThrowsWhenErrorSet()
178178
var pythonException = PythonException.FetchCurrentRaw();
179179
Exceptions.SetError(Exceptions.TypeError, "Another error");
180180
Assert.Throws<InvalidOperationException>(() => pythonException.Normalize());
181+
Exceptions.Clear();
181182
}
182183
}
183184
}

src/embed_tests/pyimport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void SetUp()
3737
Assert.IsFalse(str == IntPtr.Zero);
3838
BorrowedReference path = Runtime.Runtime.PySys_GetObject("path");
3939
Assert.IsFalse(path.IsNull);
40-
Runtime.Runtime.PyList_Append(path, str);
40+
Runtime.Runtime.PyList_Append(path, new BorrowedReference(str));
4141
Runtime.Runtime.XDecref(str);
4242
}
4343

src/runtime/assemblymanager.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ internal class AssemblyManager
3737
// modified from event handlers below, potentially triggered from different .NET threads
3838
private static ConcurrentQueue<Assembly> assemblies;
3939
internal static List<string> pypath;
40-
4140
private AssemblyManager()
4241
{
4342
}
@@ -312,6 +311,15 @@ public static bool IsValidNamespace(string name)
312311
return !string.IsNullOrEmpty(name) && namespaces.ContainsKey(name);
313312
}
314313

314+
/// <summary>
315+
/// Returns an IEnumerable<string> containing the namepsaces exported
316+
/// by loaded assemblies in the current app domain.
317+
/// </summary>
318+
public static IEnumerable<string> GetNamespaces ()
319+
{
320+
return namespaces.Keys;
321+
}
322+
315323
/// <summary>
316324
/// Returns list of assemblies that declare types in a given namespace
317325
/// </summary>

src/runtime/converter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ internal static IntPtr ToPython(object value, Type type)
202202
return ClassDerivedObject.ToPython(pyderived);
203203
}
204204

205+
// ModuleObjects are created in a way that their wrapping them as
206+
// a CLRObject fails, the ClassObject has no tpHandle. Return the
207+
// pyHandle as is, do not convert.
208+
if (value is ModuleObject modobj)
209+
{
210+
var handle = modobj.pyHandle;
211+
Runtime.XIncref(handle);
212+
return handle;
213+
}
214+
205215
// hmm - from Python, we almost never care what the declared
206216
// type is. we'd rather have the object bound to the actual
207217
// implementing class.

src/runtime/extensiontype.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
8383
{
8484
message = "readonly attribute";
8585
}
86-
Exceptions.SetError(Exceptions.TypeError, message);
86+
Exceptions.SetError(Exceptions.AttributeError, message);
8787
return -1;
8888
}
8989

0 commit comments

Comments
 (0)