Skip to content

Commit 8ee0c65

Browse files
author
dse
committed
Implicit assembly loading optimization.
1 parent 9b62597 commit 8ee0c65

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
88
## [unreleased][]
99

1010
### Added
11+
- Optimized implicit assembly loading on module import, PythonEngine.ImplicitAssemblyLoading event added.
1112
- Added support for embedding python into dotnet core 2.0 (NetStandard 2.0)
1213
- Added new build system (pythonnet.15.sln) based on dotnetcore-sdk/xplat(crossplatform msbuild).
1314
Currently there two side-by-side build systems that produces the same output (net40) from the same sources.

src/runtime/assemblymanager.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ public static Assembly LoadAssembly(string name)
196196
Assembly assembly = null;
197197
try
198198
{
199+
var importEvent = new ImplicitAssemblyLoadingEventArgs(name);
200+
if (importEvent.SkipAssemblyLoad)
201+
{
202+
return null;
203+
}
204+
205+
PythonEngine.RaiseAssemblyAsModuleImportingEvent(importEvent);
206+
199207
assembly = Assembly.Load(name);
200208
}
201209
catch (Exception)
@@ -419,12 +427,15 @@ public static List<string> GetNames(string nsname)
419427
{
420428
foreach (Assembly a in namespaces[nsname].Keys)
421429
{
422-
Type[] types = a.GetTypes();
430+
Type[] types = a.IsDynamic ? a.GetTypes(): a.GetExportedTypes();
423431
foreach (Type t in types)
424432
{
425433
if ((t.Namespace ?? "") == nsname)
426434
{
427-
names.Add(t.Name);
435+
if (!t.IsNested)
436+
{
437+
names.Add(t.Name);
438+
}
428439
}
429440
}
430441
}

src/runtime/pythonengine.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -130,6 +130,11 @@ public static string Compiler
130130
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetCompiler()); }
131131
}
132132

133+
/// <summary>
134+
/// Fires when python engines importing module and probably tries to load an assembly.
135+
/// </summary>
136+
public static event EventHandler<ImplicitAssemblyLoadingEventArgs> ImplicitAssemblyLoading;
137+
133138
public static int RunSimpleString(string code)
134139
{
135140
return Runtime.PyRun_SimpleString(code);
@@ -520,6 +525,29 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
520525
}
521526
}
522527
}
528+
529+
internal static void RaiseAssemblyAsModuleImportingEvent(ImplicitAssemblyLoadingEventArgs e)
530+
{
531+
ImplicitAssemblyLoading?.Invoke(null, e);
532+
}
533+
}
534+
535+
public class ImplicitAssemblyLoadingEventArgs: EventArgs
536+
{
537+
public ImplicitAssemblyLoadingEventArgs(string moduleName)
538+
{
539+
ModuleName = moduleName;
540+
}
541+
542+
/// <summary>
543+
/// The name of the module to import that is probably assembly name.
544+
/// </summary>
545+
public string ModuleName { get; }
546+
547+
/// <summary>
548+
/// Set it to true, if you know that <see cref="ModuleName"/> is not an assembly to import.
549+
/// </summary>
550+
public bool SkipAssemblyLoad { get; set; }
523551
}
524552

525553
public enum RunFlagType

0 commit comments

Comments
 (0)