Merge pull request #322 from mdboom/issue319

Handle numpy scalars in python2js_buffer
This commit is contained in:
Michael Droettboom 2019-02-21 14:59:13 -05:00 committed by GitHub
commit 1c4362203c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -427,6 +427,10 @@ _python2js_shareable_buffer_recursive(Py_buffer* buff,
static enum shareable_enum
_python2js_buffer_is_shareable(Py_buffer* buff)
{
if (buff->ndim == 0) {
return NOT_SHAREABLE;
}
char* invalid_codes = ">!qQ?";
for (char* i = buff->format; *i != 0; ++i) {
for (char* j = invalid_codes; *j != 0; ++j) {

View File

@ -146,6 +146,46 @@ def test_python2js_numpy_dtype(selenium_standalone):
assert selenium.run_js("return pyodide.pyimport('x')[1][1]") == 'string4'
def test_python2js_numpy_scalar(selenium_standalone):
selenium = selenium_standalone
selenium.load_package('numpy')
selenium.run("import numpy as np")
for dtype in (
'int8',
'uint8',
'int16',
'uint16',
'int32',
'uint32',
'int64',
'uint64',
'float32',
'float64'
):
selenium.run(
f"""
x = np.{dtype}(1)
"""
)
assert selenium.run_js(
"""
return pyodide.pyimport('x') == 1
"""
) is True
selenium.run(
"""
x = x.byteswap().newbyteorder()
"""
)
assert selenium.run_js(
"""
return pyodide.pyimport('x') == 1
"""
) is True
def test_pythonexc2js(selenium):
try:
selenium.run_js('return pyodide.runPython("5 / 0")')