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
This commit is contained in:
Jan Max Meyer 2019-10-10 14:28:25 +02:00 committed by Michael Droettboom
parent 74b4461bda
commit 16c512f272
2 changed files with 7 additions and 2 deletions

View File

@ -23,6 +23,11 @@ _js2python_get_ptr(int obj)
int int
_js2python_number(double val) _js2python_number(double val)
{ {
double i;
if (modf(val, &i) == 0.0)
return (int)PyLong_FromDouble(i);
return (int)PyFloat_FromDouble(val); return (int)PyFloat_FromDouble(val);
} }

View File

@ -224,10 +224,10 @@ def test_js2python(selenium):
'jsstring_ucs4 == "🐍"') 'jsstring_ucs4 == "🐍"')
assert selenium.run( assert selenium.run(
'from js import jsnumber0\n' 'from js import jsnumber0\n'
'jsnumber0 == 42') 'jsnumber0 == 42 and isinstance(jsnumber0, int)')
assert selenium.run( assert selenium.run(
'from js import jsnumber1\n' 'from js import jsnumber1\n'
'jsnumber1 == 42.5') 'jsnumber1 == 42.5 and isinstance(jsnumber1, float)')
assert selenium.run( assert selenium.run(
'from js import jsundefined\n' 'from js import jsundefined\n'
'jsundefined is None') 'jsundefined is None')