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

Skip to content

Commit 31236b5

Browse files
committed
ImportHook preserves the original exception message when an import fails
1 parent fc47172 commit 31236b5

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

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)