PyScript list comprehensions don't work for Float64Array
almarklein opened this issue · 0 comments
@nathanielatom commented on Sat Aug 26 2017
Firstly, I'd like to say thank you because PyScript is a wonderful package and has helped boost my productivity quite often.
Observed behaviour:
import flexx.pyscript as ps
code = '''
def foo():
"""
return new Float64Array([3.14, 0, -1, 0.57])
"""
def bar(arr):
return [elem for elem in arr]
bar([3.14, 0, -1, 0.57]), bar(foo()), foo()
'''
ps.evalpy(code)
# "[ [ 3.14, 0, -1, 0.57 ],\n [ '0', '1', '2', '3' ],\n Float64Array [ 3.14, 0, -1, 0.57 ] ]"
Expected behaviour would be for the second call to match the third.
Coming from a python background, I wasn't aware of a distinction between Array
and Float64Array
, so this caught me off guard. For a workaround, I casted the Float64Array
to an Array
using [].slice.call(float_arr)
before the list comprehension.
Python 3.6
Flexx 0.4.1
@almarklein commented on Tue Sep 05 2017
Thanks for reporting this. It is known behavior, at least by me :) but I can see how it can be unexpected. This is part of #332, I think.
@jburgy commented on Wed Feb 28 2018
FWIW, here's a slightly different broke-around:
from flexx.pyscript import evaljs, py2js
from re import sub
pycode = '''
def foo():
"""
return new Float64Array([3.14, 0, -1, 0.57])
"""
def bar(arr):
return [elem for elem in arr]
bar([3.14, 0, -1, 0.57]), bar(foo()), foo()
'''
jscode = sub(r'Array.isArray\(([^)]+)\)', r'(Array.isArray(\1) || ArrayBuffer.isView(\1))', py2js(pycode))
evaljs(jscode)
# "[ [ 3.14, 0, -1, 0.57 ],\n. [ 3.14, 0, -1, 0.57 ],\n Float64Array [ 3.14, 0, -1, 0.57 ] ]"