From ca105f93411005d13c0d58a3298984706d50ecb0 Mon Sep 17 00:00:00 2001 From: sefgit Date: Tue, 26 Dec 2023 14:28:58 +0700 Subject: [PATCH 1/3] Improve loader robustness Ignore and print loader error and proceed with loading if possible. --- src/runtime/AssemblyManager.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/runtime/AssemblyManager.cs b/src/runtime/AssemblyManager.cs index a8bbd1f6c..4be033f71 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) + { + System.Console.Error.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) { + System.Console.Error.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) + { + System.Console.Error.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 + { + System.Console.Error.WriteLine("[pythonnet] {0} {1}", a.GetName(), e); return new Type[0]; } + } } From 2e81a55270501a3612fb5eda2a932653d96148cc Mon Sep 17 00:00:00 2001 From: sefgit Date: Tue, 26 Dec 2023 14:41:20 +0700 Subject: [PATCH 2/3] as required to submit PR PR requirements --- AUTHORS.md | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) 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 From a57b8de4e0fb30166eee292941a8f427a530cd66 Mon Sep 17 00:00:00 2001 From: sefgit Date: Fri, 29 Dec 2023 10:49:46 +0700 Subject: [PATCH 3/3] Use Debug instead of Console.Error updated to use Debug --- src/runtime/AssemblyManager.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/runtime/AssemblyManager.cs b/src/runtime/AssemblyManager.cs index 4be033f71..67e9698be 100644 --- a/src/runtime/AssemblyManager.cs +++ b/src/runtime/AssemblyManager.cs @@ -416,7 +416,7 @@ internal static Type[] GetTypes(Assembly a) // Return all types that were successfully loaded foreach (var item in exc.LoaderExceptions) { - System.Console.Error.WriteLine("[pythonnet] {0}", item.Message); + Debug.WriteLine("[pythonnet] {0}", item.Message); } return exc.Types.Where(x => x != null && IsExported(x)).ToArray(); } @@ -429,7 +429,7 @@ internal static Type[] GetTypes(Assembly a) } catch (FileNotFoundException) { - System.Console.Error.WriteLine("[pythonnet] {0} File not found", a.GetName()); + Debug.WriteLine("[pythonnet] {0} File not found", a.GetName()); return new Type[0]; } catch (System.TypeLoadException e) @@ -442,7 +442,7 @@ internal static Type[] GetTypes(Assembly a) { foreach (var item in exc.LoaderExceptions) { - System.Console.Error.WriteLine("[pythonnet] {0}", item.Message); + Debug.WriteLine("[pythonnet] {0}", item.Message); } // Return all types that were successfully loaded return exc.Types.Where(x => x != null && IsExported(x)).ToArray(); @@ -450,7 +450,7 @@ internal static Type[] GetTypes(Assembly a) } catch (Exception e) // System.TypeLoadException { - System.Console.Error.WriteLine("[pythonnet] {0} {1}", a.GetName(), e); + Debug.WriteLine("[pythonnet] {0} {1}", a.GetName(), e); return new Type[0]; }