Skip to content

Commit 06a2960

Browse files
committed
Refactor converter.cs & methodbind*.cs
1 parent 54841da commit 06a2960

File tree

3 files changed

+97
-117
lines changed

3 files changed

+97
-117
lines changed

src/runtime/converter.cs

+38-47
Original file line numberDiff line numberDiff line change
@@ -52,62 +52,53 @@ static Converter()
5252
/// </summary>
5353
internal static Type GetTypeByAlias(IntPtr op)
5454
{
55-
if (op == Runtime.PyStringType ||
56-
op == Runtime.PyUnicodeType)
57-
{
55+
if (op == Runtime.PyStringType)
5856
return stringType;
59-
}
60-
else if (op == Runtime.PyIntType)
61-
{
57+
58+
if (op == Runtime.PyUnicodeType)
59+
return stringType;
60+
61+
if (op == Runtime.PyIntType)
6262
return int32Type;
63-
}
64-
else if (op == Runtime.PyLongType)
65-
{
63+
64+
if (op == Runtime.PyLongType)
6665
return int64Type;
67-
}
68-
else if (op == Runtime.PyFloatType)
69-
{
66+
67+
if (op == Runtime.PyFloatType)
7068
return doubleType;
71-
}
72-
else if (op == Runtime.PyBoolType)
73-
{
69+
70+
if (op == Runtime.PyBoolType)
7471
return boolType;
75-
}
72+
7673
return null;
7774
}
7875

7976
internal static IntPtr GetPythonTypeByAlias(Type op)
8077
{
8178
if (op == stringType)
82-
{
8379
return Runtime.PyUnicodeType;
84-
}
8580

86-
else if (Runtime.IsPython3 && (op == int16Type ||
87-
op == int32Type ||
88-
op == int64Type))
89-
{
81+
if (op == int16Type)
9082
return Runtime.PyIntType;
91-
}
9283

93-
else if (op == int16Type ||
94-
op == int32Type)
95-
{
84+
if (op == int32Type)
9685
return Runtime.PyIntType;
97-
}
98-
else if (op == int64Type)
99-
{
86+
87+
if (op == int64Type && Runtime.IsPython2)
10088
return Runtime.PyLongType;
101-
}
102-
else if (op == doubleType ||
103-
op == singleType)
104-
{
89+
90+
if (op == int64Type)
91+
return Runtime.PyIntType;
92+
93+
if (op == doubleType)
10594
return Runtime.PyFloatType;
106-
}
107-
else if (op == boolType)
108-
{
95+
96+
if (op == singleType)
97+
return Runtime.PyFloatType;
98+
99+
if (op == boolType)
109100
return Runtime.PyBoolType;
110-
}
101+
111102
return IntPtr.Zero;
112103
}
113104

@@ -329,27 +320,27 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
329320
return ToPrimitive(value, stringType, out result, setError);
330321
}
331322

332-
else if (Runtime.PyBool_Check(value))
323+
if (Runtime.PyBool_Check(value))
333324
{
334325
return ToPrimitive(value, boolType, out result, setError);
335326
}
336327

337-
else if (Runtime.PyInt_Check(value))
328+
if (Runtime.PyInt_Check(value))
338329
{
339330
return ToPrimitive(value, int32Type, out result, setError);
340331
}
341332

342-
else if (Runtime.PyLong_Check(value))
333+
if (Runtime.PyLong_Check(value))
343334
{
344335
return ToPrimitive(value, int64Type, out result, setError);
345336
}
346337

347-
else if (Runtime.PyFloat_Check(value))
338+
if (Runtime.PyFloat_Check(value))
348339
{
349340
return ToPrimitive(value, doubleType, out result, setError);
350341
}
351342

352-
else if (Runtime.PySequence_Check(value))
343+
if (Runtime.PySequence_Check(value))
353344
{
354345
return ToArray(value, typeof(object[]), out result, setError);
355346
}
@@ -371,31 +362,31 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
371362
return true;
372363
}
373364

374-
else if (value == Runtime.PyBoolType)
365+
if (value == Runtime.PyBoolType)
375366
{
376367
result = boolType;
377368
return true;
378369
}
379370

380-
else if (value == Runtime.PyIntType)
371+
if (value == Runtime.PyIntType)
381372
{
382373
result = int32Type;
383374
return true;
384375
}
385376

386-
else if (value == Runtime.PyLongType)
377+
if (value == Runtime.PyLongType)
387378
{
388379
result = int64Type;
389380
return true;
390381
}
391382

392-
else if (value == Runtime.PyFloatType)
383+
if (value == Runtime.PyFloatType)
393384
{
394385
result = doubleType;
395386
return true;
396387
}
397388

