Skip to content

Commit 4f62e9a

Browse files
committed
Clean-up simple classes
1 parent 2d1da5d commit 4f62e9a

34 files changed

+351
-434
lines changed

src/runtime/arrayobject.cs

Lines changed: 27 additions & 38 deletions
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
{
@@ -44,11 +43,11 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
4443

4544
public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
4645
{
47-
CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob);
48-
Array items = obj.inst as Array;
46+
var obj = (CLRObject)GetManagedObject(ob);
47+
var items = obj.inst as Array;
4948
Type itemType = obj.inst.GetType().GetElementType();
5049
int rank = items.Rank;
51-
int index = 0;
50+
int index;
5251
object value;
5352

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

6362
if (rank == 1)
6463
{
65-
index = (int)Runtime.PyInt_AsLong(idx);
64+
index = Runtime.PyInt_AsLong(idx);
6665

6766
if (Exceptions.ErrorOccurred())
6867
{
@@ -80,33 +79,29 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
8079
}
8180
catch (IndexOutOfRangeException)
8281
{
83-
Exceptions.SetError(Exceptions.IndexError,
84-
"array index out of range"
85-
);
82+
Exceptions.SetError(Exceptions.IndexError, "array index out of range");
8683
return IntPtr.Zero;
8784
}
8885

89-
return Converter.ToPython(items.GetValue(index), itemType);
86+
return Converter.ToPython(value, itemType);
9087
}
9188

9289
// Multi-dimensional arrays can be indexed a la: list[1, 2, 3].
9390

9491
if (!Runtime.PyTuple_Check(idx))
9592
{
96-
Exceptions.SetError(Exceptions.TypeError,
97-
"invalid index value"
98-
);
93+
Exceptions.SetError(Exceptions.TypeError, "invalid index value");
9994
return IntPtr.Zero;
10095
}
10196

10297
int count = Runtime.PyTuple_Size(idx);
10398

104-
Array args = Array.CreateInstance(typeof(Int32), count);
99+
Array args = new int[count];
105100

106-
for (int i = 0; i < count; i++)
101+
for (var i = 0; i < count; i++)
107102
{
108103
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
109-
index = (int)Runtime.PyInt_AsLong(op);
104+
index = Runtime.PyInt_AsLong(op);
110105

111106
if (Exceptions.ErrorOccurred())
112107
{
@@ -127,9 +122,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
127122
}
128123
catch (IndexOutOfRangeException)
129124
{
130-
Exceptions.SetError(Exceptions.IndexError,
131-
"array index out of range"
132-
);
125+
Exceptions.SetError(Exceptions.IndexError, "array index out of range");
133126
return IntPtr.Zero;
134127
}
135128

@@ -143,11 +136,11 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
143136

144137
public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
145138
{
146-
CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob);
147-
Array items = obj.inst as Array;
139+
var obj = (CLRObject)GetManagedObject(ob);
140+
var items = obj.inst as Array;
148141
Type itemType = obj.inst.GetType().GetElementType();
149142
int rank = items.Rank;
150-
int index = 0;
143+
int index;
151144
object value;
152145

153146
if (items.IsReadOnly)
@@ -163,7 +156,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
163156

164157
if (rank == 1)
165158
{
166-
index = (int)Runtime.PyInt_AsLong(idx);
159+
index = Runtime.PyInt_AsLong(idx);
167160

168161
if (Exceptions.ErrorOccurred())
169162
{
@@ -182,9 +175,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
182175
}
183176
catch (IndexOutOfRangeException)
184177
{
185-
Exceptions.SetError(Exceptions.IndexError,
186-
"array index out of range"
187-
);
178+
Exceptions.SetError(Exceptions.IndexError, "array index out of range");
188179
return -1;
189180
}
190181

@@ -199,12 +190,12 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
199190

200191
int count = Runtime.PyTuple_Size(idx);
201192

202-
Array args = Array.CreateInstance(typeof(Int32), count);
193+
Array args = new int[count];
203194

204-
for (int i = 0; i < count; i++)
195+
for (var i = 0; i < count; i++)
205196
{
206197
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
207-
index = (int)Runtime.PyInt_AsLong(op);
198+
index = Runtime.PyInt_AsLong(op);
208199

209200
if (Exceptions.ErrorOccurred())
210201
{
@@ -226,9 +217,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
226217
}
227218
catch (IndexOutOfRangeException)
228219
{
229-
Exceptions.SetError(Exceptions.IndexError,
230-
"array index out of range"
231-
);
220+
Exceptions.SetError(Exceptions.IndexError, "array index out of range");
232221
return -1;
233222
}
234223

@@ -242,9 +231,9 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
242231

243232
public static int sq_contains(IntPtr ob, IntPtr v)
244233
{
245-
CLRObject obj = (CLRObject)ManagedType.GetManagedObject(ob);
234+
var obj = (CLRObject)GetManagedObject(ob);
246235
Type itemType = obj.inst.GetType().GetElementType();
247-
IList items = obj.inst as IList;
236+
var items = obj.inst as IList;
248237
object value;
249238

250239
if (!Converter.ToManaged(v, itemType, out value, false))
@@ -267,9 +256,9 @@ public static int sq_contains(IntPtr ob, IntPtr v)
267256

268257
public static int mp_length(IntPtr ob)
269258
{
270-
CLRObject self = (CLRObject)ManagedType.GetManagedObject(ob);
271-
Array items = self.inst as Array;
259+
var self = (CLRObject)GetManagedObject(ob);
260+
var items = self.inst as Array;
272261
return items.Length;
273262
}
274263
}
275-
}
264+
}

0 commit comments

Comments
 (0)