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

Lines changed: 4 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy