Skip to content

Commit e2efdda

Browse files
author
dse
committed
Implicit assembly loading optimization.
1 parent 307f1dd commit e2efdda

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
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: 29 additions & 1 deletion
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);
@@ -526,6 +531,29 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
526531
}
527532
}
528533
}
534+
535+
internal static void RaiseAssemblyAsModuleImportingEvent(ImplicitAssemblyLoadingEventArgs e)
536+
{
537+
ImplicitAssemblyLoading?.Invoke(null, e);
538+
}
539+
}
540+
541+
public class ImplicitAssemblyLoadingEventArgs: EventArgs
542+
{
543+
public ImplicitAssemblyLoadingEventArgs(string moduleName)
544+
{
545+
ModuleName = moduleName;
546+
}
547+
548+
/// <summary>
549+
/// The name of the module to import that is probably assembly name.
550+
/// </summary>
551+
public string ModuleName { get; }
552+
553+
/// <summary>
554+
/// Set it to true, if you know that <see cref="ModuleName"/> is not an assembly to import.
555+
/// </summary>
556+
public bool SkipAssemblyLoad { get; set; }
529557
}
530558

531559
public enum RunFlagType

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy