11using System ;
22using System . Collections ;
3- using System . Reflection ;
43
54namespace Python . Runtime
65{
@@ -22,13 +21,13 @@ internal override bool CanSubclass()
2221
2322public 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 ;
2625if ( Runtime . PyTuple_Size ( args ) != 1 )
2726{
2827return Exceptions . RaiseTypeError ( "array expects 1 argument" ) ;
2928}
3029IntPtr op = Runtime . PyTuple_GetItem ( args , 0 ) ;
31- Object result ;
30+ object result ;
3231
3332if ( ! 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
4544public 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 ;
4948Type itemType = obj . inst . GetType ( ) . GetElementType ( ) ;
5049int rank = items . Rank ;
51- int index = 0 ;
50+ int index ;
5251object 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
6362if ( rank == 1 )
6463{
65- index = ( int ) Runtime . PyInt_AsLong ( idx ) ;
64+ index = Runtime . PyInt_AsLong ( idx ) ;
6665
6766if ( Exceptions . ErrorOccurred ( ) )
6867{
@@ -80,33 +79,29 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
8079}
8180catch ( IndexOutOfRangeException )
8281{
83- Exceptions . SetError ( Exceptions . IndexError ,
84- "array index out of range"
85- ) ;
82+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
8683return 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
9491if ( ! Runtime . PyTuple_Check ( idx ) )
9592{
96- Exceptions . SetError ( Exceptions . TypeError ,
97- "invalid index value"
98- ) ;
93+ Exceptions . SetError ( Exceptions . TypeError , "invalid index value" ) ;
9994return IntPtr . Zero ;
10095}
10196
10297int 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{
108103IntPtr op = Runtime . PyTuple_GetItem ( idx , i ) ;
109- index = ( int ) Runtime . PyInt_AsLong ( op ) ;
104+ index = Runtime . PyInt_AsLong ( op ) ;
110105
111106if ( Exceptions . ErrorOccurred ( ) )
112107{
@@ -127,9 +122,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
127122}
128123catch ( IndexOutOfRangeException )
129124{
130- Exceptions . SetError ( Exceptions . IndexError ,
131- "array index out of range"
132- ) ;
125+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
133126return IntPtr . Zero ;
134127}
135128
@@ -143,11 +136,11 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
143136
144137public 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 ;
148141Type itemType = obj . inst . GetType ( ) . GetElementType ( ) ;
149142int rank = items . Rank ;
150- int index = 0 ;
143+ int index ;
151144object value ;
152145
153146if ( items . IsReadOnly )
@@ -163,7 +156,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
163156
164157if ( rank == 1 )
165158{
166- index = ( int ) Runtime . PyInt_AsLong ( idx ) ;
159+ index = Runtime . PyInt_AsLong ( idx ) ;
167160
168161if ( Exceptions . ErrorOccurred ( ) )
169162{
@@ -182,9 +175,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
182175}
183176catch ( IndexOutOfRangeException )
184177{
185- Exceptions . SetError ( Exceptions . IndexError ,
186- "array index out of range"
187- ) ;
178+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
188179return - 1 ;
189180}
190181
@@ -199,12 +190,12 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
199190
200191int 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{
206197IntPtr op = Runtime . PyTuple_GetItem ( idx , i ) ;
207- index = ( int ) Runtime . PyInt_AsLong ( op ) ;
198+ index = Runtime . PyInt_AsLong ( op ) ;
208199
209200if ( Exceptions . ErrorOccurred ( ) )
210201{
@@ -226,9 +217,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
226217}
227218catch ( IndexOutOfRangeException )
228219{
229- Exceptions . SetError ( Exceptions . IndexError ,
230- "array index out of range"
231- ) ;
220+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
232221return - 1 ;
233222}
234223
@@ -242,9 +231,9 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
242231
243232public static int sq_contains ( IntPtr ob , IntPtr v )
244233{
245- CLRObject obj = ( CLRObject ) ManagedType . GetManagedObject ( ob ) ;
234+ var obj = ( CLRObject ) GetManagedObject ( ob ) ;
246235Type itemType = obj . inst . GetType ( ) . GetElementType ( ) ;
247- IList items = obj . inst as IList ;
236+ var items = obj . inst as IList ;
248237object value ;
249238
250239if ( ! Converter . ToManaged ( v , itemType , out value , false ) )
@@ -267,9 +256,9 @@ public static int sq_contains(IntPtr ob, IntPtr v)
267256
268257public 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 ;
272261return items . Length ;
273262}
274263}
275- }
264+ }