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
{
@@ -44,11 +43,11 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
44
43
45
44
public static IntPtr mp_subscript ( IntPtr ob , IntPtr idx )
46
45
{
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 ;
49
48
Type itemType = obj . inst . GetType ( ) . GetElementType ( ) ;
50
49
int rank = items . Rank ;
51
- int index = 0 ;
50
+ int index ;
52
51
object value ;
53
52
54
53
// Note that CLR 1.0 only supports int indexes - methods to
@@ -62,7 +61,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
62
61
63
62
if ( rank == 1 )
64
63
{
65
- index = ( int ) Runtime . PyInt_AsLong ( idx ) ;
64
+ index = Runtime . PyInt_AsLong ( idx ) ;
66
65
67
66
if ( Exceptions . ErrorOccurred ( ) )
68
67
{
@@ -80,33 +79,29 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
80
79
}
81
80
catch ( IndexOutOfRangeException )
82
81
{
83
- Exceptions . SetError ( Exceptions . IndexError ,
84
- "array index out of range"
85
- ) ;
82
+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
86
83
return IntPtr . Zero ;
87
84
}
88
85
89
- return Converter . ToPython ( items . GetValue ( index ) , itemType ) ;
86
+ return Converter . ToPython ( value , itemType ) ;
90
87
}
91
88
92
89
// Multi-dimensional arrays can be indexed a la: list[1, 2, 3].
93
90
94
91
if ( ! Runtime . PyTuple_Check ( idx ) )
95
92
{
96
- Exceptions . SetError ( Exceptions . TypeError ,
97
- "invalid index value"
98
- ) ;
93
+ Exceptions . SetError ( Exceptions . TypeError , "invalid index value" ) ;
99
94
return IntPtr . Zero ;
100
95
}
101
96
102
97
int count = Runtime . PyTuple_Size ( idx ) ;
103
98
104
- Array args = Array . CreateInstance ( typeof ( Int32 ) , count ) ;
99
+ Array args = new int [ count ] ;
105
100
106
- for ( int i = 0 ; i < count ; i ++ )
101
+ for ( var i = 0 ; i < count ; i ++ )
107
102
{
108
103
IntPtr op = Runtime . PyTuple_GetItem ( idx , i ) ;
109
- index = ( int ) Runtime . PyInt_AsLong ( op ) ;
104
+ index = Runtime . PyInt_AsLong ( op ) ;
110
105
111
106
if ( Exceptions . ErrorOccurred ( ) )
112
107
{
@@ -127,9 +122,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
127
122
}
128
123
catch ( IndexOutOfRangeException )
129
124
{
130
- Exceptions . SetError ( Exceptions . IndexError ,
131
- "array index out of range"
132
- ) ;
125
+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
133
126
return IntPtr . Zero ;
134
127
}
135
128
@@ -143,11 +136,11 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
143
136
144
137
public static int mp_ass_subscript ( IntPtr ob , IntPtr idx , IntPtr v )
145
138
{
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 ;
148
141
Type itemType = obj . inst . GetType ( ) . GetElementType ( ) ;
149
142
int rank = items . Rank ;
150
- int index = 0 ;
143
+ int index ;
151
144
object value ;
152
145
153
146
if ( items . IsReadOnly )
@@ -163,7 +156,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
163
156
164
157
if ( rank == 1 )
165
158
{
166
- index = ( int ) Runtime . PyInt_AsLong ( idx ) ;
159
+ index = Runtime . PyInt_AsLong ( idx ) ;
167
160
168
161
if ( Exceptions . ErrorOccurred ( ) )
169
162
{
@@ -182,9 +175,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
182
175
}
183
176
catch ( IndexOutOfRangeException )
184
177
{
185
- Exceptions . SetError ( Exceptions . IndexError ,
186
- "array index out of range"
187
- ) ;
178
+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
188
179
return - 1 ;
189
180
}
190
181
@@ -199,12 +190,12 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
199
190
200
191
int count = Runtime . PyTuple_Size ( idx ) ;
201
192
202
- Array args = Array . CreateInstance ( typeof ( Int32 ) , count ) ;
193
+ Array args = new int [ count ] ;
203
194
204
- for ( int i = 0 ; i < count ; i ++ )
195
+ for ( var i = 0 ; i < count ; i ++ )
205
196
{
206
197
IntPtr op = Runtime . PyTuple_GetItem ( idx , i ) ;
207
- index = ( int ) Runtime . PyInt_AsLong ( op ) ;
198
+ index = Runtime . PyInt_AsLong ( op ) ;
208
199
209
200
if ( Exceptions . ErrorOccurred ( ) )
210
201
{
@@ -226,9 +217,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
226
217
}
227
218
catch ( IndexOutOfRangeException )
228
219
{
229
- Exceptions . SetError ( Exceptions . IndexError ,
230
- "array index out of range"
231
- ) ;
220
+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
232
221
return - 1 ;
233
222
}
234
223
@@ -242,9 +231,9 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
242
231
243
232
public static int sq_contains ( IntPtr ob , IntPtr v )
244
233
{
245
- CLRObject obj = ( CLRObject ) ManagedType . GetManagedObject ( ob ) ;
234
+ var obj = ( CLRObject ) GetManagedObject ( ob ) ;
246
235
Type itemType = obj . inst . GetType ( ) . GetElementType ( ) ;
247
- IList items = obj . inst as IList ;
236
+ var items = obj . inst as IList ;
248
237
object value ;
249
238
250
239
if ( ! Converter . ToManaged ( v , itemType , out value , false ) )
@@ -267,9 +256,9 @@ public static int sq_contains(IntPtr ob, IntPtr v)
267
256
268
257
public static int mp_length ( IntPtr ob )
269
258
{
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 ;
272
261
return items . Length ;
273
262
}
274
263
}
275
- }
264
+ }
0 commit comments