Skip to content

fixes overloading of integer types which is py2/3 version dependent #175

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
Mar 3, 2016
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
2 changes: 1 addition & 1 deletion demo/wordpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def InitializeComponent(self):
self.richTextBox.TabIndex = 0
self.richTextBox.AutoSize = 1
self.richTextBox.ScrollBars = WinForms.RichTextBoxScrollBars.ForcedBoth
self.richTextBox.Font = System.Drawing.Font("Tahoma", 10)
self.richTextBox.Font = System.Drawing.Font("Tahoma", 10.0)
self.richTextBox.AcceptsTab = 1
self.richTextBox.Location = System.Drawing.Point(0, 0)

Expand Down
35 changes: 35 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ private Converter() {}
static NumberFormatInfo nfi;
static Type objectType;
static Type stringType;
static Type singleType;
static Type doubleType;
static Type decimalType;
static Type int16Type;
static Type int32Type;
static Type int64Type;
static Type flagsType;
Expand All @@ -40,9 +43,12 @@ static Converter () {
nfi = NumberFormatInfo.InvariantInfo;
objectType = typeof(Object);
stringType = typeof(String);
int16Type = typeof(Int16);
int32Type = typeof(Int32);
int64Type = typeof(Int64);
singleType = typeof(Single);
doubleType = typeof(Double);
decimalType = typeof(Decimal);
flagsType = typeof(FlagsAttribute);
boolType = typeof(Boolean);
typeType = typeof(Type);
Expand Down Expand Up @@ -73,6 +79,35 @@ internal static Type GetTypeByAlias(IntPtr op) {
return null;
}

internal static IntPtr GetPythonTypeByAlias(Type op)
{
if (op == stringType) {
return Runtime.PyUnicodeType;
}
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
else if ((op == int16Type) ||
(op == int32Type) ||
(op == int64Type)) {
return Runtime.PyIntType;
}
#endif
else if ((op == int16Type) ||
(op == int32Type)) {
return Runtime.PyIntType;
}
else if (op == int64Type) {
return Runtime.PyLongType;
}
else if ((op == doubleType) ||
(op == singleType)) {
return Runtime.PyFloatType;
}
else if (op == boolType) {
return Runtime.PyBoolType;
}
return IntPtr.Zero;
}


//====================================================================
// Return a Python object for the given native object, converting
Expand Down
48 changes: 33 additions & 15 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
else {
_methods = GetMethods();
}
Type type;
Type clrtype;
for (int i = 0; i < _methods.Length; i++) {
MethodBase mi = _methods[i];

Expand Down Expand Up @@ -295,34 +295,52 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
// this logic below handles cases when multiple overloading methods
// are ambiguous, hence comparison between Python and CLR types
// is necessary
type = null;

if (_methods.Length>1) {
IntPtr pyoptype = IntPtr.Zero;
clrtype = null;
IntPtr pyoptype;
if (_methods.Length > 1) {
pyoptype = IntPtr.Zero;
pyoptype = Runtime.PyObject_Type(op);
Exceptions.Clear();
if (pyoptype != IntPtr.Zero) { }
type = Converter.GetTypeByAlias(pyoptype);
Runtime.Decref(pyoptype);
if (pyoptype != IntPtr.Zero) {
clrtype = Converter.GetTypeByAlias(pyoptype);
}
Runtime.Decref(pyoptype);
}


if (type != null) {
if (pi[n].ParameterType != type) {
margs = null;
break;
if (clrtype != null) {
if (pi[n].ParameterType != clrtype) {
IntPtr pytype = Converter.GetPythonTypeByAlias(pi[n].ParameterType);
pyoptype = Runtime.PyObject_Type(op);
Exceptions.Clear();
if (pyoptype != IntPtr.Zero) {
if (pytype != pyoptype) {
Runtime.Decref(pyoptype);
margs = null;
break;
}
else {
clrtype = pi[n].ParameterType;
}
}
else {
Runtime.Decref(pyoptype);
margs = null;
break;
}
Runtime.Decref(pyoptype);
}
}
else {
type = pi[n].ParameterType;
clrtype = pi[n].ParameterType;
}

if (pi[n].IsOut || type.IsByRef)
if (pi[n].IsOut || clrtype.IsByRef)
{
outs++;
}

if (!Converter.ToManaged(op, type, out arg, false))
if (!Converter.ToManaged(op, clrtype, out arg, false))
{
Exceptions.Clear();
margs = null;
Expand Down
33 changes: 33 additions & 0 deletions src/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,39 @@ def testGenericMethodTypeHandling(self):
self._testGenericMethodByType(InterfaceTest, InterfaceTest(), 1)
self._testGenericMethodByType(ISayHello1, InterfaceTest(), 1)

def testCorrectOverloadSelection(self):
"""
Test correct overloading selection for common types.
"""
from System.Drawing import Font

from System import (String, Double, Single,
Int16, Int32, Int64)
from System import Math

substr = String("substring")
self.assertTrue(substr.Substring(2) == substr.Substring.__overloads__[Int32](
Int32(2)))
self.assertTrue(substr.Substring(2, 3) == substr.Substring.__overloads__[Int32,Int32](
Int32(2), Int32(3)))

for atype, value1, value2 in zip([Double, Single, Int16, Int32, Int64],
[1.0, 1.0, 1, 1, 1],
[2.0, 0.5, 2, 0, -1]):
self.assertTrue(Math.Abs(atype(value1)) == Math.Abs.__overloads__[atype](atype(value1)))
self.assertTrue(Math.Abs(value1) == Math.Abs.__overloads__[atype](atype(value1)))
self.assertTrue(
Math.Max(atype(value1),
atype(value2)) == Math.Max.__overloads__[atype, atype](
atype(value1),
atype(value2)))
if (atype is Int64) and six.PY2:
value2 = long(value2)
self.assertTrue(
Math.Max(atype(value1),
value2) == Math.Max.__overloads__[atype, atype](
atype(value1),
atype(value2)))

def testGenericMethodOverloadSelection(self):
"""
Expand Down