8000 fixes overloading of integer types which is py2/3 version dependent, … · pythonnet/pythonnet@6daccdd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6daccdd

Browse files
denfromufaden-run-ai
authored andcommitted
fixes overloading of integer types which is py2/3 version dependent, added tests for checking overloading behavior for main value types
1 parent aa87074 commit 6daccdd

File tree

4 files changed

+102
-16
lines changed

4 files changed

+102
-16
lines changed

demo/wordpad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def InitializeComponent(self):
205205
self.richTextBox.TabIndex = 0
206206
self.richTextBox.AutoSize = 1
207207
self.richTextBox.ScrollBars = WinForms.RichTextBoxScrollBars.ForcedBoth
208-
self.richTextBox.Font = System.Drawing.Font("Tahoma", 10)
208+
self.richTextBox.Font = System.Drawing.Font("Tahoma", 10.0)
209209
self.richTextBox.AcceptsTab = 1
210210
self.richTextBox.Location = System.Drawing.Point(0, 0)
211211

src/runtime/converter.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ private Converter() {}
2929
static NumberFormatInfo nfi;
3030
static Type objectType;
3131
static Type stringType;
32+
static Type singleType;
3233
static Type doubleType;
34+
static Type decimalType;
35+
static Type int16Type;
3336
static Type int32Type;
3437
static Type int64Type;
3538
static Type flagsType;
@@ -40,9 +43,12 @@ static Converter () {
4043
nfi = NumberFormatInfo.InvariantInfo;
4144
objectType = typeof(Object);
4245
stringType = typeof(String);
46+
int16Type = typeof(Int16);
4347
int32Type = typeof(Int32);
4448
int64Type = typeof(Int64);
49+
singleType = typeof(Single);
4550
doubleType = typeof(Double);
51+
decimalType = typeof(Decimal);
4652
flagsType = typeof(FlagsAttribute);
4753
boolType = typeof(Boolean);
4854
typeType = typeof(Type);
@@ -73,6 +79,35 @@ internal static Type GetTypeByAlias(IntPtr op) {
7379
return null;
7480
}
7581

82+
internal static IntPtr GetPythonTypeByAlias(Type op)
83+
{
84+
if (op == stringType) {
85+
return Runtime.PyUnicodeType;
86+
}
87+
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
88+
else if ((op == int16Type) ||
89+
(op == int32Type) ||
90+
(op == int64Type)) {
91+
return Runtime.PyIntType;
92+
}
93+
#endif
94+
else if ((op == int16Type) ||
95+
(op == int32Type)) {
96+
return Runtime.PyIntType;
97+
}
98+
else if (op == int64Type) {
99+
return Runtime.PyLongType;
100+
}
101+
else if ((op == doubleType) ||
102+
(op == singleType)) {
103+
return Runtime.PyFloatType;
104+
}
105+
else if (op == boolType) {
106+
return Runtime.PyBoolType;
107+
}
108+
return IntPtr.Zero;
109+
}
110+
76111

77112
//====================================================================
78113
// Return a Python object for the given native object, converting

src/runtime/methodbinder.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
245245
else {
246246
_methods = GetMethods();
247247
}
248-
Type type;
248+
Type clrtype;
249249
for (int i = 0; i < _methods.Length; i++) {
250250
MethodBase mi = _methods[i];
251251

@@ -295,34 +295,52 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
295295
// this logic below handles cases when multiple overloading methods
296296
// are ambiguous, hence comparison between Python and CLR types
297297
// is necessary
298-
type = null;
299-
300-
if (_methods.Length>1) {
301-
IntPtr pyoptype = IntPtr.Zero;
298+
clrtype = null;
299+
IntPtr pyoptype;
300+
if (_methods.Length > 1) {
301+
pyoptype = IntPtr.Zero;
302302
pyoptype = Runtime.PyObject_Type(op);
303303
Exceptions.Clear();
304-
if (pyoptype != IntPtr.Zero) { }
305-
type = Converter.GetTypeByAlias(pyoptype);
306-
Runtime.Decref(pyoptype);
304+
if (pyoptype != IntPtr.Zero) {
305+
clrtype = Converter.GetTypeByAlias(pyoptype);
307306
}
307+
Runtime.Decref(pyoptype);
308+
}
308309

309310

310-
if (type != null) {
311-
if (pi[n].ParameterType != type) {
312-
margs = null;
313-
break;
311+
if (clrtype != null) {
312+
if (pi[n].ParameterType != clrtype) {
313+
IntPtr pytype = Converter.GetPythonTypeByAlias(pi[n].ParameterType);
314+
pyoptype = Runtime.PyObject_Type(op);
315+
Exceptions.Clear();
316+
if (pyoptype != IntPtr.Zero) {
317+
if (pytype != pyoptype) {
318+
Runtime.Decref(pyoptype);
319+
margs = null;
320+
break;
321+
}
322+
else {
323+
clrtype = pi[n].ParameterType;
324+
}
325+
}
326+
else {
327+
Runtime.Decref(pyoptype);
328+
margs = null;
329+
break;
330+
}
331+
Runtime.Decref(pyoptype);
314332
}
315333
}
316334
else {
317-
type = pi[n].ParameterType;
335+
clrtype = pi[n].ParameterType;
318336
}
319337

320-
if (pi[n].IsOut || type.IsByRef)
338+
if (pi[n].IsOut || clrtype.IsByRef)
321339
{
322340
outs++;
323341
}
324342

325-
if (!Converter.ToManaged(op, type, out arg, false))
343+
if (!Converter.ToManaged(op, clrtype, out arg, false))
326344
{
327345
Exceptions.Clear();
328346
margs = null;

src/tests/test_generic.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,39 @@ def testGenericMethodTypeHandling(self):
341341
self._testGenericMethodByType(InterfaceTest, InterfaceTest(), 1)
342342
self._testGenericMethodByType(ISayHello1, InterfaceTest(), 1)
343343

344+
def testCorrectOverloadSelection(self):
345+
"""
346+
Test correct overloading selection for common types.
347+
"""
348+
from System.Drawing import Font
349+
350+
from System import (String, Double, Single,
351+
Int16, Int32, Int64)
352+
from System import Math
353+
354+
substr = String("substring")
355+
self.assertTrue(substr.Substring(2) == substr.Substring.__overloads__[Int32](
356+
Int32(2)))
357+
self.assertTrue(substr.Substring(2, 3) == substr.Substring.__overloads__[Int32,Int32](
358+
Int32(2), Int32(3)))
359+
360+
for atype, value1, value2 in zip([Double, Single, Int16, Int32, Int64],
361+
[1.0, 1.0, 1, 1, 1],
362+
[2.0, 0.5, 2, 0, -1]):
363+
self.assertTrue(Math.Abs(atype(value1)) == Math.Abs.__overloads__[atype](atype(value1)))
364+
self.assertTrue(Math.Abs(value1) == Math.Abs.__overloads__[atype](atype(value1)))
365+
self.assertTrue(
366+
Math.Max(atype(value1),
367+
atype(value2)) == Math.Max.__overloads__[atype, atype](
368+
atype(value1),
369+
atype(value2)))
370+
if (atype is Int64) and six.PY2:
371+
value2 = long(value2)
372+
self.assertTrue(
373+
Math.Max(atype(value1),
374+
value2) == Math.Max.__overloads__[atype, atype](
375+
atype(value1),
376+
atype(value2)))
344377

345378
def testGenericMethodOverloadSelection(self):
346379
"""

0 commit comments

Comments
 (0)
0