From 0952ebe91c2d5a64b2d492253c60767f01e07171 Mon Sep 17 00:00:00 2001 From: Konstantin Posudevskiy Date: Mon, 12 Jun 2017 14:47:52 +0300 Subject: [PATCH 1/2] Fix conversion of 'float' and 'double' values Fix problem of conversion 'float' and 'double' values in converter.cs. As there was a range check both for 'float' and 'double' values, which are less or greater than its 'MinValue' and 'MaxValue' accordingly, several values like 'float.NegativeInfinity', 'float.PositiveInfinity' and the same 'double' values cannot be converted from Python to .NET values. Add error check after 'PyFloat_AsDouble' call. Due to Python C API documentation, method 'PyFloat_AsDouble' can return '-1.0' upon failure. So it requires error check. This rule forces to check for error and throw exception in case of error. Add tests, which cover problem of conversion 'float' and 'double' values. Resolves: #486. --- AUTHORS.md | 1 + CHANGELOG.md | 1 + src/embed_tests/Python.EmbeddingTest.csproj | 1 + src/embed_tests/TestConverter.cs | 48 +++++++++++++++++++++ src/runtime/converter.cs | 11 ++--- 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/embed_tests/TestConverter.cs diff --git a/AUTHORS.md b/AUTHORS.md index d715eed6f..09fc47525 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -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)) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8b54b36d..d9d59c3a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - Fixed Visual Studio 2017 compat (#434) for setup.py - Fixed `FooBar` bug +- Fixed conversion of 'float' and 'double' values (#486) ## [2.3.0][] - 2017-03-11 diff --git a/src/embed_tests/Python.EmbeddingTest.csproj b/src/embed_tests/Python.EmbeddingTest.csproj index 2edf4f515..1f6013b88 100644 --- a/src/embed_tests/Python.EmbeddingTest.csproj +++ b/src/embed_tests/Python.EmbeddingTest.csproj @@ -84,6 +84,7 @@ + diff --git a/src/embed_tests/TestConverter.cs b/src/embed_tests/TestConverter.cs new file mode 100644 index 000000000..346c8afdc --- /dev/null +++ b/src/embed_tests/TestConverter.cs @@ -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)); + } + } +} diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index 0bf882cc5..5179c849b 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -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; @@ -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; } From 71651fb2e8673c495024146269adb5a4e074d278 Mon Sep 17 00:00:00 2001 From: Konstantin Posudevskiy Date: Tue, 13 Jun 2017 19:00:54 +0300 Subject: [PATCH 2/2] Fix failing 'test_double_conversion' test Fix incorrect part of 'test_double_conversion' test in test_conversion.py. An 'OverflowError' was expected for valid values, which represent Python 'inf' and '-inf'. The problem was identified with support of conversion for Python 'inf' and '-inf' to .NET System.Double PositiveInfinity and NegativeInfinity. See also: #487. --- src/tests/test_conversion.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/tests/test_conversion.py b/src/tests/test_conversion.py index 9152e30d0..53e5d8051 100644 --- a/src/tests/test_conversion.py +++ b/src/tests/test_conversion.py @@ -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():