Skip to content

Commit da9c030

Browse files
committed
Clean-up simple classes
1 parent 432467f commit da9c030

32 files changed

+237
-267
lines changed

src/runtime/arrayobject.cs

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections;
3-
using System.Reflection;
43

54
namespace Python.Runtime
65
{
@@ -22,13 +21,13 @@ internal override bool CanSubclass()
2221

2322
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
2423
{
25-
ArrayObject self = GetManagedObject(tp) as ArrayObject;
24+
var self = GetManagedObject(tp) as ArrayObject;
2625
if (Runtime.PyTuple_Size(args) != 1)
2726
{
2827
return Exceptions.RaiseTypeError("array expects 1 argument");
2928
}
3029
IntPtr op = Runtime.PyTuple_GetItem(args, 0);
31-
Object result;
30+
object result;
3231

3332
if (!Converter.ToManaged(op, self.type, out result, true))
3433
{
@@ -43,11 +42,11 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
4342
/// </summary>
4443
public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
4544
{
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;
4847
Type itemType = obj.inst.GetType().GetElementType();
4948
int rank = items.Rank;
50-
int index = 0;
49+
int index;
5150
object value;
5251

5352
// Note that CLR 1.0 only supports int indexes - methods to
@@ -61,7 +60,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
6160

6261
if (rank == 1)
6362
{
64-
index = (int)Runtime.PyInt_AsLong(idx);
63+
index = Runtime.PyInt_AsLong(idx);
6564

6665
if (Exceptions.ErrorOccurred())
6766
{
@@ -83,7 +82,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
8382
return IntPtr.Zero;
8483
}
8584

86-
return Converter.ToPython(items.GetValue(index), itemType);
85+
return Converter.ToPython(value, itemType);
8786
}
8887

8988
// 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)
9695

9796
int count = Runtime.PyTuple_Size(idx);
9897

99-
Array args = Array.CreateInstance(typeof(Int32), count);
98+
var args = new int[count];
10099

101-
for (int i = 0; i < count; i++)
100+
for (var i = 0; i < count; i++)
102101
{
103102
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
104-
index = (int)Runtime.PyInt_AsLong(op);
103+
index = Runtime.PyInt_AsLong(op);
105104

106105
if (Exceptions.ErrorOccurred())
107106
{
@@ -135,11 +134,11 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
135134
/// </summary>
136135
public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
137136
{
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;
140139
Type itemType = obj.inst.GetType().GetElementType();
141140
int rank = items.Rank;
142-
int index = 0;
141+
int index;
143142
object value;
144143

145144
if (items.IsReadOnly)
@@ -155,7 +154,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
155154

156155
if (rank == 1)
157156
{
158-
index = (int)Runtime.PyInt_AsLong(idx);
157+
index = Runtime.PyInt_AsLong(idx);
159158

160159
if (Exceptions.ErrorOccurred())
161160
{
@@ -188,13 +187,12 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
188187
}
189188

190189
int count = Runtime.PyTuple_Size(idx);
190+
var args = new int[count];
191191

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++)
195193
{
196194
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
197-
index = (int)Runtime.PyInt_AsLong(op);
195+
index = Runtime.PyInt_AsLong(op);
198196

199197
if (Exceptions.ErrorOccurred())
200198
{
@@ -229,9 +227,9 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
229227
/// </summary>
230228
public static int sq_contains(IntPtr ob, IntPtr v)
231229
{
232-
CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob);
230+
var obj = (CLRObject)GetManagedObject(ob);
233231
Type itemType = obj.inst.GetType().GetElementType();
234-
IList items = obj.inst as IList;
232+
var items = obj.inst as IList;
235233
object value;
236234

237235
if (!Converter.ToManaged(v, itemType, out value, false))
@@ -253,8 +251,8 @@ public static int sq_contains(IntPtr ob, IntPtr v)
253251
/// </summary>
254252
public static int mp_length(IntPtr ob)
255253
{
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;
258256
return items.Length;
259257
}
260258
}

src/runtime/assemblymanager.cs

+24-25
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ internal class AssemblyManager
2121
//static Dictionary<string, Dictionary<string, string>> generics;
2222
static AssemblyLoadEventHandler lhandler;
2323
static ResolveEventHandler rhandler;
24+
2425
// updated only under GIL?
2526
static Dictionary<string, int> probed;
27+
2628
// modified from event handlers below, potentially triggered from different .NET threads
2729
static AssemblyList assemblies;
2830
internal static List<string> pypath;
@@ -53,7 +55,7 @@ internal static void Initialize()
5355
domain.AssemblyResolve += rhandler;
5456

5557
Assembly[] items = domain.GetAssemblies();
56-
foreach (var a in items)
58+
foreach (Assembly a in items)
5759
{
5860
try
5961
{
@@ -62,7 +64,7 @@ internal static void Initialize()
6264
}
6365
catch (Exception ex)
6466
{
65-
Debug.WriteLine(string.Format("Error scanning assembly {0}. {1}", a, ex));
67+
Debug.WriteLine("Error scanning assembly {0}. {1}", a, ex);
6668
}
6769
}
6870
}
@@ -86,7 +88,7 @@ internal static void Shutdown()
8688
/// so that we can know about assemblies that get loaded after the
8789
/// Python runtime is initialized.
8890
/// </summary>
89-
static void AssemblyLoadHandler(Object ob, AssemblyLoadEventArgs args)
91+
private static void AssemblyLoadHandler(object ob, AssemblyLoadEventArgs args)
9092
{
9193
Assembly assembly = args.LoadedAssembly;
9294
assemblies.Add(assembly);
@@ -101,7 +103,7 @@ static void AssemblyLoadHandler(Object ob, AssemblyLoadEventArgs args)
101103
/// for failed loads, because they might be dependencies of something
102104
/// we loaded from Python which also needs to be found on PYTHONPATH.
103105
/// </summary>
104-
static Assembly ResolveHandler(Object ob, ResolveEventArgs args)
106+
private static Assembly ResolveHandler(object ob, ResolveEventArgs args)
105107
{
106108
string name = args.Name.ToLower();
107109
foreach (Assembly a in assemblies)
@@ -197,7 +199,7 @@ public static Assembly LoadAssembly(string name)
197199
{
198200
assembly = Assembly.Load(name);
199201
}
200-
catch (System.Exception)
202+
catch (Exception)
201203
{
202204
//if (!(e is System.IO.FileNotFoundException))
203205
//{
@@ -221,7 +223,7 @@ public static Assembly LoadAssemblyPath(string name)
221223
{
222224
assembly = Assembly.LoadFrom(path);
223225
}
224-
catch
226+
catch (Exception)
225227
{
226228
}
227229
}
@@ -241,7 +243,9 @@ public static Assembly LoadAssemblyFullPath(string name)
241243
if (Path.IsPathRooted(name))
242244
{
243245
if (!Path.HasExtension(name))
246+
{
244247
name = name + ".dll";
248+
}
245249
if (File.Exists(name))
246250
{
247251
try
@@ -287,13 +291,13 @@ public static Assembly FindLoadedAssembly(string name)
287291
public static bool LoadImplicit(string name, bool warn = true)
288292
{
289293
string[] names = name.Split('.');
290-
bool loaded = false;
291-
string s = "";
294+
var loaded = false;
295+
var s = "";
292296
Assembly lastAssembly = null;
293297
HashSet<Assembly> assembliesSet = null;
294-
for (int i = 0; i < names.Length; i++)
298+
for (var i = 0; i < names.Length; i++)
295299
{
296-
s = (i == 0) ? names[0] : s + "." + names[i];
300+
s = i == 0 ? names[0] : s + "." + names[i];
297301
if (!probed.ContainsKey(s))
298302
{
299303
if (assembliesSet == null)
@@ -321,7 +325,7 @@ public static bool LoadImplicit(string name, bool warn = true)
321325
// Deprecation warning
322326
if (warn && loaded)
323327
{
324-
string deprWarning = String.Format(
328+
string deprWarning = string.Format(
325329
"\nThe module was found, but not in a referenced namespace.\n" +
326330
"Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").",
327331
Path.GetFileNameWithoutExtension(lastAssembly.Location));
@@ -345,17 +349,16 @@ internal static void ScanAssembly(Assembly assembly)
345349
// the assembly.
346350

347351
Type[] types = assembly.GetTypes();
348-
for (int i = 0; i < types.Length; i++)
352+
foreach (Type t in types)
349353
{
350-
Type t = types[i];
351354
string ns = t.Namespace ?? "";
352355
if (!namespaces.ContainsKey(ns))
353356
{
354357
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++)
357360
{
358-
s = (n == 0) ? names[0] : s + "." + names[n];
361+
s = n == 0 ? names[0] : s + "." + names[n];
359362
namespaces.TryAdd(s, new ConcurrentDictionary<Assembly, string>());
360363
}
361364
}
@@ -374,7 +377,7 @@ internal static void ScanAssembly(Assembly assembly)
374377

375378
public static AssemblyName[] ListAssemblies()
376379
{
377-
List<AssemblyName> names = new List<AssemblyName>(assemblies.Count);
380+
var names = new List<AssemblyName>(assemblies.Count);
378381
foreach (Assembly assembly in assemblies)
379382
{
380383
names.Add(assembly.GetName());
@@ -388,18 +391,15 @@ public static AssemblyName[] ListAssemblies()
388391
/// </summary>
389392
public static bool IsValidNamespace(string name)
390393
{
391-
return !String.IsNullOrEmpty(name) && namespaces.ContainsKey(name);
394+
return !string.IsNullOrEmpty(name) && namespaces.ContainsKey(name);
392395
}
393396

394397
/// <summary>
395398
/// Returns list of assemblies that declare types in a given namespace
396399
/// </summary>
397400
public static IEnumerable<Assembly> GetAssemblies(string nsname)
398401
{
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;
403403
}
404404

405405
/// <summary>
@@ -408,7 +408,7 @@ public static IEnumerable<Assembly> GetAssemblies(string nsname)
408408
public static List<string> GetNames(string nsname)
409409
{
410410
//Dictionary<string, int> seen = new Dictionary<string, int>();
411-
List<string> names = new List<string>(8);
411+
var names = new List<string>(8);
412412

413413
List<string> g = GenericUtil.GetGenericBaseNames(nsname);
414414
if (g != null)
@@ -424,9 +424,8 @@ public static List<string> GetNames(string nsname)
424424
foreach (Assembly a in namespaces[nsname].Keys)
425425
{
426426
Type[] types = a.GetTypes();
427-
for (int i = 0; i < types.Length; i++)
427+
foreach (Type t in types)
428428
{
429-
Type t = types[i];
430429
if ((t.Namespace ?? "") == nsname)
431430
{
432431
names.Add(t.Name);

src/runtime/clrobject.cs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
using System;
2-
using System.Collections;
3-
using System.Reflection;
42
using System.Runtime.InteropServices;
53

64
namespace Python.Runtime
75
{
86
internal class CLRObject : ManagedType
97
{
10-
internal Object inst;
8+
internal object inst;
119

12-
internal CLRObject(Object ob, IntPtr tp) : base()
10+
internal CLRObject(object ob, IntPtr tp)
1311
{
1412
IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);
1513

16-
int flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);
14+
var flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);
1715
if ((flags & TypeFlags.Subclass) != 0)
1816
{
1917
IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.DictOffset(tp));
@@ -26,9 +24,9 @@ internal CLRObject(Object ob, IntPtr tp) : base()
2624

2725
GCHandle gc = GCHandle.Alloc(this);
2826
Marshal.WriteIntPtr(py, ObjectOffset.magic(tp), (IntPtr)gc);
29-
this.tpHandle = tp;
30-
this.pyHandle = py;
31-
this.gcHandle = gc;
27+
tpHandle = tp;
28+
pyHandle = py;
29+
gcHandle = gc;
3230
inst = ob;
3331

3432
// Fix the BaseException args (and __cause__ in case of Python 3)
@@ -37,35 +35,35 @@ internal CLRObject(Object ob, IntPtr tp) : base()
3735
}
3836

3937

40-
internal static CLRObject GetInstance(Object ob, IntPtr pyType)
38+
internal static CLRObject GetInstance(object ob, IntPtr pyType)
4139
{
4240
return new CLRObject(ob, pyType);
4341
}
4442

4543

46-
internal static CLRObject GetInstance(Object ob)
44+
internal static CLRObject GetInstance(object ob)
4745
{
4846
ClassBase cc = ClassManager.GetClass(ob.GetType());
4947
return GetInstance(ob, cc.tpHandle);
5048
}
5149

5250

53-
internal static IntPtr GetInstHandle(Object ob, IntPtr pyType)
51+
internal static IntPtr GetInstHandle(object ob, IntPtr pyType)
5452
{
5553
CLRObject co = GetInstance(ob, pyType);
5654
return co.pyHandle;
5755
}
5856

5957

60-
internal static IntPtr GetInstHandle(Object ob, Type type)
58+
internal static IntPtr GetInstHandle(object ob, Type type)
6159
{
6260
ClassBase cc = ClassManager.GetClass(type);
6361
CLRObject co = GetInstance(ob, cc.tpHandle);
6462
return co.pyHandle;
6563
}
6664

6765

68-
internal static IntPtr GetInstHandle(Object ob)
66+
internal static IntPtr GetInstHandle(object ob)
6967
{
7068
CLRObject co = GetInstance(ob);
7169
return co.pyHandle;

0 commit comments

Comments
 (0)