Skip to content

Refactor converter.cs & methodbind*.cs #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Refactor converter.cs & methodbind*.cs
  • Loading branch information
vmuriart committed Feb 14, 2017
commit a14ef82cbb94ef422713079823c11cfe2b63079e
85 changes: 38 additions & 47 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,53 @@ static Converter()
/// </summary>
internal static Type GetTypeByAlias(IntPtr op)
{
if (op == Runtime.PyStringType ||
op == Runtime.PyUnicodeType)
{
if (op == Runtime.PyStringType)
return stringType;
}
else if (op == Runtime.PyIntType)
{

if (op == Runtime.PyUnicodeType)
return stringType;

if (op == Runtime.PyIntType)
return int32Type;
}
else if (op == Runtime.PyLongType)
{

if (op == Runtime.PyLongType)
return int64Type;
}
else if (op == Runtime.PyFloatType)
{

if (op == Runtime.PyFloatType)
return doubleType;
}
else if (op == Runtime.PyBoolType)
{

if (op == Runtime.PyBoolType)
return boolType;
}

return null;
}

internal static IntPtr GetPythonTypeByAlias(Type op)
{
if (op == stringType)
{
return Runtime.PyUnicodeType;
}

else if (Runtime.IsPython3 && (op == int16Type ||
op == int32Type ||
op == int64Type))
{
if (op == int16Type)
return Runtime.PyIntType;
}

else if (op == int16Type ||
op == int32Type)
{
if (op == int32Type)
return Runtime.PyIntType;
}
else if (op == int64Type)
{

if (op == int64Type && Runtime.IsPython2)
return Runtime.PyLongType;
}
else if (op == doubleType ||
op == singleType)
{

if (op == int64Type)
return Runtime.PyIntType;

if (op == doubleType)
return Runtime.PyFloatType;
}
else if (op == boolType)
{

if (op == singleType)
return Runtime.PyFloatType;

if (op == boolType)
return Runtime.PyBoolType;
}

return IntPtr.Zero;
}

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

else if (Runtime.PyBool_Check(value))
if (Runtime.PyBool_Check(value))
{
return ToPrimitive(value, boolType, out result, setError);
}

else if (Runtime.PyInt_Check(value))
if (Runtime.PyInt_Check(value))
{
return ToPrimitive(value, int32Type, out result, setError);
}

else if (Runtime.PyLong_Check(value))
if (Runtime.PyLong_Check(value))
{
return ToPrimitive(value, int64Type, out result, setError);
}

else if (Runtime.PyFloat_Check(value))
if (Runtime.PyFloat_Check(value))
{
return ToPrimitive(value, doubleType, out result, setError);
}

else if (Runtime.PySequence_Check(value))
if (Runtime.PySequence_Check(value))
{
return ToArray(value, typeof(object[]), out result, setError);
}
Expand All @@ -371,31 +362,31 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
return true;
}

else if (value == Runtime.PyBoolType)
if (value == Runtime.PyBoolType)
{
result = boolType;
return true;
}

else if (value == Runtime.PyIntType)
if (value == Runtime.PyIntType)
{
result = int32Type;
return true;
}

else if (value == Runtime.PyLongType)
if (value == Runtime.PyLongType)
{
result = int64Type;
return true;
}

else if (value == Runtime.PyFloatType)
if (value == Runtime.PyFloatType)
{
result = doubleType;
return true;
}

else if (value == Runtime.PyListType || value == Runtime.PyTupleType)
if (value == Runtime.PyListType || value == Runtime.PyTupleType)
{
result = typeof(object[]);
return true;
Expand Down
99 changes: 45 additions & 54 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ internal MethodBase[] GetMethods()
/// Precedence algorithm largely lifted from jython - the concerns are
/// generally the same so we'll start w/this and tweak as necessary.
/// </summary>
/// <remarks>
/// TODO: Add link to specific Jython Section/Code/File
/// </remarks>
internal static int GetPrecedence(MethodBase mi)
{
ParameterInfo[] pi = mi.GetParameters();
Expand Down Expand Up @@ -198,61 +201,49 @@ internal static int ArgPrecedence(Type t)

TypeCode tc = Type.GetTypeCode(t);
// TODO: Clean up
if (tc == TypeCode.Object)
switch (tc)
{
return 1;
}
if (tc == TypeCode.UInt64)
{
return 10;
}
if (tc == TypeCode.UInt32)
{
return 11;
}
if (tc == TypeCode.UInt16)
{
return 12;
}
if (tc == TypeCode.Int64)
{
return 13;
}
if (tc == TypeCode.Int32)
{
return 14;
}
if (tc == TypeCode.Int16)
{
return 15;
}
if (tc == TypeCode.Char)
{
return 16;
}
if (tc == TypeCode.SByte)
{
return 17;
}
if (tc == TypeCode.Byte)
{
return 18;
}
if (tc == TypeCode.Single)
{
return 20;
}
if (tc == TypeCode.Double)
{
return 21;
}
if (tc == TypeCode.String)
{
return 30;
}
if (tc == TypeCode.Boolean)
{
return 40;
case TypeCode.Object:
return 1;

case TypeCode.UInt64:
return 10;

case TypeCode.UInt32:
return 11;

case TypeCode.UInt16:
return 12;

case TypeCode.Int64:
return 13;

case TypeCode.Int32:
return 14;

case TypeCode.Int16:
return 15;

case TypeCode.Char:
return 16;

case TypeCode.SByte:
return 17;

case TypeCode.Byte:
return 18;

case TypeCode.Single:
return 20;

case TypeCode.Double:
return 21;

case TypeCode.String:
return 30;

case TypeCode.Boolean:
return 40;
}

if (t.IsArray)
Expand Down
30 changes: 14 additions & 16 deletions src/runtime/methodbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public static IntPtr mp_subscript(IntPtr tp, IntPtr idx)
return Exceptions.RaiseTypeError("No match found for given type params");
}

var mb = new MethodBinding(self.m, self.target);
mb.info = mi;
var mb = new MethodBinding(self.m, self.target) { info = mi };
Runtime.XIncref(mb.pyHandle);
return mb.pyHandle;
}
Expand All @@ -76,22 +75,21 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
}

string name = Runtime.GetManagedString(key);
if (name == "__doc__")
switch (name)
{
IntPtr doc = self.m.GetDocString();
Runtime.XIncref(doc);
return doc;
case "__doc__":
IntPtr doc = self.m.GetDocString();
Runtime.XIncref(doc);
return doc;
// FIXME: deprecate __overloads__ soon...
case "__overloads__":
case "Overloads":
var om = new OverloadMapper(self.m, self.target);
Runtime.XIncref(om.pyHandle);
return om.pyHandle;
default:
return Runtime.PyObject_GenericGetAttr(ob, key);
}

// FIXME: deprecate __overloads__ soon...
if (name == "__overloads__" || name == "Overloads")
{
var om = new OverloadMapper(self.m, self.target);
Runtime.XIncref(om.pyHandle);
return om.pyHandle;
}

return Runtime.PyObject_GenericGetAttr(ob, key);
}


Expand Down