diff --git a/AUTHORS.md b/AUTHORS.md index 577e898aa..c66ccea82 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -86,3 +86,4 @@ - ([@gpetrou](https://github.com/gpetrou)) - Ehsan Iran-Nejad ([@eirannejad](https://github.com/eirannejad)) - ([@legomanww](https://github.com/legomanww)) +- Effendi Soewono ([@sefgit](https://github.com/sefgit)) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdab9bf64..5f3bde68c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. ### Fixed +- Improve loader (AddReference) robustness. Ignore and print loader error and try to continue to load if possible. ## [3.0.3](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.3) - 2023-10-11 diff --git a/src/runtime/AssemblyManager.cs b/src/runtime/AssemblyManager.cs index a8bbd1f6c..67e9698be 100644 --- a/src/runtime/AssemblyManager.cs +++ b/src/runtime/AssemblyManager.cs @@ -414,6 +414,10 @@ internal static Type[] GetTypes(Assembly a) catch (ReflectionTypeLoadException exc) { // Return all types that were successfully loaded + foreach (var item in exc.LoaderExceptions) + { + Debug.WriteLine("[pythonnet] {0}", item.Message); + } return exc.Types.Where(x => x != null && IsExported(x)).ToArray(); } } @@ -425,8 +429,31 @@ internal static Type[] GetTypes(Assembly a) } catch (FileNotFoundException) { + Debug.WriteLine("[pythonnet] {0} File not found", a.GetName()); + return new Type[0]; + } + catch (System.TypeLoadException e) + { + try + { + return a.GetTypes().Where(IsExported).ToArray(); + } + catch (ReflectionTypeLoadException exc) + { + foreach (var item in exc.LoaderExceptions) + { + Debug.WriteLine("[pythonnet] {0}", item.Message); + } + // Return all types that were successfully loaded + return exc.Types.Where(x => x != null && IsExported(x)).ToArray(); + } + } + catch (Exception e) // System.TypeLoadException + { + Debug.WriteLine("[pythonnet] {0} {1}", a.GetName(), e); return new Type[0]; } + } }