From 16c512f272cebaf91aba60e30c65bcf5d0ed8b23 Mon Sep 17 00:00:00 2001 From: Jan Max Meyer Date: Thu, 10 Oct 2019 14:28:25 +0200 Subject: [PATCH] Convert integer-only doubles to PyLong (#512) * Convert integer-only doubles to PyLong * Using PyLong_FromDouble directly, improved tests * This test should to it as well --- src/js2python.c | 5 +++++ test/test_python.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/js2python.c b/src/js2python.c index bb81a0f67..2788a8b0c 100644 --- a/src/js2python.c +++ b/src/js2python.c @@ -23,6 +23,11 @@ _js2python_get_ptr(int obj) int _js2python_number(double val) { + double i; + + if (modf(val, &i) == 0.0) + return (int)PyLong_FromDouble(i); + return (int)PyFloat_FromDouble(val); } diff --git a/test/test_python.py b/test/test_python.py index ac951ffd9..83ce6d596 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -224,10 +224,10 @@ def test_js2python(selenium): 'jsstring_ucs4 == "🐍"') assert selenium.run( 'from js import jsnumber0\n' - 'jsnumber0 == 42') + 'jsnumber0 == 42 and isinstance(jsnumber0, int)') assert selenium.run( 'from js import jsnumber1\n' - 'jsnumber1 == 42.5') + 'jsnumber1 == 42.5 and isinstance(jsnumber1, float)') assert selenium.run( 'from js import jsundefined\n' 'jsundefined is None')