Skip to content

Commit 2a2c9c7

Browse files
committed
added intial test that illustrates how int->double conversion works in calling a constructor, prior to the change in the methodbinder.cs, this test failed, since the no-match in ct. was failing silently, now it works. Next step is to give exception when ct args do not match
1 parent 5f21a85 commit 2a2c9c7

File tree

9 files changed

+77
-6
lines changed

9 files changed

+77
-6
lines changed

pythonnet.sln

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.30110.0
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25123.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Python.Runtime", "src\runtime\Python.Runtime.csproj", "{097B4AC0-74E9-4C58-BCF8-C69746EC8271}"
77
EndProject
@@ -89,10 +89,18 @@ Global
8989
{E29DCF0A-5114-4A98-B1DD-71264B6EA349}.ReleaseWin|x64.Build.0 = Release|x64
9090
{E29DCF0A-5114-4A98-B1DD-71264B6EA349}.ReleaseWin|x86.ActiveCfg = Release|x86
9191
{E29DCF0A-5114-4A98-B1DD-71264B6EA349}.ReleaseWin|x86.Build.0 = Release|x86
92+
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugMono|x64.ActiveCfg = DebugMono|x64
93+
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugMono|x64.Build.0 = DebugMono|x64
94+
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugMono|x86.ActiveCfg = DebugMono|x86
95+
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugMono|x86.Build.0 = DebugMono|x86
9296
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugWin|x64.ActiveCfg = DebugWin|x64
9397
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugWin|x64.Build.0 = DebugWin|x64
9498
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugWin|x86.ActiveCfg = DebugWin|x86
9599
{86E834DE-1139-4511-96CC-69636A56E7AC}.DebugWin|x86.Build.0 = DebugWin|x86
100+
{86E834DE-1139-4511-96CC-69636A56E7AC}.ReleaseMono|x64.ActiveCfg = ReleaseMono|x64
101+
{86E834DE-1139-4511-96CC-69636A56E7AC}.ReleaseMono|x64.Build.0 = ReleaseMono|x64
102+
{86E834DE-1139-4511-96CC-69636A56E7AC}.ReleaseMono|x86.ActiveCfg = ReleaseMono|x86
103+
{86E834DE-1139-4511-96CC-69636A56E7AC}.ReleaseMono|x86.Build.0 = ReleaseMono|x86
96104
{86E834DE-1139-4511-96CC-69636A56E7AC}.ReleaseWin|x64.ActiveCfg = ReleaseWin|x64
97105
{86E834DE-1139-4511-96CC-69636A56E7AC}.ReleaseWin|x64.Build.0 = ReleaseWin|x64
98106
{86E834DE-1139-4511-96CC-69636A56E7AC}.ReleaseWin|x86.ActiveCfg = ReleaseWin|x86

src/clrmodule/clrmodule.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
108108
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
109109
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
110+
<DefineConstants>PYTHON34;UCS2</DefineConstants>
110111
</PropertyGroup>
111112
<ItemGroup>
112113
<Reference Include="RGiesecke.DllExport.Metadata">

src/console/Console.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
145145
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
146146
<WarningLevel>4</WarningLevel>
147+
<DefineConstants>TRACE;PYTHON34, UCS2</DefineConstants>
147148
</PropertyGroup>
148149
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'EmbeddingTest|x64'">
149150
<DebugSymbols>True</DebugSymbols>
@@ -196,7 +197,7 @@
196197
<ItemGroup>
197198
<Content Include="python-clear.ico" />
198199
<EmbeddedResource Include="$(PythonBuildDir)\Python.Runtime.dll">
199-
<LogicalName>Python.Runtime.dll</LogicalName>
200+
<LogicalName>Python.Runtime.dll</LogicalName>
200201
</EmbeddedResource>
201202
</ItemGroup>
202203
<ItemGroup>

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
127127
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
128128
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
129+
<DefineConstants>PYTHON34, UCS2</DefineConstants>
129130
</PropertyGroup>
130131
<ItemGroup>
131132
<Reference Include="System" />

