| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 432467f commit 7cf4ba9
38 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,5 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections; | |
| 3 | - using System.Reflection; | ||
| 4 | 3 | ||
| 5 | 4 | namespace Python.Runtime | |
| 6 | 5 | { | |
@@ -22,13 +21,13 @@ internal override bool CanSubclass() | |||
| 22 | 21 | ||
| 23 | 22 | public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) | |
| 24 | 23 | { | |
| 25 | - ArrayObject self = GetManagedObject(tp) as ArrayObject; | ||
| 24 | + var self = GetManagedObject(tp) as ArrayObject; | ||
| 26 | 25 | if (Runtime.PyTuple_Size(args) != 1) | |
| 27 | 26 | { | |
| 28 | 27 | return Exceptions.RaiseTypeError("array expects 1 argument"); | |
| 29 | 28 | } | |
| 30 | 29 | IntPtr op = Runtime.PyTuple_GetItem(args, 0); | |
| 31 | - Object result; | ||
| 30 | + object result; | ||
| 32 | 31 | ||
| 33 | 32 | if (!Converter.ToManaged(op, self.type, out result, true)) | |
| 34 | 33 | { | |
@@ -43,11 +42,11 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) | |||
| 43 | 42 | /// </summary> | |
| 44 | 43 | public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) | |
| 45 | 44 | { | |
| 46 | - CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob); | ||
| 47 | - Array items = obj.inst as Array; | ||
| 45 | + var obj = (CLRObject)GetManagedObject(ob); | ||
| 46 | + var items = obj.inst as Array; | ||
| 48 | 47 | Type itemType = obj.inst.GetType().GetElementType(); | |
| 49 | 48 | int rank = items.Rank; | |
| 50 | - int index = 0; | ||
| 49 | + int index; | ||
| 51 | 50 | object value; | |
| 52 | 51 | ||
| 53 | 52 | // Note that CLR 1.0 only supports int indexes - methods to | |
@@ -61,7 +60,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) | |||
| 61 | 60 | ||
| 62 | 61 | if (rank == 1) | |
| 63 | 62 | { | |
| 64 | - index = (int)Runtime.PyInt_AsLong(idx); | ||
| 63 | + index = Runtime.PyInt_AsLong(idx); | ||
| 65 | 64 | ||
| 66 | 65 | if (Exceptions.ErrorOccurred()) | |
| 67 | 66 | { | |
@@ -83,7 +82,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) | |||
| 83 | 82 | return IntPtr.Zero; | |
| 84 | 83 | } | |
| 85 | 84 | ||
| 86 | - return Converter.ToPython(items.GetValue(index), itemType); | ||
| 85 | + return Converter.ToPython(value, itemType); | ||
| 87 | 86 | } | |
| 88 | 87 | ||
| 89 | 88 | // Multi-dimensional arrays can be indexed a la: list[1, 2, 3]. | |
@@ -96,12 +95,12 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) | |||
| 96 | 95 | ||
| 97 | 96 | int count = Runtime.PyTuple_Size(idx); | |
| 98 | 97 | ||
| 99 | - Array args = Array.CreateInstance(typeof(Int32), count); | ||
| 98 | + var args = new int[count]; | ||
| 100 | 99 | ||
| 101 | - for (int i = 0; i < count; i++) | ||
| 100 | + for (var i = 0; i < count; i++) | ||
| 102 | 101 | { | |
| 103 | 102 | IntPtr op = Runtime.PyTuple_GetItem(idx, i); | |
| 104 | - index = (int)Runtime.PyInt_AsLong(op); | ||
| 103 | + index = Runtime.PyInt_AsLong(op); | ||
| 105 | 104 | ||
| 106 | 105 | if (Exceptions.ErrorOccurred()) | |
| 107 | 106 | { | |
@@ -135,11 +134,11 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx) | |||
| 135 | 134 | /// </summary> | |
| 136 | 135 | public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) | |
| 137 | 136 | { | |
| 138 | - CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob); | ||
| 139 | - Array items = obj.inst as Array; | ||
| 137 | + var obj = (CLRObject)GetManagedObject(ob); | ||
| 138 | + var items = obj.inst as Array; | ||
| 140 | 139 | Type itemType = obj.inst.GetType().GetElementType(); | |
| 141 | 140 | int rank = items.Rank; | |
| 142 | - int index = 0; | ||
| 141 | + int index; | ||
| 143 | 142 | object value; | |
| 144 | 143 | ||
| 145 | 144 | if (items.IsReadOnly) | |
@@ -155,7 +154,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) | |||
| 155 | 154 | ||
| 156 | 155 | if (rank == 1) | |
| 157 | 156 | { | |
| 158 | - index = (int)Runtime.PyInt_AsLong(idx); | ||
| 157 | + index = Runtime.PyInt_AsLong(idx); | ||
| 159 | 158 | ||
| 160 | 159 | if (Exceptions.ErrorOccurred()) | |
| 161 | 160 | { | |
@@ -188,13 +187,12 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) | |||
| 188 | 187 | } | |
| 189 | 188 | ||
| 190 | 189 | int count = Runtime.PyTuple_Size(idx); | |
| 190 | + var args = new int[count]; | ||
| 191 | 191 | ||
| 192 | - Array args = Array.CreateInstance(typeof(Int32), count); | ||
| 193 | - | ||
| 194 | - for (int i = 0; i < count; i++) | ||
| 192 | + for (var i = 0; i < count; i++) | ||
| 195 | 193 | { | |
| 196 | 194 | IntPtr op = Runtime.PyTuple_GetItem(idx, i); | |
| 197 | - index = (int)Runtime.PyInt_AsLong(op); | ||
| 195 | + index = Runtime.PyInt_AsLong(op); | ||
| 198 | 196 | ||
| 199 | 197 | if (Exceptions.ErrorOccurred()) | |
| 200 | 198 | { | |
@@ -229,9 +227,9 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) | |||
| 229 | 227 | /// </summary> | |
| 230 | 228 | public static int sq_contains(IntPtr ob, IntPtr v) | |
| 231 | 229 | { | |
| 232 | - CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob); | ||
| 230 | + var obj = (CLRObject)GetManagedObject(ob); | ||
| 233 | 231 | Type itemType = obj.inst.GetType().GetElementType(); | |
| 234 | - IList items = obj.inst as IList; | ||
| 232 | + var items = obj.inst as IList; | ||
| 235 | 233 | object value; | |
| 236 | 234 | ||
| 237 | 235 | if (!Converter.ToManaged(v, itemType, out value, false)) | |
@@ -253,8 +251,8 @@ public static int sq_contains(IntPtr ob, IntPtr v) | |||
| 253 | 251 | /// </summary> | |
| 254 | 252 | public static int mp_length(IntPtr ob) | |
| 255 | 253 | { | |
| 256 | - CLRObject self = (CLRObject)ManagedType.GetManagedObject(ob); | ||
| 257 | - Array items = self.inst as Array; | ||
| 254 | + var self = (CLRObject)GetManagedObject(ob); | ||
| 255 | + var items = self.inst as Array; | ||
| 258 | 256 | return items.Length; | |
| 259 | 257 | } | |
| 260 | 258 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,8 +21,10 @@ internal class AssemblyManager | |||
| 21 | 21 | //static Dictionary<string, Dictionary<string, string>> generics; | |
| 22 | 22 | static AssemblyLoadEventHandler lhandler; | |
| 23 | 23 | static ResolveEventHandler rhandler; | |
| 24 | + | ||
| 24 | 25 | // updated only under GIL? | |
| 25 | 26 | static Dictionary<string, int> probed; | |
| 27 | + | ||
| 26 | 28 | // modified from event handlers below, potentially triggered from different .NET threads | |
| 27 | 29 | static AssemblyList assemblies; | |
| 28 | 30 | internal static List<string> pypath; | |
@@ -53,7 +55,7 @@ internal static void Initialize() | |||
| 53 | 55 | domain.AssemblyResolve += rhandler; | |
| 54 | 56 | ||
| 55 | 57 | Assembly[] items = domain.GetAssemblies(); | |
| 56 | - foreach (var a in items) | ||
| 58 | + foreach (Assembly a in items) | ||
| 57 | 59 | { | |
| 58 | 60 | try | |
| 59 | 61 | { | |
@@ -62,7 +64,7 @@ internal static void Initialize() | |||
| 62 | 64 | } | |
| 63 | 65 | catch (Exception ex) | |
| 64 | 66 | { | |
| 65 | - Debug.WriteLine(string.Format("Error scanning assembly {0}. {1}", a, ex)); | ||
| 67 | + Debug.WriteLine("Error scanning assembly {0}. {1}", a, ex); | ||
| 66 | 68 | } | |
| 67 | 69 | } | |
| 68 | 70 | } | |
@@ -86,7 +88,7 @@ internal static void Shutdown() | |||
| 86 | 88 | /// so that we can know about assemblies that get loaded after the | |
| 87 | 89 | /// Python runtime is initialized. | |
| 88 | 90 | /// </summary> | |
| 89 | - static void AssemblyLoadHandler(Object ob, AssemblyLoadEventArgs args) | ||
| 91 | + private static void AssemblyLoadHandler(object ob, AssemblyLoadEventArgs args) | ||
| 90 | 92 | { | |
| 91 | 93 | Assembly assembly = args.LoadedAssembly; | |
| 92 | 94 | assemblies.Add(assembly); | |
@@ -101,7 +103,7 @@ static void AssemblyLoadHandler(Object ob, AssemblyLoadEventArgs args) | |||
| 101 | 103 | /// for failed loads, because they might be dependencies of something | |
| 102 | 104 | /// we loaded from Python which also needs to be found on PYTHONPATH. | |
| 103 | 105 | /// </summary> | |
| 104 | - static Assembly ResolveHandler(Object ob, ResolveEventArgs args) | ||
| 106 | + private static Assembly ResolveHandler(object ob, ResolveEventArgs args) | ||
| 105 | 107 | { | |
| 106 | 108 | string name = args.Name.ToLower(); | |
| 107 | 109 | foreach (Assembly a in assemblies) | |
@@ -135,7 +137,7 @@ internal static void UpdatePath() | |||
| 135 | 137 | { | |
| 136 | 138 | pypath.Clear(); | |
| 137 | 139 | probed.Clear(); | |
| 138 | - for (int i = 0; i < count; i++) | ||
| 140 | + for (var i = 0; i < count; i++) | ||
| 139 | 141 | { | |
| 140 | 142 | IntPtr item = Runtime.PyList_GetItem(list, i); | |
| 141 | 143 | string path = Runtime.GetManagedString(item); | |
@@ -159,7 +161,7 @@ public static string FindAssembly(string name) | |||
| 159 | 161 | string path; | |
| 160 | 162 | string temp; | |
| 161 | 163 | ||
| 162 | - for (int i = 0; i < pypath.Count; i++) | ||
| 164 | + for (var i = 0; i < pypath.Count; i++) | ||
| 163 | 165 | { | |
| 164 | 166 | string head = pypath[i]; | |
| 165 | 167 | if (head == null || head.Length == 0) | |
@@ -197,7 +199,7 @@ public static Assembly LoadAssembly(string name) | |||
| 197 | 199 | { | |
| 198 | 200 | assembly = Assembly.Load(name); | |
| 199 | 201 | } | |
| 200 | - catch (System.Exception) | ||
| 202 | + catch (Exception) | ||
| 201 | 203 | { | |
| 202 | 204 | //if (!(e is System.IO.FileNotFoundException)) | |
| 203 | 205 | //{ | |
@@ -221,7 +223,7 @@ public static Assembly LoadAssemblyPath(string name) | |||
| 221 | 223 | { | |
| 222 | 224 | assembly = Assembly.LoadFrom(path); | |
| 223 | 225 | } | |
| 224 | - catch | ||
| 226 | + catch (Exception) | ||
| 225 | 227 | { | |
| 226 | 228 | } | |
| 227 | 229 | } | |
@@ -241,7 +243,9 @@ public static Assembly LoadAssemblyFullPath(string name) | |||
| 241 | 243 | if (Path.IsPathRooted(name)) | |
| 242 | 244 | { | |
| 243 | 245 | if (!Path.HasExtension(name)) | |
| 246 | + { | ||
| 244 | 247 | name = name + ".dll"; | |
| 248 | + } | ||
| 245 | 249 | if (File.Exists(name)) | |
| 246 | 250 | { | |
| 247 | 251 | try | |
@@ -287,13 +291,13 @@ public static Assembly FindLoadedAssembly(string name) | |||
| 287 | 291 | public static bool LoadImplicit(string name, bool warn = true) | |
| 288 | 292 | { | |
| 289 | 293 | string[] names = name.Split('.'); | |
| 290 | - bool loaded = false; | ||
| 291 | - string s = ""; | ||
| 294 | + var loaded = false; | ||
| 295 | + var s = ""; | ||
| 292 | 296 | Assembly lastAssembly = null; | |
| 293 | 297 | HashSet<Assembly> assembliesSet = null; | |
| 294 | - for (int i = 0; i < names.Length; i++) | ||
| 298 | + for (var i = 0; i < names.Length; i++) | ||
| 295 | 299 | { | |
| 296 | - s = (i == 0) ? names[0] : s + "." + names[i]; | ||
| 300 | + s = i == 0 ? names[0] : s + "." + names[i]; | ||
| 297 | 301 | if (!probed.ContainsKey(s)) | |
| 298 | 302 | { | |
| 299 | 303 | if (assembliesSet == null) | |
@@ -321,7 +325,7 @@ public static bool LoadImplicit(string name, bool warn = true) | |||
| 321 | 325 | // Deprecation warning | |
| 322 | 326 | if (warn && loaded) | |
| 323 | 327 | { | |
| 324 | - string deprWarning = String.Format( | ||
| 328 | + string deprWarning = string.Format( | ||
| 325 | 329 | "\nThe module was found, but not in a referenced namespace.\n" + | |
| 326 | 330 | "Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").", | |
| 327 | 331 | Path.GetFileNameWithoutExtension(lastAssembly.Location)); | |
@@ -345,24 +349,23 @@ internal static void ScanAssembly(Assembly assembly) | |||
| 345 | 349 | // the assembly. | |
| 346 | 350 | ||
| 347 | 351 | Type[] types = assembly.GetTypes(); | |
| 348 | - for (int i = 0; i < types.Length; i++) | ||
| 352 | + foreach (Type t in types) | ||
| 349 | 353 | { | |
| 350 | - Type t = types[i]; | ||
| 351 | 354 | string ns = t.Namespace ?? ""; | |
| 352 | 355 | if (!namespaces.ContainsKey(ns)) | |
| 353 | 356 | { | |
| 354 | 357 | string[] names = ns.Split('.'); | |
| 355 | - string s = ""; | ||
| 356 | - for (int n = 0; n < names.Length; n++) | ||
| 358 | + var s = ""; | ||
| 359 | + for (var n = 0; n < names.Length; n++) | ||
| 357 | 360 | { | |
| 358 | - s = (n == 0) ? names[0] : s + "." + names[n]; | ||
| 361 | + s = n == 0 ? names[0] : s + "." + names[n]; | ||
| 359 | 362 | namespaces.TryAdd(s, new ConcurrentDictionary<Assembly, string>()); | |
| 360 | 363 | } | |
| 361 | 364 | } | |
| 362 | 365 | ||
| 363 | 366 | if (ns != null) | |
| 364 | 367 | { | |
| 365 | - namespaces[ns].TryAdd(assembly, String.Empty); | ||
| 368 | + namespaces[ns].TryAdd(assembly, string.Empty); | ||
| 366 | 369 | } | |
| 367 | 370 | ||
| 368 | 371 | if (ns != null && t.IsGenericTypeDefinition) | |
@@ -374,7 +377,7 @@ internal static void ScanAssembly(Assembly assembly) | |||
| 374 | 377 | ||
| 375 | 378 | public static AssemblyName[] ListAssemblies() | |
| 376 | 379 | { | |
| 377 | - List<AssemblyName> names = new List<AssemblyName>(assemblies.Count); | ||
| 380 | + var names = new List<AssemblyName>(assemblies.Count); | ||
| 378 | 381 | foreach (Assembly assembly in assemblies) | |
| 379 | 382 | { | |
| 380 | 383 | names.Add(assembly.GetName()); | |
@@ -388,18 +391,15 @@ public static AssemblyName[] ListAssemblies() | |||
| 388 | 391 | /// </summary> | |
| 389 | 392 | public static bool IsValidNamespace(string name) | |
| 390 | 393 | { | |
| 391 | - return !String.IsNullOrEmpty(name) && namespaces.ContainsKey(name); | ||
| 394 | + return !string.IsNullOrEmpty(name) && namespaces.ContainsKey(name); | ||
| 392 | 395 | } | |
| 393 | 396 | ||
| 394 | 397 | /// <summary> | |
| 395 | 398 | /// Returns list of assemblies that declare types in a given namespace | |
| 396 | 399 | /// </summary> | |
| 397 | 400 | public static IEnumerable<Assembly> GetAssemblies(string nsname) | |
| 398 | 401 | { | |
| 399 | - if (!namespaces.ContainsKey(nsname)) | ||
| 400 | - return new List<Assembly>(); | ||
| 401 | - | ||
| 402 | - return namespaces[nsname].Keys; | ||
| 402 | + return !namespaces.ContainsKey(nsname) ? new List<Assembly>() : namespaces[nsname].Keys; | ||
| 403 | 403 | } | |
| 404 | 404 | ||
| 405 | 405 | /// <summary> | |
@@ -408,7 +408,7 @@ public static IEnumerable<Assembly> GetAssemblies(string nsname) | |||
| 408 | 408 | public static List<string> GetNames(string nsname) | |
| 409 | 409 | { | |
| 410 | 410 | //Dictionary<string, int> seen = new Dictionary<string, int>(); | |
| 411 | - List<string> names = new List<string>(8); | ||
| 411 | + var names = new List<string>(8); | ||
| 412 | 412 | ||
| 413 | 413 | List<string> g = GenericUtil.GetGenericBaseNames(nsname); | |
| 414 | 414 | if (g != null) | |
@@ -424,9 +424,8 @@ public static List<string> GetNames(string nsname) | |||
| 424 | 424 | foreach (Assembly a in namespaces[nsname].Keys) | |
| 425 | 425 | { | |
| 426 | 426 | Type[] types = a.GetTypes(); | |
| 427 | - for (int i = 0; i < types.Length; i++) | ||
| 427 | + foreach (Type t in types) | ||
| 428 | 428 | { | |
| 429 | - Type t = types[i]; | ||
| 430 | 429 | if ((t.Namespace ?? "") == nsname) | |
| 431 | 430 | { | |
| 432 | 431 | names.Add(t.Name); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments