Skip to content

Commit

Permalink
MAINT: Update conftest to check PyErr_Occurred (pyodide#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hood Chatham authored Jan 13, 2021
1 parent e4c4702 commit 816aaa0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
32 changes: 27 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ def run_js(self, code):
Error.stackTraceLimit = Infinity;
let run = () => { %s }
try {
return [0, run()]
let result = run();
if(pyodide && pyodide._module && pyodide._module._PyErr_Occurred()){
try {
pyodide._module._pythonexc2js();
} catch(e){
console.error(`Python exited with error flag set! Error was:\n{e.message}`);
// Don't put original error message in new one: we want
// "pytest.raises(xxx, match=msg)" to fail
throw new Error(`Python exited with error flag set!`);
}
}
return [0, result]
} catch (e) {
return [1, e.toString(), e.stack];
}
Expand All @@ -130,11 +141,22 @@ def run_js_async(self, code):
let cb = arguments[arguments.length - 1];
let run = async () => { %s }
(async () => {
try {{
cb([0, await run()]);
}} catch (e) {{
try {
let result = await run();
if(pyodide && pyodide._module && pyodide._module._PyErr_Occurred()){
try {
pyodide._module._pythonexc2js();
} catch(e){
console.error(`Python exited with error flag set! Error was:\n{e.message}`);
// Don't put original error message in new one: we want
// "pytest.raises(xxx, match=msg)" to fail
throw new Error(`Python exited with error flag set!`);
}
}
cb([0, result]);
} catch (e) {
cb([1, e.toString(), e.stack]);
}}
}
})()
"""

Expand Down
14 changes: 6 additions & 8 deletions packages/numpy/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,12 @@ def test_py2js_buffer_clear_error_flag(selenium):
selenium.load_package("numpy")
selenium.run("import numpy as np")
selenium.run("x = np.array([['string1', 'string2'], ['string3', 'string4']])")
assert (
selenium.run_js(
"""
pyodide.globals.x
return pyodide._module._PyErr_Occurred();
"""
)
== 0
selenium.run_js(
"""
pyodide.globals.x
// Implicit assertion: this doesn't leave python error indicator set
// (automatically checked in conftest.py)
"""
)


Expand Down
14 changes: 11 additions & 3 deletions src/tests/test_typeconversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,21 @@ def test_memoryview_conversion(selenium):
"""
import array
a = array.array("Q", [1,2,3])
b = array.array("u", "123")
"""
)
selenium.run_js(
"""
pyodide.globals.a
if(pyodide._module._PyErr_Occurred()){
pyodide._module._pythonexc2js();
}
// Implicit assertion: this doesn't leave python error indicator set
// (automatically checked in conftest.py)
"""
)

selenium.run_js(
"""
pyodide.globals.b
// Implicit assertion: this doesn't leave python error indicator set
// (automatically checked in conftest.py)
"""
)

0 comments on commit 816aaa0

Please sign in to comment.