Skip to content

Commit be8154f

Browse files
author
dse
committed
Merge
2 parents 866de1a + e2efdda commit be8154f

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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 (NetCoreApp2.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

Lines changed: 13 additions & 2 deletions
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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public static string Compiler
136136

137137
internal static PyReferenceDecrementer CurrentRefDecrementer { get; private set; }
138138

139+
/// <summary>
140+
/// Fires when python engines importing module and probably tries to load an assembly.
141+
/// </summary>
142+
public static event EventHandler<ImplicitAssemblyLoadingEventArgs> ImplicitAssemblyLoading;
143+
139144
public static int RunSimpleString(string code)
140145
{
141146
return Runtime.PyRun_SimpleString(code);
@@ -549,6 +554,29 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
549554
}
550555
}
551556
}
557+
558+
internal static void RaiseAssemblyAsModuleImportingEvent(ImplicitAssemblyLoadingEventArgs e)
559+
{
560+
ImplicitAssemblyLoading?.Invoke(null, e);
561+
}
562+
}
563+
564+
public class ImplicitAssemblyLoadingEventArgs: EventArgs
565+
{
566+
public ImplicitAssemblyLoadingEventArgs(string moduleName)
567+
{
568+
ModuleName = moduleName;
569+
}
570+
571+
/// <summary>
572+
/// The name of the module to import that is probably assembly name.
573+
/// </summary>
574+
public string ModuleName { get; }
575+
576+
/// <summary>
577+
/// Set it to true, if you know that <see cref="ModuleName"/> is not an assembly to import.
578+
/// </summary>
579+
public bool SkipAssemblyLoad { get; set; }
552580
}
553581

554582
public enum RunFlagType

0 commit comments

Comments
 (0)