Skip to content

Commit d8cf55a

Browse files
committed
Clean-up code-style
Remove redundant parenthesis, enforce brackets, use keywork types.
1 parent 61536f3 commit d8cf55a

File tree

6 files changed

+91
-56
lines changed

6 files changed

+91
-56
lines changed

src/runtime/classobject.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ internal ClassObject(Type tp) : base(tp)
3232
internal IntPtr GetDocString()
3333
{
3434
MethodBase[] methods = binder.GetMethods();
35-
string str = "";
35+
var str = "";
3636
foreach (MethodBase t in methods)
3737
{
3838
if (str.Length > 0)
39+
{
3940
str += Environment.NewLine;
41+
}
4042
str += t.ToString();
4143
}
4244
return Runtime.PyString_FromString(str);
@@ -48,7 +50,7 @@ internal IntPtr GetDocString()
4850
/// </summary>
4951
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
5052
{
51-
ClassObject self = GetManagedObject(tp) as ClassObject;
53+
var self = GetManagedObject(tp) as ClassObject;
5254

5355
// Sanity check: this ensures a graceful error if someone does
5456
// something intentially wrong like use the managed metatype for
@@ -72,7 +74,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
7274
}
7375

7476
IntPtr op = Runtime.PyTuple_GetItem(args, 0);
75-
Object result;
77+
object result;
7678

7779
if (!Converter.ToManaged(op, type, out result, true))
7880
{
@@ -94,7 +96,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
9496
return IntPtr.Zero;
9597
}
9698

97-
Object obj = self.binder.InvokeRaw(IntPtr.Zero, args, kw);
99+
object obj = self.binder.InvokeRaw(IntPtr.Zero, args, kw);
98100
if (obj == null)
99101
{
100102
return IntPtr.Zero;
@@ -119,8 +121,8 @@ public override IntPtr type_subscript(IntPtr idx)
119121
{
120122
return Exceptions.RaiseTypeError("type expected");
121123
}
122-
ClassBase c = GetManagedObject(idx) as ClassBase;
123-
Type t = (c != null) ? c.type : Converter.GetTypeByAlias(idx);
124+
var c = GetManagedObject(idx) as ClassBase;
125+
Type t = c != null ? c.type : Converter.GetTypeByAlias(idx);
124126
if (t == null)
125127
{
126128
return Exceptions.RaiseTypeError("type expected");
@@ -159,7 +161,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
159161
{
160162
//ManagedType self = GetManagedObject(ob);
161163
IntPtr tp = Runtime.PyObject_TYPE(ob);
162-
ClassBase cls = (ClassBase)GetManagedObject(tp);
164+
var cls = (ClassBase)GetManagedObject(tp);
163165

164166
if (cls.indexer == null || !cls.indexer.CanGet)
165167
{
@@ -171,7 +173,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
171173
// parameters. If so, use it directly, else make a new tuple
172174
// with the index arg (method binders expect arg tuples).
173175
IntPtr args = idx;
174-
bool free = false;
176+
var free = false;
175177

176178
if (!Runtime.PyTuple_Check(idx))
177179
{
@@ -205,7 +207,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
205207
{
206208
//ManagedType self = GetManagedObject(ob);
207209
IntPtr tp = Runtime.PyObject_TYPE(ob);
208-
ClassBase cls = (ClassBase)GetManagedObject(tp);
210+
var cls = (ClassBase)GetManagedObject(tp);
209211

210212
if (cls.indexer == null || !cls.indexer.CanSet)
211213
{
@@ -217,7 +219,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
217219
// parameters. If so, use it directly, else make a new tuple
218220
// with the index arg (method binders expect arg tuples).
219221
IntPtr args = idx;
220-
bool free = false;
222+
var free = false;
221223

222224
if (!Runtime.PyTuple_Check(idx))
223225
{
@@ -233,15 +235,15 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
233235
int numOfDefaultArgs = Runtime.PyTuple_Size(defaultArgs);
234236
int temp = i + numOfDefaultArgs;
235237
IntPtr real = Runtime.PyTuple_New(temp + 1);
236-
for (int n = 0; n < i; n++)
238+
for (var n = 0; n < i; n++)
237239
{
238240
IntPtr item = Runtime.PyTuple_GetItem(args, n);
239241
Runtime.XIncref(item);
240242
Runtime.PyTuple_SetItem(real, n, item);
241243
}
242244

243245
// Add Default Args if needed
244-
for (int n = 0; n < numOfDefaultArgs; n++)
246+
for (var n = 0; n < numOfDefaultArgs; n++)
245247
{
246248
IntPtr item = Runtime.PyTuple_GetItem(defaultArgs, n);
247249
Runtime.XIncref(item);
@@ -288,23 +290,23 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
288290
{
289291
//ManagedType self = GetManagedObject(ob);
290292
IntPtr tp = Runtime.PyObject_TYPE(ob);
291-
ClassBase cb = (ClassBase)GetManagedObject(tp);
293+
var cb = (ClassBase)GetManagedObject(tp);
292294

293295
if (cb.type != typeof(Delegate))
294296
{
295297
Exceptions.SetError(Exceptions.TypeError, "object is not callable");
296298
return IntPtr.Zero;
297299
}
298300

299-
CLRObject co = (CLRObject)ManagedType.GetManagedObject(ob);
300-
Delegate d = co.inst as Delegate;
301+
var co = (CLRObject)GetManagedObject(ob);
302+
var d = co.inst as Delegate;
301303
BindingFlags flags = BindingFlags.Public |
302304
BindingFlags.NonPublic |
303305
BindingFlags.Instance |
304306
BindingFlags.Static;
305307

306308
MethodInfo method = d.GetType().GetMethod("Invoke", flags);
307-
MethodBinder binder = new MethodBinder(method);
309+
var binder = new MethodBinder(method);
308310
return binder.Invoke(ob, args, kw);
309311
}
310312
}

src/runtime/converter.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ static Converter()
5252
/// </summary>
5353
internal static Type GetTypeByAlias(IntPtr op)
5454
{
55-
if ((op == Runtime.PyStringType) ||
56-
(op == Runtime.PyUnicodeType))
55+
if (op == Runtime.PyStringType ||
56+
op == Runtime.PyUnicodeType)
5757
{
5858
return stringType;
5959
}
@@ -83,24 +83,24 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
8383
return Runtime.PyUnicodeType;
8484
}
8585

86-
else if (Runtime.IsPython3 && ((op == int16Type) ||
87-
(op == int32Type) ||
88-
(op == int64Type)))
86+
else if (Runtime.IsPython3 && (op == int16Type ||
87+
op == int32Type ||
88+
op == int64Type))
8989
{
9090
return Runtime.PyIntType;
9191
}
9292

93-
else if ((op == int16Type) ||
94-
(op == int32Type))
93+
else if (op == int16Type ||
94+
op == int32Type)
9595
{
9696
return Runtime.PyIntType;
9797
}
9898
else if (op == int64Type)
9999
{
100100
return Runtime.PyLongType;
101101
}
102-
else if ((op == doubleType) ||
103-
(op == singleType))
102+
else if (op == doubleType ||
103+
op == singleType)
104104
{
105105
return Runtime.PyFloatType;
106106
}
@@ -123,7 +123,7 @@ internal static IntPtr ToPython<T>(T value)
123123
return ToPython(value, typeof(T));
124124
}
125125

126-
internal static IntPtr ToPython(Object value, Type type)
126+
internal static IntPtr ToPython(object value, Type type)
127127
{
128128
if (value is PyObject)
129129
{
@@ -144,7 +144,7 @@ internal static IntPtr ToPython(Object value, Type type)
144144

145145
// it the type is a python subclass of a managed type then return the
146146
// underlying python object rather than construct a new wrapper object.
147-
IPythonDerivedType pyderived = value as IPythonDerivedType;
147+
var pyderived = value as IPythonDerivedType;
148148
if (null != pyderived)
149149
{
150150
return ClassDerivedObject.ToPython(pyderived);
@@ -221,7 +221,9 @@ internal static IntPtr ToPython(Object value, Type type)
221221
foreach (object o in (IEnumerable)value)
222222
{
223223
using (var p = new PyObject(ToPython(o, o?.GetType())))
224+
{
224225
resultlist.Append(p);
226+
}
225227
}
226228
Runtime.XIncref(resultlist.Handle);
227229
return resultlist.Handle;
@@ -237,7 +239,7 @@ internal static IntPtr ToPython(Object value, Type type)
237239
/// In a few situations, we don't have any advisory type information
238240
/// when we want to convert an object to Python.
239241
/// </summary>
240-
internal static IntPtr ToPythonImplicit(Object value)
242+
internal static IntPtr ToPythonImplicit(object value)
241243
{
242244
if (value == null)
243245
{
@@ -266,7 +268,7 @@ internal static bool ToManaged(IntPtr value, Type type,
266268

267269

268270
internal static bool ToManagedValue(IntPtr value, Type obType,
269-
out Object result, bool setError)
271+
out object result, bool setError)
270272
{
271273
if (obType == typeof(PyObject))
272274
{
@@ -290,8 +292,8 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
290292
result = tmp;
291293
return true;
292294
}
293-
string err = "value cannot be converted to {0}";
294-
err = String.Format(err, obType);
295+
var err = "value cannot be converted to {0}";
296+
err = string.Format(err, obType);
295297
Exceptions.SetError(Exceptions.TypeError, err);
296298
return false;
297299
}
@@ -474,7 +476,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
474476
}
475477
long ll = (long)Runtime.PyLong_AsLongLong(op);
476478
Runtime.XDecref(op);
477-
if ((ll == -1) && Exceptions.ErrorOccurred())
479+
if (ll == -1 && Exceptions.ErrorOccurred())
478480
{
479481
goto overflow;
480482
}
@@ -487,7 +489,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
487489
}
488490

489491
case TypeCode.Boolean:
490-
result = (Runtime.PyObject_IsTrue(value) != 0);
492+
result = Runtime.PyObject_IsTrue(value) != 0;
491493
return true;
492494

493495
case TypeCode.Byte:
@@ -791,9 +793,9 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
791793

792794
if (setError)
793795
{
794-
string format = "'{0}' value cannot be converted to {1}";
796+
var format = "'{0}' value cannot be converted to {1}";
795797
string tpName = Runtime.PyObject_GetTypeName(value);
796-
string error = String.Format(format, tpName, obType);
798+
string error = string.Format(format, tpName, obType);
797799
Exceptions.SetError(Exceptions.TypeError, error);
798800
}
799801

@@ -803,20 +805,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
803805

804806
if (setError)
805807
{
806-
string error = "value too large to convert";
808+
var error = "value too large to convert";
807809
Exceptions.SetError(Exceptions.OverflowError, error);
808810
}
809811

810812
return false;
811813
}
812814

813815

814-
static void SetConversionError(IntPtr value, Type target)
816+
private static void SetConversionError(IntPtr value, Type target)
815817
{
816818
IntPtr ob = Runtime.PyObject_Repr(value);
817819
string src = Runtime.GetManagedString(ob);
818820
Runtime.XDecref(ob);
819-
string error = String.Format("Cannot convert {0} to {1}", src, target);
821+
string error = string.Format("Cannot convert {0} to {1}", src, target);
820822
Exceptions.SetError(Exceptions.TypeError, error);
821823
}
822824

@@ -844,9 +846,9 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
844846
Array items = Array.CreateInstance(elementType, size);
845847

846848
// XXX - is there a better way to unwrap this if it is a real array?
847-
for (int i = 0; i < size; i++)
849+
for (var i = 0; i < size; i++)
848850
{
849-
Object obj = null;
851+
object obj = null;
850852
IntPtr item = Runtime.PySequence_GetItem(value, i);
851853
if (item == IntPtr.Zero)
852854
{
@@ -899,7 +901,7 @@ private static bool ToEnum(IntPtr value, Type obType, out object result, bool se
899901

900902
if (setError)
901903
{
902-
string error = "invalid enumeration value";
904+
var error = "invalid enumeration value";
903905
Exceptions.SetError(Exceptions.ValueError, error);
904906
}
905907

src/runtime/interop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal class ObjectOffset
7474
static ObjectOffset()
7575
{
7676
int size = IntPtr.Size;
77-
int n = 0; // Py_TRACE_REFS add two pointers to PyObject_HEAD
77+
var n = 0; // Py_TRACE_REFS add two pointers to PyObject_HEAD
7878
#if Py_DEBUG
7979
_ob_next = 0;
8080
_ob_prev = 1 * size;

src/runtime/moduleobject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ModuleObject(string name)
4343
IntPtr pyname = Runtime.PyString_FromString(moduleName);
4444
IntPtr pyfilename = Runtime.PyString_FromString(filename);
4545
IntPtr pydocstring = Runtime.PyString_FromString(docstring);
46-
IntPtr pycls = TypeManager.GetTypeHandle(this.GetType());
46+
IntPtr pycls = TypeManager.GetTypeHandle(GetType());
4747
Runtime.PyDict_SetItemString(dict, "__name__", pyname);
4848
Runtime.PyDict_SetItemString(dict, "__file__", pyfilename);
4949
Runtime.PyDict_SetItemString(dict, "__doc__", pydocstring);
@@ -318,7 +318,7 @@ public CLRModule() : base("clr")
318318
// import requires the module to pass PyModule_Check. :(
319319
if (!hacked)
320320
{
321-
IntPtr type = this.tpHandle;
321+
IntPtr type = tpHandle;
322322
IntPtr mro = Marshal.ReadIntPtr(type, TypeOffset.tp_mro);
323323
IntPtr ext = Runtime.ExtendTuple(mro, Runtime.PyModuleType);
324324
Marshal.WriteIntPtr(type, TypeOffset.tp_mro, ext);

src/runtime/pyobject.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,9 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
895895
return true;
896896
}
897897
else
898+
{
898899
return base.TryGetMember(binder, out result);
900+
}
899901
}
900902

901903
public override bool TrySetMember(SetMemberBinder binder, object value)
@@ -906,13 +908,18 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
906908
return true;
907909
}
908910
else
911+
{
909912
return base.TrySetMember(binder, value);
913+
}
910914
}
911915

912916
private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
913917
{
914918
int arg_count;
915-
for (arg_count = 0; arg_count < inargs.Length && !(inargs[arg_count] is Py.KeywordArguments); ++arg_count) ;
919+
for (arg_count = 0; arg_count < inargs.Length && !(inargs[arg_count] is Py.KeywordArguments); ++arg_count)
920+
{
921+
;
922+
}
916923
IntPtr argtuple = Runtime.PyTuple_New(arg_count);
917924
for (var i = 0; i < arg_count; i++)
918925
{
@@ -975,7 +982,9 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
975982
return true;
976983
}
977984
else
985+
{
978986
return base.TryInvokeMember(binder, args, out result);
987+
}
979988
}
980989

981990
public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
@@ -1003,7 +1012,9 @@ public override bool TryInvoke(InvokeBinder binder, object[] args, out object re
10031012
return true;
10041013
}
10051014
else
1015+
{
10061016
return base.TryInvoke(binder, args, out result);
1017+
}
10071018
}
10081019

10091020
public override bool TryConvert(ConvertBinder binder, out object result)

0 commit comments

Comments
 (0)