8000 Improve loader robustness · pythonnet/pythonnet@ca105f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca105f9

Browse files
committed
Improve loader robustness
Ignore and print loader error and proceed with loading if possible.
1 parent eef67db commit ca105f9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/runtime/AssemblyManager.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ internal static Type[] GetTypes(Assembly a)
414414
catch (ReflectionTypeLoadException exc)
415415
{
416416
// Return all types that were successfully loaded
417+
foreach (var item in exc.LoaderExceptions)
418+
{
419+
System.Console.Error.WriteLine("[pythonnet] {0}", item.Message);
420+
}
417421
return exc.Types.Where(x => x != null && IsExported(x)).ToArray();
418422
}
419423
}
@@ -425,8 +429,31 @@ internal static Type[] GetTypes(Assembly a)
425429
}
426430
catch (FileNotFoundException)
427431
{
432+
System.Console.Error.WriteLine("[pythonnet] {0} File not found", a.GetName());
433+
return new Type[0];
434+
}
435+
catch (System.TypeLoadException e)
436+
{
437+
try
438+
{
439+
return a.GetTypes().Where(IsExported).ToArray();
440+
}
441+
catch (ReflectionTypeLoadException exc)
442+
{
443+
foreach (var item in exc.LoaderExceptions)
444+
{
445+
System.Console.Error.WriteLine("[pythonnet] {0}", item.Message);
446+
}
447+
// Return all types that were successfully loaded
448+
return exc.Types.Where(x => x != null && IsExported(x)).ToArray();
449+
}
450+
}
451+
catch (Exception e) // System.TypeLoadException
452+
{
453+
System.Console.Error.WriteLine("[pythonnet] {0} {1}", a.GetName(), e);
428454
return new Type[0];
429455
}
456+
430457
}
431458
}
432459

0 commit comments

Comments
 (0)
0