X Tutup
Skip to content

Commit a2f303b

Browse files
author
dse
committed
Merge
2 parents 2f5c7b2 + d09fc92 commit a2f303b

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1212
Usage: Python.EmbeddingTest.exe --loop --where="cat != Unstable"
1313
- Improved performance. String marshaling between python and clr now cached.
1414
Cache reduces GC pressure and saves from extensive memory copying.
15+
- Optimized implicit assembly loading on module import, PythonEngine.ImplicitAssemblyLoading event added.
1516
- Added support for embedding python into dotnet core 2.0 (NetStandard 2.0)
1617
- Added new build system (pythonnet.15.sln) based on dotnetcore-sdk/xplat(crossplatform msbuild).
1718
Currently there two side-by-side build systems that produces the same output (net40) from the same sources.

src/runtime/assemblymanager.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,17 @@ public static Assembly LoadAssembly(string name)
194194
Assembly assembly = null;
195195
try
196196
{
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+
}
198208
}
199209
catch (Exception)
200210
{
@@ -341,8 +351,17 @@ internal static void ScanAssembly(Assembly assembly)
341351
// A couple of things we want to do here: first, we want to
342352
// gather a list of all of the namespaces contributed to by
343353
// 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+
}
344364

345-
Type[] types = assembly.GetTypes();
346365
foreach (Type t in types)
347366
{
348367
string ns = t.Namespace ?? "";
@@ -417,12 +436,15 @@ public static List<string> GetNames(string nsname)
417436
{
418437
foreach (Assembly a in namespaces[nsname].Keys)
419438
{
420-
Type[] types = a.GetTypes();
439+
Type[] types = a.IsDynamic ? a.GetTypes(): a.GetExportedTypes();
421440
foreach (Type t in types)
422441
{
423442
if ((t.Namespace ?? "") == nsname)
424443
{
425-
names.Add(t.Name);
444+
if (!t.IsNested)
445+
{
446+
names.Add(t.Name);
447+
}
426448
}
427449
}
428450
}

src/runtime/pythonengine.cs

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

140140
internal static PyReferenceDecrementer CurrentRefDecrementer { get; private set; }
141141

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+
142147
public static int RunSimpleString(string code)
143148
{
144149
return Runtime.PyRun_SimpleString(code);
@@ -554,6 +559,29 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
554559
}
555560
}
556561
}
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; }
557585
}
558586

559587
public enum RunFlagType

0 commit comments

Comments
 (0)
X Tutup