8000 Improve loader robustness by sefgit · Pull Request #2303 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Improve loader robustness #2303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension 10000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions src/runtime/AssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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];
}

}
}

Expand Down
0