| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. | |||
| 12 | 12 | Usage: Python.EmbeddingTest.exe --loop --where="cat != Unstable" | |
| 13 | 13 | - Improved performance. String marshaling between python and clr now cached. | |
| 14 | 14 | Cache reduces GC pressure and saves from extensive memory copying. | |
| 15 | + - Optimized implicit assembly loading on module import, PythonEngine.ImplicitAssemblyLoading event added. | ||
| 15 | 16 | - Added support for embedding python into dotnet core 2.0 (NetStandard 2.0) | |
| 16 | 17 | - Added new build system (pythonnet.15.sln) based on dotnetcore-sdk/xplat(crossplatform msbuild). | |
| 17 | 18 | Currently there two side-by-side build systems that produces the same output (net40) from the same sources. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,7 +194,17 @@ public static Assembly LoadAssembly(string name) | |||
| 194 | 194 | Assembly assembly = null; | |
| 195 | 195 | try | |
| 196 | 196 | { | |
| 197 | - assembly = Assembly.Load(name); | ||
| 197 | + var importEvent = new ImplicitAssemblyLoadingEventArgs(name); | ||
| 198 | + if (importEvent.SkipAssemblyLoad) | ||
| 199 | + { | ||
| 200 | + return null; | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + PythonEngine.RaiseAssemblyAsModuleImportingEvent(importEvent); | ||
| 204 | + if (!importEvent.SkipAssemblyLoad) | ||
| 205 | + { | ||
| 206 | + assembly = Assembly.Load(name); | ||
| 207 | + } | ||
| 198 | 208 | } | |
| 199 | 209 | catch (Exception) | |
| 200 | 210 | { | |
@@ -341,8 +351,17 @@ internal static void ScanAssembly(Assembly assembly) | |||
| 341 | 351 | // A couple of things we want to do here: first, we want to | |
| 342 | 352 | // gather a list of all of the namespaces contributed to by | |
| 343 | 353 | // the assembly. | |
| 354 | + Type[] types = new Type[0]; | ||
| 355 | + try | ||
| 356 | + { | ||
| 357 | + types = assembly.IsDynamic ? assembly.GetTypes():assembly.GetExportedTypes(); | ||
| 358 | + } | ||
| 359 | + catch(TypeLoadException) | ||
| 360 | + { | ||
| 361 | + // Do nothing. | ||
| 362 | + // This problem usually occurs when transitive dependencies have references to older packages than main application. | ||
| 363 | + } | ||
| 344 | 364 | ||
| 345 | - Type[] types = assembly.GetTypes(); | ||
| 346 | 365 | foreach (Type t in types) | |
| 347 | 366 | { | |
| 348 | 367 | string ns = t.Namespace ?? ""; | |
@@ -417,12 +436,15 @@ public static List<string> GetNames(string nsname) | |||
| 417 | 436 | { | |
| 418 | 437 | foreach (Assembly a in namespaces[nsname].Keys) | |
| 419 | 438 | { | |
| 420 | - Type[] types = a.GetTypes(); | ||
| 439 | + Type[] types = a.IsDynamic ? a.GetTypes(): a.GetExportedTypes(); | ||
| 421 | 440 | foreach (Type t in types) | |
| 422 | 441 | { | |
| 423 | 442 | if ((t.Namespace ?? "") == nsname) | |
| 424 | 443 | { | |
| 425 | - names.Add(t.Name); | ||
| 444 | + if (!t.IsNested) | ||
| 445 | + { | ||
| 446 | + names.Add(t.Name); | ||
| 447 | + } | ||
| 426 | 448 | } | |
| 427 | 449 | } | |
| 428 | 450 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,6 +139,11 @@ public static string Compiler | |||
| 139 | 139 | ||
| 140 | 140 | internal static PyReferenceDecrementer CurrentRefDecrementer { get; private set; } | |
| 141 | 141 | ||
| 142 | + /// <summary> | ||
| 143 | + /// Fires when python engines importing module and probably tries to load an assembly. | ||
| 144 | + /// </summary> | ||
| 145 | + public static event EventHandler<ImplicitAssemblyLoadingEventArgs> ImplicitAssemblyLoading; | ||
| 146 | + | ||
| 142 | 147 | public static int RunSimpleString(string code) | |
| 143 | 148 | { | |
| 144 | 149 | return Runtime.PyRun_SimpleString(code); | |
@@ -554,6 +559,29 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals, | |||
| 554 | 559 | } | |
| 555 | 560 | } | |
| 556 | 561 | } | |
| 562 | + | ||
| 563 | + internal static void RaiseAssemblyAsModuleImportingEvent(ImplicitAssemblyLoadingEventArgs e) | ||
| 564 | + { | ||
| 565 | + ImplicitAssemblyLoading?.Invoke(null, e); | ||
| 566 | + } | ||
| 567 | + } | ||
| 568 | + | ||
| 569 | + public class ImplicitAssemblyLoadingEventArgs: EventArgs | ||
| 570 | + { | ||
| 571 | + public ImplicitAssemblyLoadingEventArgs(string moduleName) | ||
| 572 | + { | ||
| 573 | + ModuleName = moduleName; | ||
| 574 | + } | ||
| 575 | + | ||
| 576 | + /// <summary> | ||
| 577 | + /// The name of the module to import that is probably assembly name. | ||
| 578 | + /// </summary> | ||
| 579 | + public string ModuleName { get; } | ||
| 580 | + | ||
| 581 | + /// <summary> | ||
| 582 | + /// Set it to true, if you know that <see cref="ModuleName"/> is not an assembly to import. | ||
| 583 | + /// </summary> | ||
| 584 | + public bool SkipAssemblyLoad { get; set; } | ||
| 557 | 585 | } | |
| 558 | 586 | ||
| 559 | 587 | public enum RunFlagType | |
| Back | FazBrowse Home | New Git URL |
0 commit comments