forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassemblymanager.cs
More file actions
432 lines (368 loc) · 17.2 KB
/
assemblymanager.cs
File metadata and controls
432 lines (368 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// ==========================================================================
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
// ==========================================================================
using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
namespace Python.Runtime {
/// <summary>
/// The AssemblyManager maintains information about loaded assemblies
/// namespaces and provides an interface for name-based type lookup.
/// </summary>
internal class AssemblyManager {
static Dictionary<string, Dictionary<Assembly, string>> namespaces;
//static Dictionary<string, Dictionary<string, string>> generics;
static AssemblyLoadEventHandler lhandler;
static ResolveEventHandler rhandler;
static Dictionary<string, int> probed;
static List<Assembly> assemblies;
internal static List<string> pypath;
private AssemblyManager() {}
//===================================================================
// Initialization performed on startup of the Python runtime. Here we
// scan all of the currently loaded assemblies to determine exported
// names, and register to be notified of new assembly loads.
//===================================================================
internal static void Initialize() {
namespaces = new
Dictionary<string, Dictionary<Assembly, string>>(32);
probed = new Dictionary<string, int>(32);
//generics = new Dictionary<string, Dictionary<string, string>>();
assemblies = new List<Assembly>(16);
pypath = new List<string>(16);
AppDomain domain = AppDomain.CurrentDomain;
lhandler = new AssemblyLoadEventHandler(AssemblyLoadHandler);
domain.AssemblyLoad += lhandler;
rhandler = new ResolveEventHandler(ResolveHandler);
domain.AssemblyResolve += rhandler;
Assembly[] items = domain.GetAssemblies();
foreach (var a in items)
{
try
{
ScanAssembly(a);
assemblies.Add(a);
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("Error scanning assembly {0}. {1}", a, ex));
}
}
}
//===================================================================
// Cleanup resources upon shutdown of the Python runtime.
//===================================================================
internal static void Shutdown() {
AppDomain domain = AppDomain.CurrentDomain;
domain.AssemblyLoad -= lhandler;
domain.AssemblyResolve -= rhandler;
}
//===================================================================
// Event handler for assembly load events. At the time the Python
// runtime loads, we scan the app domain to map the assemblies that
// are loaded at the time. We also have to register this event handler
// so that we can know about assemblies that get loaded after the
// Python runtime is initialized.
//===================================================================
static void AssemblyLoadHandler(Object ob, AssemblyLoadEventArgs args){
Assembly assembly = args.LoadedAssembly;
assemblies.Add(assembly);
ScanAssembly(assembly);
}
//===================================================================
// Event handler for assembly resolve events. This is needed because
// we augment the assembly search path with the PYTHONPATH when we
// load an assembly from Python. Because of that, we need to listen
// for failed loads, because they might be dependencies of something
// we loaded from Python which also needs to be found on PYTHONPATH.
//===================================================================
static Assembly ResolveHandler(Object ob, ResolveEventArgs args){
string name = args.Name.ToLower();
for (int i = 0; i < assemblies.Count; i++) {
Assembly a = (Assembly)assemblies[i];
string full = a.FullName.ToLower();
if (full.StartsWith(name)) {
return a;
}
}
return LoadAssemblyPath(args.Name);
}
//===================================================================
// We __really__ want to avoid using Python objects or APIs when
// probing for assemblies to load, since our ResolveHandler may be
// called in contexts where we don't have the Python GIL and can't
// even safely try to get it without risking a deadlock ;(
//
// To work around that, we update a managed copy of sys.path (which
// is the main thing we care about) when UpdatePath is called. The
// import hook calls this whenever it knows its about to use the
// assembly manager, which lets us keep up with changes to sys.path
// in a relatively lightweight and low-overhead way.
//===================================================================
internal static void UpdatePath() {
IntPtr list = Runtime.PySys_GetObject("path");
int count = Runtime.PyList_Size(list);
if (count != pypath.Count) {
pypath.Clear();
probed.Clear();
for (int i = 0; i < count; i++) {
IntPtr item = Runtime.PyList_GetItem(list, i);
string path = Runtime.GetManagedString(item);
if (path != null) {
pypath.Add(path);
}
}
}
}
//===================================================================
// Given an assembly name, try to find this assembly file using the
// PYTHONPATH. If not found, return null to indicate implicit load
// using standard load semantics (app base directory then GAC, etc.)
//===================================================================
public static string FindAssembly(string name) {
char sep = Path.DirectorySeparatorChar;
string path;
string temp;
for (int i = 0; i < pypath.Count; i++) {
string head = pypath[i];
if (head == null || head.Length == 0) {
path = name;
}
else {
path = head + sep + name;
}
temp = path + ".dll";
if (File.Exists(temp)) {
return temp;
}
temp = path + ".exe";
if (File.Exists(temp)) {
return temp;
}
}
return null;
}
//===================================================================
// Loads an assembly from the application directory or the GAC
// given a simple assembly name. Returns the assembly if loaded.
//===================================================================
public static Assembly LoadAssembly(string name) {
Assembly assembly = null;
try {
assembly = Assembly.Load(name);
}
catch (System.Exception e) {
//if (!(e is System.IO.FileNotFoundException)) {
// throw;
//}
}
return assembly;
}
//===================================================================
// Loads an assembly using an augmented search path (the python path).
//===================================================================
public static Assembly LoadAssemblyPath(string name) {
string path = FindAssembly(name);
Assembly assembly = null;
if (path != null) {
try { assembly = Assembly.LoadFrom(path); }
catch {}
}
return assembly;
}
/// <summary>
/// Loads an assembly using full path.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static Assembly LoadAssemblyFullPath(string name) {
Assembly assembly = null;
if (Path.IsPathRooted(name)) {
if (!Path.HasExtension(name))
name = name + ".dll";
if (File.Exists(name)) {
try { assembly = Assembly.LoadFrom(name); }
catch { }
}
}
return assembly;
}
//===================================================================
// Returns an assembly that's already been loaded
//===================================================================
public static Assembly FindLoadedAssembly(string name) {
for (int i = 0; i < assemblies.Count; i++) {
Assembly a = (Assembly)assemblies[i];
if (a.GetName().Name == name) {
return a;
}
}
return null;
}
//===================================================================
// Given a qualified name of the form A.B.C.D, attempt to load
// an assembly named after each of A.B.C.D, A.B.C, A.B, A. This
// will only actually probe for the assembly once for each unique
// namespace. Returns true if any assemblies were loaded.
// TODO item 3 "* Deprecate implicit loading of assemblies":
// Set the fromFile flag if the name of the loaded assembly matches
// the fully qualified name that was requested if the framework
// actually loads an assembly.
// Call ONLY for namespaces that HAVE NOT been cached yet.
//===================================================================
public static bool LoadImplicit(string name, bool warn=true) {
string[] names = name.Split('.');
bool loaded = false;
string s = "";
Assembly lastAssembly = null;
HashSet<Assembly> assemblies = null;
for (int i = 0; i < names.Length; i++) {
s = (i == 0) ? names[0] : s + "." + names[i];
if (!probed.ContainsKey(s)) {
if (assemblies == null) {
assemblies = new HashSet<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
}
Assembly a = FindLoadedAssembly(s);
if (a == null) {
a = LoadAssemblyPath(s);
}
if (a == null) {
a = LoadAssembly(s);
}
if (a != null && !assemblies.Contains(a)) {
loaded = true;
lastAssembly = a;
}
probed[s] = 1;
}
}
// Deprecation warning
if (warn && loaded)
{
string deprWarning = String.Format(
"\nThe module was found, but not in a referenced namespace.\n" +
"Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").",
Path.GetFileNameWithoutExtension(lastAssembly.Location));
Exceptions.deprecation(deprWarning);
}
return loaded;
}
//===================================================================
// Scans an assembly for exported namespaces, adding them to the
// mapping of valid namespaces. Note that for a given namespace
// a.b.c.d, each of a, a.b, a.b.c and a.b.c.d are considered to
// be valid namespaces (to better match Python import semantics).
//===================================================================
internal static void ScanAssembly(Assembly assembly) {
// A couple of things we want to do here: first, we want to
// gather a list of all of the namespaces contributed to by
// the assembly.
Type[] types = assembly.GetTypes();
for (int i = 0; i < types.Length; i++) {
Type t = types[i];
string ns = t.Namespace ?? "";
if (!namespaces.ContainsKey(ns)) {
string[] names = ns.Split('.');
string s = "";
for (int n = 0; n < names.Length; n++) {
s = (n == 0) ? names[0] : s + "." + names[n];
if (!namespaces.ContainsKey(s)) {
namespaces.Add(s, new Dictionary<Assembly, string>());
}
}
}
if (ns != null && !namespaces[ns].ContainsKey(assembly)) {
namespaces[ns].Add(assembly, String.Empty);
}
if (ns != null && t.IsGenericTypeDefinition) {
GenericUtil.Register(t);
}
}
}
public static AssemblyName[] ListAssemblies()
{
AssemblyName[] names = new AssemblyName[assemblies.Count];
Assembly assembly;
for (int i=0; i < assemblies.Count; i++)
{
assembly = assemblies[i];
names.SetValue(assembly.GetName(), i);
}
return names;
}
//===================================================================
// Returns true if the given qualified name matches a namespace
// exported by an assembly loaded in the current app domain.
//===================================================================
public static bool IsValidNamespace(string name) {
return namespaces.ContainsKey(name);
}
//===================================================================
// Returns list of assemblies that declare types in a given namespace
//===================================================================
public static IEnumerable<Assembly> GetAssemblies(string nsname) {
if (!namespaces.ContainsKey(nsname))
return new List<Assembly>();
return namespaces[nsname].Keys;
}
//===================================================================
// Returns the current list of valid names for the input namespace.
//===================================================================
public static List<string> GetNames(string nsname) {
//Dictionary<string, int> seen = new Dictionary<string, int>();
List<string> names = new List<string>(8);
List<string> g = GenericUtil.GetGenericBaseNames(nsname);
if (g != null) {
foreach (string n in g) {
names.Add(n);
}
}
if (namespaces.ContainsKey(nsname)) {
foreach (Assembly a in namespaces[nsname].Keys) {
Type[] types = a.GetTypes();
for (int i = 0; i < types.Length; i++) {
Type t = types[i];
if ((t.Namespace ?? "") == nsname) {
names.Add(t.Name);
}
}
}
int nslen = nsname.Length;
foreach (string key in namespaces.Keys) {
if (key.Length > nslen && key.StartsWith(nsname)) {
//string tail = key.Substring(nslen);
if (key.IndexOf('.') == -1) {
names.Add(key);
}
}
}
}
return names;
}
//===================================================================
// Returns the System.Type object for a given qualified name,
// looking in the currently loaded assemblies for the named
// type. Returns null if the named type cannot be found.
//===================================================================
public static Type LookupType(string qname) {
for (int i = 0; i < assemblies.Count; i++) {
Assembly assembly = (Assembly)assemblies[i];
Type type = assembly.GetType(qname);
if (type != null) {
return type;
}
}
return null;
}
}
}