398-
else if (value == Runtime.PyListType || value == Runtime.PyTupleType)
389+
if (value == Runtime.PyListType || value == Runtime.PyTupleType)
399390
{
400391
result = typeof(object[]);
401392
return true;

src/runtime/methodbinder.cs

+45-54
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ internal MethodBase[] GetMethods()
170170
/// Precedence algorithm largely lifted from jython - the concerns are
171171
/// generally the same so we'll start w/this and tweak as necessary.
172172
/// </summary>
173+
/// <remarks>
174+
/// TODO: Add link to specific Jython Section/Code/File
175+
/// </remarks>
173176
internal static int GetPrecedence(MethodBase mi)
174177
{
175178
ParameterInfo[] pi = mi.GetParameters();
@@ -198,61 +201,49 @@ internal static int ArgPrecedence(Type t)
198201

199202
TypeCode tc = Type.GetTypeCode(t);
200203
// TODO: Clean up
201-
if (tc == TypeCode.Object)
204+
switch (tc)
202205
{
203-
return 1;
204-
}
205-
if (tc == TypeCode.UInt64)
206-
{
207-
return 10;
208-
}
209-
if (tc == TypeCode.UInt32)
210-
{
211-
return 11;
212-
}
213-
if (tc == TypeCode.UInt16)
214-
{
215-
return 12;
216-
}
217-
if (tc == TypeCode.Int64)
218-
{
219-
return 13;
220-
}
221-
if (tc == TypeCode.Int32)
222-
{
223-
return 14;
224-
}
225-
if (tc == TypeCode.Int16)
226-
{
227-
return 15;
228-
}
229-
if (tc == TypeCode.Char)
230-
{
231-
return 16;
232-
}
233-
if (tc == TypeCode.SByte)
234-
{
235-
return 17;
236-
}
237-
if (tc == TypeCode.Byte)
238-
{
239-
return 18;
240-
}
241-
if (tc == TypeCode.Single)
242-
{
243-
return 20;
244-
}
245-
if (tc == TypeCode.Double)
246-
{
247-
return 21;
248-
}
249-
if (tc == TypeCode.String)
250-
{
251-
return 30;
252-
}
253-
if (tc == TypeCode.Boolean)
254-
{
255-
return 40;
206+
case TypeCode.Object:
207+
return 1;
208+
209+
case TypeCode.UInt64:
210+
return 10;
211+
212+
case TypeCode.UInt32:
213+
return 11;
214+
215+
case TypeCode.UInt16:
216+
return 12;
217+
218+
case TypeCode.Int64:
219+
return 13;
220+
221+
case TypeCode.Int32:
222+
return 14;
223+
224+
case TypeCode.Int16:
225+
return 15;
226+
227+
case TypeCode.Char:
228+
return 16;
229+
230+
case TypeCode.SByte:
231+
return 17;
232+
233+
case TypeCode.Byte:
234+
return 18;
235+
236+
case TypeCode.Single:
237+
return 20;
238+
239+
case TypeCode.Double:
240+
return 21;
241+
242+
case TypeCode.String:
243+
return 30;
244+
245+
case TypeCode.Boolean:
246+
return 40;
256247
}
257248

258249
if (t.IsArray)

src/runtime/methodbinding.cs

+14-16
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public static IntPtr mp_subscript(IntPtr tp, IntPtr idx)
5555
return Exceptions.RaiseTypeError("No match found for given type params");
5656
}
5757

58-
var mb = new MethodBinding(self.m, self.target);
59-
mb.info = mi;
58+
var mb = new MethodBinding(self.m, self.target) { info = mi };
6059
Runtime.XIncref(mb.pyHandle);
6160
return mb.pyHandle;
6261
}
@@ -76,22 +75,21 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
7675
}
7776

7877
string name = Runtime.GetManagedString(key);
79-
if (name == "__doc__")
78+
switch (name)
8079
{
81-
IntPtr doc = self.m.GetDocString();
82-
Runtime.XIncref(doc);
83-
return doc;
80+
case "__doc__":
81+
IntPtr doc = self.m.GetDocString();
82+
Runtime.XIncref(doc);
83+
return doc;
84+
// FIXME: deprecate __overloads__ soon...
85+
case "__overloads__":
86+
case "Overloads":
87+
var om = new OverloadMapper(self.m, self.target);
88+
Runtime.XIncref(om.pyHandle);
89+
return om.pyHandle;
90+
default:
91+
return Runtime.PyObject_GenericGetAttr(ob, key);
8492
}
85-
86-
// FIXME: deprecate __overloads__ soon...
87-
if (name == "__overloads__" || name == "Overloads")
88-
{
89-
var om = new OverloadMapper(self.m, self.target);
90-
Runtime.XIncref(om.pyHandle);
91-
return om.pyHandle;
92-
}
93-
94-
return Runtime.PyObject_GenericGetAttr(ob, key);
9593
}
9694

9795

0 commit comments

Comments
 (0)