Skip to content

Constructor argument matching supports simple int to (float|double) types #239

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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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 pythonnet.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Python.Runtime", "src\runtime\Python.Runtime.csproj", "{097B4AC0-74E9-4C58-BCF8-C69746EC8271}"
EndProject
Expand Down
2 changes: 1 addition & 1 deletion src/clrmodule/clrmodule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
<DefineConstants>PYTHON34;UCS2</DefineConstants>
<DefineConstants>PYTHON35,UCS2</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="RGiesecke.DllExport.Metadata">
Expand Down
21 changes: 11 additions & 10 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|x86'">
<OutputPath>bin\x86\ReleaseMono\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS4</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON35, UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
<DefineConstants>PYTHON27,UCS2</DefineConstants>
<DefineConstants>PYTHON35,UCS2</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|x64'">
<OutputPath>bin\x64\ReleaseMono\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS4</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON35, UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
Expand All @@ -34,7 +34,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseWin|x86'">
<OutputPath>bin\x86\ReleaseWin\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS2</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON35, UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
Expand All @@ -44,19 +44,19 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseWin|x64'">
<OutputPath>bin\x64\ReleaseWin\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS2</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON35, UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
<DefineConstants>PYTHON34, UCS2</DefineConstants>
<DefineConstants>PYTHON35,UCS2</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\DebugMono\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON35,UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
Expand All @@ -68,7 +68,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\DebugMono\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON35,UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
Expand All @@ -77,7 +77,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugWin|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\DebugWin\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON35,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
Expand All @@ -89,11 +89,12 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugWin|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\DebugWin\</OutputPath>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON35,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<DefineConstants>TRACE;DEBUG;PYTHON35,UCS2</DefineConstants>
</PropertyGroup>
<Choose>
<When Condition=" '$(Configuration)'=='DebugMono' ">
Expand Down
32 changes: 32 additions & 0 deletions src/testing/constructortests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,36 @@ public SubclassConstructorTest(Exception v)
this.value = v;
}
}
public class ToDoubleConstructorTest
{
public ToDoubleConstructorTest()
{
//Just default values
}
public ToDoubleConstructorTest(string a, double b,string c)
{
this.a = a;
this.b = b;
this.c = c;
}
public string a;
public double b;
public string c;
}
public class ToFloatConstructorTest
{
public ToFloatConstructorTest()
{
// just default values.
}
public ToFloatConstructorTest(string a, float b, string c)
{
this.a = a;
this.b = b;
this.c = c;
}
public string a;
public float b;
public string c;
}
}
18 changes: 18 additions & 0 deletions src/testing/methodtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public Guid TestStructConversion(Guid v)
{
return v;
}
/// <summary>
/// Method to support testing
/// simple promotion of numeric types:
///
/// integer types -> double
///
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public double TestSimpleIntToDoubleTypePromotion(double a, double b)
{
return a + b;
}
public float TestSimpleIntToFloatTypePromotion(float a, float b)
{
return a + b;
}

public Exception TestSubclassConversion(Exception v)
{
Expand Down
21 changes: 20 additions & 1 deletion src/tests/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class sub(System.Exception):

instance = sub()
ob = SubclassConstructorTest(instance)
print(ob)
self.assertTrue(isinstance(ob.value, System.Exception))


def testConstructorArgumentMatching(self):
""" Test that simple type promitions works for int->double """
from Python.Test import AConstrucorTest, LinkConstructorTest
Expand All @@ -60,6 +60,25 @@ def testConstructorArgumentMatching(self):
self.assertEqual(l1.a2.name, a2.name)
self.assertAlmostEqual(3000.0,l1.MatchMe)

def testIntToDoubleConstructorArguments(self):
from Python.Test import ToDoubleConstructorTest

o = ToDoubleConstructorTest('a',2,'c')
self.assertEqual(o.a,'a')
self.assertAlmostEqual(o.b,2)
self.assertEqual(o.c,'c')

o = ToDoubleConstructorTest()

def testIntToFloatConstructorArguments(self):
from Python.Test import ToFloatConstructorTest

o = ToFloatConstructorTest('a',2,'c')
self.assertEqual(o.a,'a')
self.assertAlmostEqual(o.b,2)
self.assertEqual(o.c,'c')

o = ToFloatConstructorTest()

def test_suite():
return unittest.makeSuite(ConstructorTests)
Expand Down
49 changes: 49 additions & 0 deletions src/tests/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,55 @@ def testMethodCallFlagsConversion(self):
r = object.TestFlagsConversion(flags)
self.assertTrue(r == flags)

def testSimpleTypePromotionIntToDouble(self):
object = MethodTest()
try:
sum_of_a_plus_b = object.TestSimpleIntToDoubleTypePromotion(2,2)
self.assertAlmostEqual(sum_of_a_plus_b,2+2)
sum_of_a_plus_b = object.TestSimpleIntToDoubleTypePromotion(2.0,2)
self.assertAlmostEqual(sum_of_a_plus_b,2+2)
sum_of_a_plus_b = object.TestSimpleIntToDoubleTypePromotion(2,2.0)
self.assertAlmostEqual(sum_of_a_plus_b,2+2)
sum_of_a_plus_b = object.TestSimpleIntToDoubleTypePromotion(2.0,2.0)
self.assertAlmostEqual(sum_of_a_plus_b,2.0+2.0)
except:
self.assertTrue(False,"Type promotion from int to double failed")

try:
should_fail = object.TestSimpleIntToDoubleTypePromotion(2,'2.0')
self.assertTrue(False,"the promotion of string to double should fail")
except:
pass
try:
should_fail = object.TestSimpleIntToDoubleTypePromotion(2,True)
self.assertTrue(False,"the promotion of boolean to double should fail")
except:
pass

def testSimpleTypePromotionIntToFloat(self):
object = MethodTest()
try:
sum_of_a_plus_b = object.TestSimpleIntToFloatTypePromotion(2,2)
self.assertAlmostEqual(sum_of_a_plus_b,2+2)
sum_of_a_plus_b = object.TestSimpleIntToFloatTypePromotion(2.0,2)
self.assertAlmostEqual(sum_of_a_plus_b,2+2)
sum_of_a_plus_b = object.TestSimpleIntToFloatTypePromotion(2,2.0)
self.assertAlmostEqual(sum_of_a_plus_b,2+2)
sum_of_a_plus_b = object.TestSimpleIntToFloatTypePromotion(2.0,2.0)
self.assertAlmostEqual(sum_of_a_plus_b,2.0+2.0)
except:
self.assertTrue(False,"Type promotion from int to float failed")
try:
should_fail = object.TestSimpleIntToFloatTypePromotion(2,'2.0')
self.assertTrue(False,"the promotion of string to float should fail")
except:
pass
try:
should_fail = object.TestSimpleIntToFloatTypePromotion(2,True)
self.assertTrue(False,"the promotion of boolean to float should fail")
except:
pass

def testMethodCallStructConversion(self):
"""Test struct conversion in method call."""
from System import Guid
Expand Down