From 7c097fa4c2562907db3e05768fd022a69c759cb5 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 21 Feb 2019 13:59:45 -0500 Subject: [PATCH] Handle numpy scalars in python2js_buffer --- src/python2js_buffer.c | 4 ++++ test/test_python.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/python2js_buffer.c b/src/python2js_buffer.c index cbb5454d5..a899f00f9 100644 --- a/src/python2js_buffer.c +++ b/src/python2js_buffer.c @@ -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) { diff --git a/test/test_python.py b/test/test_python.py index 3c31d7075..b246002be 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -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")')