FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

commit import refactoring · pythonnet/pythonnet@1456e25 · GitHub

Commit 1456e25

Browse files
committed
commit import refactoring
1 parent d66d962 commit 1456e25

33 files changed

Lines changed: 1076 additions & 496 deletions

‎pythonnet/makefile‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,11 @@ python.exe: Python.Runtime.dll
1717
cd ..; cd ..;
1818

1919

20-
Python.Runtime.dll: callconvutil.exe
20+
Python.Runtime.dll:
2121
cd src; cd runtime; \
2222
$(CSC) /nologo /unsafe /target:library /out:../../Python.Runtime.dll \
2323
/recurse:*.cs
2424
cd ..; cd ..;
25-
$(ILDASM) /nobar Python.Runtime.dll /out=Python.Runtime.il;
26-
$(RUNNER) ./callconvutil.exe;
27-
$(ILASM) /nologo /quiet /dll \
28-
/resource=Python.Runtime.res /output=Python.Runtime.dll \
29-
Python.Runtime.il2;
30-
rm -f Python.Runtime.il;
31-
rm -f Python.Runtime.il2;
32-
rm -f Python.Runtime.res;
33-
rm -f ./callconvutil.exe;
3425

3526

3627
CLR.dll: Python.Runtime.dll
@@ -46,23 +37,14 @@ Python.Test.dll: Python.Runtime.dll
4637
cd ..; cd ..;
4738

4839

