| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d66d962 commit 1456e25
33 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,20 +17,11 @@ python.exe: Python.Runtime.dll | |||
| 17 | 17 | cd ..; cd ..; | |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | - Python.Runtime.dll: callconvutil.exe | ||
| 20 | + Python.Runtime.dll: | ||
| 21 | 21 | cd src; cd runtime; \ | |
| 22 | 22 | $(CSC) /nologo /unsafe /target:library /out:../../Python.Runtime.dll \ | |
| 23 | 23 | /recurse:*.cs | |
| 24 | 24 | 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; | ||
| 34 | 25 | ||
| 35 | 26 | ||
| 36 | 27 | CLR.dll: Python.Runtime.dll | |
@@ -46,23 +37,14 @@ Python.Test.dll: Python.Runtime.dll | |||
| 46 | 37 | cd ..; cd ..; | |
| 47 | 38 | ||
| 48 | 39 | ||
| 49 | - callconvutil.exe: | ||
| 50 | - cd src; cd tools; \ | ||
| 51 | - $(CSC) /nologo /target:exe /out:../../callconvutil.exe \ | ||
| 52 | - /recurse:*.cs | ||
| 53 | - cd ..; cd ..; | ||
| 54 | - | ||
| 55 | - | ||
| 56 | 40 | clean: | |
| 57 | 41 | rm -f python.exe Python*.dll Python*.il Python*.il2 Python*.res | |
| 58 | - rm -f callconvutil.exe | ||
| 59 | 42 | rm -f CLR.dll | |
| 60 | 43 | rm -f ./*~ | |
| 61 | 44 | cd src; cd console; rm -f *~; cd ..; cd ..; | |
| 62 | 45 | cd src; cd runtime; rm -f *~; cd ..; cd ..; | |
| 63 | 46 | cd src; cd testing; rm -f *~; cd ..; cd ..; | |
| 64 | 47 | cd src; cd tests; rm -f *~; rm -f *.pyc; cd ..; cd ..; | |
| 65 | - cd src; cd tools; rm -f *~; cd ..; cd ..; | ||
| 66 | 48 | cd doc; rm -f *~; cd ..; | |
| 67 | 49 | cd demo; rm -f *~; cd ..; | |
| 68 | 50 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | using System.Runtime.InteropServices; | |
| 16 | 16 | ||
| 17 | 17 | [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")] | ||
| 19 | 19 | [assembly: AssemblyTitleAttribute("Python Console")] | |
| 20 | 20 | [assembly: AssemblyDefaultAliasAttribute("python.exe")] | |
| 21 | 21 | [assembly: CLSCompliant(true)] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,6 @@ | |||
| 12 | 12 | using System; | |
| 13 | 13 | using System.Collections; | |
| 14 | 14 | using System.Reflection; | |
| 15 | - using System.Runtime.InteropServices; | ||
| 16 | 15 | ||
| 17 | 16 | namespace Python.Runtime { | |
| 18 | 17 | ||
@@ -30,7 +29,6 @@ internal override bool CanSubclass() { | |||
| 30 | 29 | return false; | |
| 31 | 30 | } | |
| 32 | 31 | ||
| 33 | - [CallConvCdecl()] | ||
| 34 | 32 | public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) { | |
| 35 | 33 | string message = "cannot instantiate array wrapper"; | |
| 36 | 34 | return Exceptions.RaiseTypeError(message); | |
@@ -41,7 +39,6 @@ public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) { | |||
| 41 | 39 | // Implements __getitem__ for array types. | |
| 42 | 40 | //==================================================================== | |
| 43 | 41 | ||
| 44 | - [CallConvCdecl()] | ||
| 45 | 42 | public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) { | |
| 46 | 43 | CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob); | |
| 47 | 44 | Array items = obj.inst as Array; | |
@@ -129,7 +126,6 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) { | |||
| 129 | 126 | // Implements __setitem__ for array types. | |
| 130 | 127 | //==================================================================== | |
| 131 | 128 | ||
| 132 | - [CallConvCdecl()] | ||
| 133 | 129 | public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) { | |
| 134 | 130 | CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob); | |
| 135 | 131 | Array items = obj.inst as Array; | |
@@ -215,7 +211,6 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) { | |||
| 215 | 211 | // Implements __contains__ for array types. | |
| 216 | 212 | //==================================================================== | |
| 217 | 213 | ||
| 218 | - [CallConvCdecl()] | ||
| 219 | 214 | public static int sq_contains(IntPtr ob, IntPtr v) { | |
| 220 | 215 | CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob); | |
| 221 | 216 | Type itemType = obj.inst.GetType().GetElementType(); | |
@@ -238,7 +233,6 @@ public static int sq_contains(IntPtr ob, IntPtr v) { | |||
| 238 | 233 | // Implements __len__ for array types. | |
| 239 | 234 | //==================================================================== | |
| 240 | 235 | ||
| 241 | - [CallConvCdecl()] | ||
| 242 | 236 | public static int mp_length(IntPtr ob) { | |
| 243 | 237 | CLRObject self = (CLRObject)ManagedType.GetManagedObject(ob); | |
| 244 | 238 | Array items = self.inst as Array; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,58 +1,64 @@ | |||
| 1 | - // Copyright (c) 2001, 2002 Zope Corporation and Contributors. | ||
| 2 | - // | ||
| 3 | - // All Rights Reserved. | ||
| 1 | + // =========================================================================== | ||
| 4 | 2 | // | |
| 5 | 3 | // 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. | ||
| 7 | 5 | // THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 | 6 | // WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 | 7 | // WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 | 8 | // FOR A PARTICULAR PURPOSE. | |
| 9 | + // | ||
| 10 | + // =========================================================================== | ||
| 11 | 11 | ||
| 12 | 12 | using System; | |
| 13 | 13 | using System.IO; | |
| 14 | 14 | using System.Collections; | |
| 15 | 15 | using System.Collections.Specialized; | |
| 16 | + using System.Collections.Generic; | ||
| 16 | 17 | using System.Reflection; | |
| 17 | 18 | using System.Reflection.Emit; | |
| 18 | 19 | ||
| 19 | 20 | namespace Python.Runtime { | |
| 20 | 21 | ||
| 21 | 22 | /// <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. | ||
| 25 | 25 | /// </summary> | |
| 26 | 26 | ||
| 27 | 27 | internal class AssemblyManager { | |
| 28 | 28 | ||
| 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; | ||
| 35 | 36 | static int last; | |
| 36 | 37 | ||
| 37 | 38 | private AssemblyManager() {} | |
| 38 | 39 | ||
| 39 | 40 | //=================================================================== | |
| 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. | ||
| 41 | 44 | //=================================================================== | |
| 42 | 45 | ||
| 43 | 46 | 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); | ||
| 48 | 54 | ||
| 49 | 55 | AppDomain domain = AppDomain.CurrentDomain; | |
| 50 | 56 | ||
| 51 | - lh = new AssemblyLoadEventHandler(AssemblyLoadHandler); | ||
| 52 | - domain.AssemblyLoad += lh; | ||
| 57 | + lhandler = new AssemblyLoadEventHandler(AssemblyLoadHandler); | ||
| 58 | + domain.AssemblyLoad += lhandler; | ||
| 53 | 59 | ||
| 54 | - rh = new ResolveEventHandler(ResolveHandler); | ||
| 55 | - domain.AssemblyResolve += rh; | ||
| 60 | + rhandler = new ResolveEventHandler(ResolveHandler); | ||
| 61 | + domain.AssemblyResolve += rhandler; | ||
| 56 | 62 | ||
| 57 | 63 | Assembly[] items = domain.GetAssemblies(); | |
| 58 | 64 | for (int i = 0; i < items.Length; i++) { | |
@@ -69,8 +75,8 @@ internal static void Initialize() { | |||
| 69 | 75 | ||
| 70 | 76 | internal static void Shutdown() { | |
| 71 | 77 | AppDomain domain = AppDomain.CurrentDomain; | |
| 72 | - domain.AssemblyLoad -= lh; | ||
| 73 | - domain.AssemblyResolve -= rh; | ||
| 78 | + domain.AssemblyLoad -= lhandler; | ||
| 79 | + domain.AssemblyResolve -= rhandler; | ||
| 74 | 80 | } | |
| 75 | 81 | ||
| 76 | 82 | ||
@@ -106,8 +112,33 @@ static Assembly ResolveHandler(Object ob, ResolveEventArgs args){ | |||
| 106 | 112 | return a; | |
| 107 | 113 | } | |
| 108 | 114 | } | |
| 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 | + | ||
| 111 | 142 | } | |
| 112 | 143 | ||
| 113 | 144 | ||
@@ -218,7 +249,7 @@ public static bool LoadImplicit(string name) { | |||
| 218 | 249 | string s = ""; | |
| 219 | 250 | for (int i = 0; i < names.Length; i++) { | |
| 220 | 251 | s = (i == 0) ? names[0] : s + "." + names[i]; | |
| 221 | - if (probed[s] == null) { | ||
| 252 | + if (!probed.ContainsKey(s)) { | ||
| 222 | 253 | if (LoadAssemblyPath(s) != null){ | |
| 223 | 254 | loaded = true; | |
| 224 | 255 | } | |
@@ -237,32 +268,48 @@ public static bool LoadImplicit(string name) { | |||
| 237 | 268 | // mapping of valid namespaces. Note that for a given namespace | |
| 238 | 269 | // a.b.c.d, each of a, a.b, a.b.c and a.b.c.d are considered to | |
| 239 | 270 | // be valid namespaces (to better match Python import semantics). | |
| 271 | + // {'System' : ['Reflection', 'String']} | ||
| 240 | 272 | //=================================================================== | |
| 241 | 273 | ||
| 242 | 274 | static void ScanAssembly(Assembly assembly) { | |
| 243 | 275 | ||
| 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. | ||
| 252 | 283 | ||
| 253 | 284 | Type[] types = assembly.GetTypes(); | |
| 254 | 285 | 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('.'); | ||
| 258 | 290 | string s = ""; | |
| 259 | 291 | for (int n = 0; n < names.Length; n++) { | |
| 260 | 292 | s = (n == 0) ? names[0] : s + "." + names[n]; | |
| 261 | 293 | if (!namespaces.ContainsKey(s)) { | |
| 262 | - namespaces.Add(s, String.Empty); | ||
| 294 | + namespaces.Add(s, | ||
| 295 | + new Dictionary<Assembly, string>() | ||
| 296 | + ); | ||
| 263 | 297 | } | |
| 264 | 298 | } | |
| 265 | 299 | } | |
| 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 | + | ||
| 266 | 313 | } | |
| 267 | 314 | } | |
| 268 | 315 | ||
@@ -277,16 +324,48 @@ public static bool IsValidNamespace(string name) { | |||
| 277 | 324 | } | |
| 278 | 325 | ||
| 279 | 326 | ||
| 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 | + | ||
| 280 | 356 | //=================================================================== | |
| 281 | 357 | // Returns the System.Type object for a given qualified name, | |
| 282 | 358 | // looking in the currently loaded assemblies for the named | |
| 283 | 359 | // type. Returns null if the named type cannot be found. | |
| 284 | 360 | //=================================================================== | |
| 285 | 361 | ||
| 286 | - public static Type LookupType(string qualifiedName) { | ||
| 362 | + // funny names: generics, | ||
| 363 | + | ||
| 364 | + public static Type LookupType(string qname) { | ||
| 365 | + string name = ConvertSpecialName(qname); | ||
| 287 | 366 | for (int i = 0; i < assemblies.Count; i++) { | |
| 288 | 367 | Assembly assembly = (Assembly)assemblies[i]; | |
| 289 | - Type type = assembly.GetType(qualifiedName); | ||
| 368 | + Type type = assembly.GetType(name); | ||
| 290 | 369 | if (type != null) { | |
| 291 | 370 | return type; | |
| 292 | 371 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments