Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix conversion of 'float' and 'double' values #487

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 4 commits into from
Jun 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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
- John Burnett ([@johnburnett](https://github.com/johnburnett))
- Luke Stratman ([@lstratman](https://github.com/lstratman))
- Konstantin Posudevskiy ([@konstantin-posudevskiy](https://github.com/konstantin-posudevskiy))
- Matthias Dittrich ([@matthid](https://github.com/matthid))
- Patrick Stewart ([@patstew](https://github.com/patstew))
- Raphael Nestler ([@rnestler](https://github.com/rnestler))
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Fixed crash on exit of the Python interpreter if a python class
derived from a .NET class has a `__namespace__` or `__assembly__`
attribute (#481)
- Fixed conversion of 'float' and 'double' values (#486)


## [2.3.0][] - 2017-03-11

Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="pyimport.cs" />
<Compile Include="pyinitialize.cs" />
<Compile Include="pyrunstring.cs" />
<Compile Include="TestConverter.cs" />
<Compile Include="TestCustomMarshal.cs" />
<Compile Include="TestExample.cs" />
<Compile Include="TestPyAnsiString.cs" />
Expand Down
48 changes: 48 additions & 0 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestConverter
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void TestConvertSingleToManaged(
[Values(float.PositiveInfinity, float.NegativeInfinity, float.MinValue, float.MaxValue, float.NaN,
float.Epsilon)] float testValue)
{
var pyFloat = new PyFloat(testValue);

object convertedValue;
var converted = Converter.ToManaged(pyFloat.Handle, typeof(float), out convertedValue, false);

Assert.IsTrue(converted);
Assert.IsTrue(((float) convertedValue).Equals(testValue));
}

[Test]
public void TestConvertDoubleToManaged(
[Values(double.PositiveInfinity, double.NegativeInfinity, double.MinValue, double.MaxValue, double.NaN,
double.Epsilon)] double testValue)
{
var pyFloat = new PyFloat(testValue);

object convertedValue;
var converted = Converter.ToManaged(pyFloat.Handle, typeof(double), out convertedValue, false);

Assert.IsTrue(converted);
Assert.IsTrue(((double) convertedValue).Equals(testValue));
}
}
}
11 changes: 6 additions & 5 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,14 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
goto type_error;
}
double dd = Runtime.PyFloat_AsDouble(op);
Runtime.CheckExceptionOccurred();
Runtime.XDecref(op);
if (dd > Single.MaxValue || dd < Single.MinValue)
{
goto overflow;
if (!double.IsInfinity(dd))
{
goto overflow;
}
}
result = (float)dd;
return true;
Expand All @@ -785,11 +789,8 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
goto type_error;
}
double d = Runtime.PyFloat_AsDouble(op);
Runtime.CheckExceptionOccurred();
Runtime.XDecref(op);
if (d > Double.MaxValue || d < Double.MinValue)
{
goto overflow;
}
result = d;
return true;
}
Expand Down
11 changes: 0 additions & 11 deletions src/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,6 @@ def test_double_conversion():
with pytest.raises(TypeError):
ConversionTest().DoubleField = None

with pytest.raises(OverflowError):
ConversionTest().DoubleField = 1.7976931348623159e308

with pytest.raises(OverflowError):
ConversionTest().DoubleField = -1.7976931348623159e308

with pytest.raises(OverflowError):
_ = System.Double(1.7976931348623159e308)

with pytest.raises(OverflowError):
_ = System.Double(-1.7976931348623159e308)


def test_decimal_conversion():
Expand Down