49-
callconvutil.exe:
50-
cd src; cd tools; \
51-
$(CSC) /nologo /target:exe /out:../../callconvutil.exe \
52-
/recurse:*.cs
53-
cd ..; cd ..;
54-
55-
5640
clean:
5741
rm -f python.exe Python*.dll Python*.il Python*.il2 Python*.res
58-
rm -f callconvutil.exe
5942
rm -f CLR.dll
6043
rm -f ./*~
6144
cd src; cd console; rm -f *~; cd ..; cd ..;
6245
cd src; cd runtime; rm -f *~; cd ..; cd ..;
6346
cd src; cd testing; rm -f *~; cd ..; cd ..;
6447
cd src; cd tests; rm -f *~; rm -f *.pyc; cd ..; cd ..;
65-
cd src; cd tools; rm -f *~; cd ..; cd ..;
6648
cd doc; rm -f *~; cd ..;
6749
cd demo; rm -f *~; cd ..;
6850

‎pythonnet/src/console/assemblyinfo.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using System.Runtime.InteropServices;
1616

1717
[assembly: System.Reflection.AssemblyProduct("Python for .NET")]
18-
[assembly: System.Reflection.AssemblyVersion("1.0.0.0")]
18+
[assembly: System.Reflection.AssemblyVersion("2.0.0.0")]
1919
[assembly: AssemblyTitleAttribute("Python Console")]
2020
[assembly: AssemblyDefaultAliasAttribute("python.exe")]
2121
[assembly: CLSCompliant(true)]

‎pythonnet/src/runtime/arrayobject.cs‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System;
1313
using System.Collections;
1414
using System.Reflection;
15-
using System.Runtime.InteropServices;
1615

1716
namespace Python.Runtime {
1817

@@ -30,7 +29,6 @@ internal override bool CanSubclass() {
3029
return false;
3130
}
3231

33-
[CallConvCdecl()]
3432
public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) {
3533
string message = "cannot instantiate array wrapper";
3634
return Exceptions.RaiseTypeError(message);
@@ -41,7 +39,6 @@ public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) {
4139
// Implements __getitem__ for array types.
4240
//====================================================================
4341

44-
[CallConvCdecl()]
4542
public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) {
4643
CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob);
4744
Array items = obj.inst as Array;
@@ -129,7 +126,6 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) {
129126
// Implements __setitem__ for array types.
130127
//====================================================================
131128

132-
[CallConvCdecl()]
133129
public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) {
134130
CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob);
135131
Array items = obj.inst as Array;
@@ -215,7 +211,6 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) {
215211
// Implements __contains__ for array types.
216212
//====================================================================
217213

218-
[CallConvCdecl()]
219214
public static int sq_contains(IntPtr ob, IntPtr v) {
220215
CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob);
221216
Type itemType = obj.inst.GetType().GetElementType();
@@ -238,7 +233,6 @@ public static int sq_contains(IntPtr ob, IntPtr v) {
238233
// Implements __len__ for array types.
239234
//====================================================================
240235

241-
[CallConvCdecl()]
242236
public static int mp_length(IntPtr ob) {
243237
CLRObject self = (CLRObject)ManagedType.GetManagedObject(ob);
244238
Array items = self.inst as Array;

‎pythonnet/src/runtime/assemblymanager.cs‎

Lines changed: 120 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,64 @@
1-
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
2-
//
3-
// All Rights Reserved.
1+
// ===========================================================================
42
//
53
// This software is subject to the provisions of the Zope Public License,
6-
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
4+
// Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
75
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
86
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
97
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
108
// FOR A PARTICULAR PURPOSE.
9+
//
10+
// ===========================================================================
1111

1212
using System;
1313
using System.IO;
1414
using System.Collections;
1515
using System.Collections.Specialized;
16+
using System.Collections.Generic;
1617
using System.Reflection;
1718
using System.Reflection.Emit;
1819

1920
namespace Python.Runtime {
2021

2122
/// <summary>
22-
/// The AssemblyManager maintains information about the assemblies and
23-
/// namespaces that have been loaded, and provides a simplified internal
24-
/// interface for finding and obtaining Type objects by qualified names.
23+
/// The AssemblyManager maintains information about loaded assemblies
24+
/// namespaces and provides an interface for name-based type lookup.
2525
/// </summary>
2626

2727
internal class AssemblyManager {
2828

29-
static StringCollection pypath;
30-
static AssemblyLoadEventHandler lh;
31-
static ResolveEventHandler rh;
32-
static Hashtable namespaces;
33-
static ArrayList assemblies;
34-
static Hashtable probed;
29+
static AssemblyLoadEventHandler lhandler;
30+
static ResolveEventHandler rhandler;
31+
static Dictionary<string, string> snames;
32+
static Dictionary<string, Dictionary<Assembly, string>> namespaces;
33+
static Dictionary<string, int> probed;
34+
static List<Assembly> assemblies;
35+
static List<string> pypath;
3536
static int last;
3637

3738
private AssemblyManager() {}
3839

3940
//===================================================================
40-
// Initialization performed on startup of the Python runtime.
41+
// Initialization performed on startup of the Python runtime. Here we
42+
// scan all of the currently loaded assemblies to determine exported
43+
// names, and register to be notified of new assembly loads.
4144
//===================================================================
4245

4346
internal static void Initialize() {
44-
pypath = new StringCollection();
45-
namespaces = new Hashtable();
46-
assemblies = new ArrayList();
47-
probed = new Hashtable();
47+
snames = new Dictionary<string, string>();
48+
49+
namespaces = new
50+
Dictionary<string, Dictionary<Assembly, string>>(32);
51+
probed = new Dictionary<string, int>(32);
52+
assemblies = new List<Assembly>(16);
53+
pypath = new List<string>(16);
4854

4955
AppDomain domain = AppDomain.CurrentDomain;
5056

51-
lh = new AssemblyLoadEventHandler(AssemblyLoadHandler);
52-
domain.AssemblyLoad += lh;
57+
lhandler = new AssemblyLoadEventHandler(AssemblyLoadHandler);
58+
domain.AssemblyLoad += lhandler;
5359

54-
rh = new ResolveEventHandler(ResolveHandler);
55-
domain.AssemblyResolve += rh;
60+
rhandler = new ResolveEventHandler(ResolveHandler);
61+
domain.AssemblyResolve += rhandler;
5662

5763
Assembly[] items = domain.GetAssemblies();
5864
for (int i = 0; i < items.Length; i++) {
@@ -69,8 +75,8 @@ internal static void Initialize() {
6975

7076
internal static void Shutdown() {
7177
AppDomain domain = AppDomain.CurrentDomain;
72-
domain.AssemblyLoad -= lh;
73-
domain.AssemblyResolve -= rh;
78+
domain.AssemblyLoad -= lhandler;
79+
domain.AssemblyResolve -= rhandler;
7480
}
7581

7682

@@ -106,8 +112,33 @@ static Assembly ResolveHandler(Object ob, ResolveEventArgs args){
106112
return a;
107113
}
108114
}
109-
Assembly ao = LoadAssemblyPath(args.Name);
110-
return ao;
115+
return LoadAssemblyPath(args.Name);
116+
}
117+
118+
119+
//===================================================================
120+
// Certain kinds of names (such as the names of generic types) are
121+
// mangled so we need to perform mapping from human-friendly names
122+
// to inhumane names. Given a friendly name, this method will either
123+
// return the associated inhumane name or the original name with no
124+
// changes if there is no mapping for that name.
125+
//===================================================================
126+
127+
static string ConvertSpecialName(string name) {
128+
if (snames.ContainsKey(name)) {
129+
return snames[name];
130+
}
131+
return name;
132+
}
133+
134+
static void AddSpecialName(string nice, string ugly) {
135+
if (!snames.ContainsKey(nice)) {
136+
snames.Add(nice, ugly);
137+
}
138+
else {
139+
//Console.WriteLine("dup: {0} : {1}", nice, ugly);
140+
}
141+
111142
}
112143

113144

@@ -218,7 +249,7 @@ public static bool LoadImplicit(string name) {
218249
string s = "";
219250
for (int i = 0; i < names.Length; i++) {
220251
s = (i == 0) ? names[0] : s + "." + names[i];
221-
if (probed[s] == null) {
252+
if (!probed.ContainsKey(s)) {
222253
if (LoadAssemblyPath(s) != null){
223254
loaded = true;
224255
}
@@ -237,32 +268,48 @@ public static bool LoadImplicit(string name) {
237268
// mapping of valid namespaces. Note that for a given namespace
238269
// a.b.c.d, each of a, a.b, a.b.c and a.b.c.d are considered to
239270
// be valid namespaces (to better match Python import semantics).
271+
// {'System' : ['Reflection', 'String']}
240272
//===================================================================
241273

242274
static void ScanAssembly(Assembly assembly) {
243275

244-
// TODO: this is a workaround for a current Mono bug: calling
245-
// GetTypes on a generated assembly will cause it to fall over.
246-
// For now we'll skip generated assemblies, which usually are
247-
// uninteresting support code anyway.
248-
249-
if (assembly is AssemblyBuilder) {
250-
return;
251-
}
276+
// A couple of things we want to do here: first, we want to
277+
// gather a list of all of the namespaces contributed to by
278+
// the assembly. Since we have to rifle through all of the
279+
// types in the assembly anyway, we also build up a running
280+
// list of 'odd names' like generic names so that we can map
281+
// them appropriately later while still being lazy about
282+
// type lookup and instantiation.
252283

253284
Type[] types = assembly.GetTypes();
254285
for (int i = 0; i < types.Length; i++) {
255-
string type_ns = types[i].Namespace;
256-
if ((type_ns != null) && (!namespaces.ContainsKey(type_ns))) {
257-
string[] names = type_ns.Split('.');
286+
Type t = types[i];
287+
string ns = t.Namespace;
288+
if ((ns != null) && (!namespaces.ContainsKey(ns))) {
289+
string[] names = ns.Split('.');
258290
string s = "";
259291
for (int n = 0; n < names.Length; n++) {
260292
s = (n == 0) ? names[0] : s + "." + names[n];
261293
if (!namespaces.ContainsKey(s)) {
262-
namespaces.Add(s, String.Empty);
294+
namespaces.Add(s,
295+
new Dictionary<Assembly, string>()
296+
);
263297
}
264298
}
265299
}
300+
301+
if (ns != null && !namespaces[ns].ContainsKey(assembly)) {
302+
namespaces[ns].Add(assembly, String.Empty);
303+
}
304+
305+
if (t.IsGenericTypeDefinition) {
306+
string qname = t.FullName;
307+
int tick = qname.IndexOf('`');
308+
if (tick != -1) {
309+
AddSpecialName(qname.Substring(0, tick), qname);
310+
}
311+
}
312+
266313
}
267314
}
268315

@@ -277,16 +324,48 @@ public static bool IsValidNamespace(string name) {
277324
}
278325

279326

327+
//===================================================================
328+
// Returns the current list of valid names for the input namespace.
329+
//===================================================================
330+
331+
public static List<string> GetNames(string nsname) {
332+
List<string> names = new List<string>(32);
333+
if (namespaces.ContainsKey(nsname)) {
334+
foreach (Assembly a in namespaces[nsname].Keys) {
335+
Type[] types = a.GetTypes();
336+
for (int i = 0; i < types.Length; i++) {
337+
Type t = types[i];
338+
if (t.Namespace == nsname) {
339+
names.Add(ConvertSpecialName(t.Name));
340+
}
341+
}
342+
}
343+
int nslen = nsname.Length;
344+
foreach (string key in namespaces.Keys) {
345+
if (key.Length > nslen && key.StartsWith(nsname)) {
346+
string tail = key.Substring(nslen);
347+
if (key.IndexOf('.') == -1) {
348+
names.Add(key);
349+
}
350+
}
351+
}
352+
}
353+
return names;
354+
}
355+
280356
//===================================================================
281357
// Returns the System.Type object for a given qualified name,
282358
// looking in the currently loaded assemblies for the named
283359
// type. Returns null if the named type cannot be found.
284360
//===================================================================
285361

286-
public static Type LookupType(string qualifiedName) {
362+
// funny names: generics,
363+
364+
public static Type LookupType(string qname) {
365+
string name = ConvertSpecialName(qname);
287366
for (int i = 0; i < assemblies.Count; i++) {
288367
Assembly assembly = (Assembly)assemblies[i];
289-
Type type = assembly.GetType(qualifiedName);
368+
Type type = assembly.GetType(name);
290369
if (type != null) {
291370
return type;
292371
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL