Skip to content

Commit 0ccc443

Browse files
committed
ImportHook preserves the original exception message when an import fails
1 parent 5bb2566 commit 0ccc443

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ jobs:
5353
run: dotnet test --runtime any-${{ matrix.platform }} src/embed_tests/
5454
if: ${{ matrix.os != 'macos' }} # Not working right now, doesn't find libpython
5555

56-
- name: Python tests runner
56+
- name: Python tests run from .NET
5757
run: dotnet test --runtime any-${{ matrix.platform }} src/python_tests_runner/
58-
if: ${{ matrix.os != 'macos' }} # Not working right now, doesn't find libpython
58+
if: ${{ matrix.os == 'windows' }} # Not working for others right now
5959

6060
# TODO: Run perf tests
6161
# TODO: Run mono tests on Windows?

src/runtime/importhook.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
291291
// We don't support them anyway
292292
return IntPtr.Zero;
293293
}
294+
// Save the exception
295+
var originalException = new PythonException();
296+
var originalExceptionMessage = originalException.ToString();
294297
// Otherwise, just clear the it.
295298
Exceptions.Clear();
296299

@@ -342,7 +345,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
342345
ManagedType mt = tail.GetAttribute(name, true);
343346
if (!(mt is ModuleObject))
344347
{
345-
Exceptions.SetError(Exceptions.ImportError, $"No module named {name}");
348+
Exceptions.SetError(Exceptions.ImportError, originalExceptionMessage);
346349
return IntPtr.Zero;
347350
}
348351
if (head == null)

0 commit comments

Comments
 (0)