src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<PlatformTarget>x64</PlatformTarget>
5252
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
5353
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
54+
<DefineConstants>PYTHON34, UCS2</DefineConstants>
5455
</PropertyGroup>
5556
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|x86'">
5657
<DebugSymbols>true</DebugSymbols>

src/runtime/methodbinder.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,20 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
375375
Runtime.Decref(pyoptype);
376376
if (!typematch)
377377
{
378-
margs = null;
379-
break;
378+
if (SimplePromotableType(clrtype, pi[n].ParameterType))
379+
{
380+
if (!Converter.ToManaged(op, pi[n].ParameterType, out arg, false))
381+
{
382+
margs = null;// basically we are failing this match, but can't throw yet
383+
break;
384+
}
385+
}
386+
else
387+
{
388+
margs = null;
389+
break;
390+
}
391+
380392
}
381393
}
382394
else
@@ -455,6 +467,18 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
455467
}
456468
return null;
457469
}
470+
/// <summary>
471+
/// Returns true if src type (int -type) is promotable to a higher resolution type (float|double)
472+
/// </summary>
473+
/// <param name="src"></param>
474+
/// <param name="dst"></param>
475+
/// <returns></returns>
476+
private bool SimplePromotableType(Type src, Type dst)
477+
{
478+
return (((src == typeof(int) || src == typeof(short)) && (dst == typeof(double) || dst == typeof(float))))
479+
|| ((src == typeof(long)) && (dst == typeof(double)))
480+
|| ((src ==typeof(int) || src == typeof(short)) && (dst == typeof(long)));
481+
}
458482

459483
internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw)
460484
{

src/testing/Python.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
106106
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
107107
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
108+
<DefineConstants>PYTHON34, UCS2</DefineConstants>
108109
</PropertyGroup>
109110
<ItemGroup>
110111
<Compile Include="arraytest.cs" />

src/testing/constructortests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,28 @@ namespace Python.Test
77
//========================================================================
88
// These classes support the CLR constructor unit tests.
99
//========================================================================
10-
10+
public class AConstrucorTest
11+
{
12+
public string name;
13+
public AConstrucorTest(string n) { name = n; }
14+
}
15+
public class LinkConstructorTest
16+
{
17+
public LinkConstructorTest()
18+
{
19+
DefaultCtCalled = true;
20+
}
21+
public LinkConstructorTest(AConstrucorTest a,double matchMe,AConstrucorTest b)
22+
{
23+
MatchMe = matchMe;
24+
a1 = a;
25+
a2 = b;
26+
}
27+
public bool DefaultCtCalled;
28+
public double MatchMe;
29+
public AConstrucorTest a1;
30+
public AConstrucorTest a2;
31+
}
1132
public class EnumConstructorTest
1233
{
1334
public TypeCode value;

src/tests/test_constructors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ class sub(System.Exception):
4747
print(ob)
4848
self.assertTrue(isinstance(ob.value, System.Exception))
4949

50+
def testConstructorArgumentMatching(self):
51+
""" Test that simple type promitions works for int->double """
52+
from Python.Test import AConstrucorTest, LinkConstructorTest
53+
a1=AConstrucorTest('a1')
54+
a2=AConstrucorTest('a2')
55+
self.assertEqual(a1.name,'a1')
56+
self.assertEqual(a2.name, 'a2')
57+
l1=LinkConstructorTest(a1,3000,a2)
58+
#l2=LinkConstructorTest(a1,3000.0,a2)
59+
self.assertEqual(l1.a1.name, a1.name)
60+
self.assertEqual(l1.a2.name, a2.name)
61+
self.assertAlmostEqual(3000.0,l1.MatchMe)
62+
5063

5164
def test_suite():
5265
return unittest.makeSuite(ConstructorTests)

0 commit comments

Comments
 (0)