From 486a4b28933ce5d484abfd334f3b8c9ddca14e10 Mon Sep 17 00:00:00 2001 From: Rolf Madsen Date: Fri, 28 Oct 2022 14:28:40 +0200 Subject: [PATCH 1/3] 1990 PyInt To String Bug: unit test Added a unit test to show a problem with String.Format / PyInt.ToString() --- src/python_tests_runner/PythonTestRunner.cs | 1 + tests/test_delegate.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/python_tests_runner/PythonTestRunner.cs b/src/python_tests_runner/PythonTestRunner.cs index 05298997b..79f26f74f 100644 --- a/src/python_tests_runner/PythonTestRunner.cs +++ b/src/python_tests_runner/PythonTestRunner.cs @@ -35,6 +35,7 @@ static IEnumerable PythonTestCases() // Add the test that you want to debug here. yield return new[] { "test_indexer", "test_boolean_indexer" }; yield return new[] { "test_delegate", "test_bool_delegate" }; + yield return new[] { "test_delegate", "test_object_string_format" }; } /// diff --git a/tests/test_delegate.py b/tests/test_delegate.py index 55115203c..7b33acdd5 100644 --- a/tests/test_delegate.py +++ b/tests/test_delegate.py @@ -451,3 +451,11 @@ def wrong_return_type(intValue, stringValue): # test sig mismatch, both on managed and Python side # test return wrong type + +def test_object_string_format(): + """Test boolean delegate.""" + from System import String + integer_value = 200 + string_value = String.Format("{0}", integer_value) + assert string_value == "200", f"{string_value} != ""200""" + From 21e2f88f58663f0348d9c6ce2d1d629a4ac787a4 Mon Sep 17 00:00:00 2001 From: Rolf Madsen Date: Fri, 28 Oct 2022 14:03:53 +0200 Subject: [PATCH 2/3] 1990 Fixed bug related to converting number to a string There was an issue with values between 128 and 256, possibly because of some bug inside BigInteger, which I did not fully understand. Anyway, it seems that a simpler solution is to just use LongInteger when possible. --- AUTHORS.md | 1 + src/runtime/PythonTypes/PyInt.cs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/AUTHORS.md b/AUTHORS.md index 92f1a4a97..778640c0d 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -85,3 +85,4 @@ - ([@alxnull](https://github.com/alxnull)) - ([@gpetrou](https://github.com/gpetrou)) - Ehsan Iran-Nejad ([@eirannejad](https://github.com/eirannejad)) +- Rolf Madsen ([@rmadsen-ks](https://github.com/rmadsen-ks)) diff --git a/src/runtime/PythonTypes/PyInt.cs b/src/runtime/PythonTypes/PyInt.cs index 6b3dbf210..278056345 100644 --- a/src/runtime/PythonTypes/PyInt.cs +++ b/src/runtime/PythonTypes/PyInt.cs @@ -228,7 +228,8 @@ public BigInteger ToBigInteger() public string ToString(string format, IFormatProvider formatProvider) { using var _ = Py.GIL(); - return ToBigInteger().ToString(format, formatProvider); + object val = Runtime.PyLong_AsLongLong(obj); + return val?.ToString() ?? ToBigInteger().ToString(format, formatProvider); } public override TypeCode GetTypeCode() => TypeCode.Int64; From c12032f8eda701f333841f6961c9197f80e894da Mon Sep 17 00:00:00 2001 From: Rolf Madsen Date: Fri, 28 Oct 2022 15:14:58 +0200 Subject: [PATCH 3/3] Moved test_object_string_format to a more fitting location. --- src/python_tests_runner/PythonTestRunner.cs | 2 +- tests/test_conversion.py | 6 ++++++ tests/test_delegate.py | 7 +------ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/python_tests_runner/PythonTestRunner.cs b/src/python_tests_runner/PythonTestRunner.cs index 79f26f74f..920219b72 100644 --- a/src/python_tests_runner/PythonTestRunner.cs +++ b/src/python_tests_runner/PythonTestRunner.cs @@ -35,7 +35,7 @@ static IEnumerable PythonTestCases() // Add the test that you want to debug here. yield return new[] { "test_indexer", "test_boolean_indexer" }; yield return new[] { "test_delegate", "test_bool_delegate" }; - yield return new[] { "test_delegate", "test_object_string_format" }; + yield return new[] { "test_conversion", "test_object_string_format" }; } /// diff --git a/tests/test_conversion.py b/tests/test_conversion.py index bb686dd52..2eebbe7f8 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -752,3 +752,9 @@ def test_explicit_conversion(): assert int(t(123.4)) == 123 with pytest.raises(TypeError): index(t(123.4)) + +def test_object_string_format(): + from System import String + integer_value = 200 + string_value = String.Format("{0}", integer_value) + assert string_value == "200", f"{string_value} != ""200""" diff --git a/tests/test_delegate.py b/tests/test_delegate.py index 7b33acdd5..7e2f27fed 100644 --- a/tests/test_delegate.py +++ b/tests/test_delegate.py @@ -452,10 +452,5 @@ def wrong_return_type(intValue, stringValue): # test return wrong type -def test_object_string_format(): - """Test boolean delegate.""" - from System import String - integer_value = 200 - string_value = String.Format("{0}", integer_value) - assert string_value == "200", f"{string_value} != ""200""" + pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy