/
test_pyodide.py
574 lines (504 loc) · 16.5 KB
/
test_pyodide.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import pytest
from pathlib import Path
import sys
from textwrap import dedent
sys.path.append(str(Path(__file__).resolve().parents[2] / "src" / "py"))
from pyodide import find_imports, eval_code, CodeRunner, should_quiet # noqa: E402
def test_find_imports():
res = find_imports(
dedent(
"""
import numpy as np
from scipy import sparse
import matplotlib.pyplot as plt
"""
)
)
assert set(res) == {"numpy", "scipy", "matplotlib"}
def test_code_runner():
assert should_quiet("1+1;")
assert not should_quiet("1+1#;")
assert not should_quiet("5-2 # comment with trailing semicolon ;")
# Normal usage
assert CodeRunner("1+1").compile().run() == 2
assert CodeRunner("1+1\n1+1").compile().run() == 2
assert CodeRunner("x + 7").compile().run({"x": 3}) == 10
cr = CodeRunner("x + 7")
# Ast transform
import ast
l = cr.ast.body[0].value.left
cr.ast.body[0].value.left = ast.BinOp(
left=l, op=ast.Mult(), right=ast.Constant(value=2)
)
assert cr.compile().run({"x": 3}) == 13
# Code transform
cr.code = cr.code.replace(co_consts=(0, 3, 5, None))
assert cr.run({"x": 4}) == 17
def test_code_runner_mode():
from codeop import PyCF_DONT_IMPLY_DEDENT
assert CodeRunner("1+1\n1+1", mode="exec").compile().run() == 2
with pytest.raises(SyntaxError, match="invalid syntax"):
CodeRunner("1+1\n1+1", mode="eval").compile().run()
with pytest.raises(
SyntaxError,
match="multiple statements found while compiling a single statement",
):
CodeRunner("1+1\n1+1", mode="single").compile().run()
with pytest.raises(SyntaxError, match="unexpected EOF while parsing"):
CodeRunner(
"def f():\n 1", mode="single", flags=PyCF_DONT_IMPLY_DEDENT
).compile().run()
def test_eval_code():
ns = {}
assert (
eval_code(
"""
def f(x):
return 2*x + 5
f(77)
""",
ns,
)
== 2 * 77 + 5
)
assert ns["f"](7) == 2 * 7 + 5
assert eval_code("(x:=4)", ns) == 4
assert ns["x"] == 4
assert eval_code("x=7", ns) is None
assert ns["x"] == 7
# default return_mode ('last_expr'), semicolon
assert eval_code("1+1;", ns) is None
assert eval_code("1+1#;", ns) == 2
assert eval_code("5-2 # comment with trailing semicolon ;", ns) == 3
assert eval_code("4//2\n", ns) == 2
assert eval_code("2**1\n\n", ns) == 2
assert eval_code("4//2;\n", ns) is None
assert eval_code("2**1;\n\n", ns) is None
# 'last_expr_or_assign' return_mode, semicolon
assert eval_code("1 + 1", ns, return_mode="last_expr_or_assign") == 2
assert eval_code("x = 1 + 1", ns, return_mode="last_expr_or_assign") == 2
assert eval_code("a = 5 ; a += 1", ns, return_mode="last_expr_or_assign") == 6
assert eval_code("a = 5 ; a += 1;", ns, return_mode="last_expr_or_assign") is None
assert (
eval_code("l = [1, 1, 2] ; l[0] = 0", ns, return_mode="last_expr_or_assign")
is None
)
assert eval_code("a = b = 2", ns, return_mode="last_expr_or_assign") == 2
# 'none' return_mode, (useless) semicolon
assert eval_code("1 + 1", ns, return_mode="none") is None
assert eval_code("x = 1 + 1", ns, return_mode="none") is None
assert eval_code("a = 5 ; a += 1", ns, return_mode="none") is None
assert eval_code("a = 5 ; a += 1;", ns, return_mode="none") is None
assert eval_code("l = [1, 1, 2] ; l[0] = 0", ns, return_mode="none") is None
# with 'quiet_trailing_semicolon' set to False
assert eval_code("1+1;", ns, quiet_trailing_semicolon=False) == 2
assert eval_code("1+1#;", ns, quiet_trailing_semicolon=False) == 2
assert (
eval_code(
"5-2 # comment with trailing semicolon ;",
ns,
quiet_trailing_semicolon=False,
)
== 3
)
assert eval_code("4//2\n", ns, quiet_trailing_semicolon=False) == 2
assert eval_code("2**1\n\n", ns, quiet_trailing_semicolon=False) == 2
assert eval_code("4//2;\n", ns, quiet_trailing_semicolon=False) == 2
assert eval_code("2**1;\n\n", ns, quiet_trailing_semicolon=False) == 2
def test_eval_code_locals():
globals = {}
eval_code("x=2", globals, {})
with pytest.raises(NameError):
eval_code("x", globals, {})
locals = {}
eval_code("import sys; sys.getrecursionlimit()", globals, locals)
with pytest.raises(NameError):
eval_code("sys.getrecursionlimit()", globals, {})
eval_code("sys.getrecursionlimit()", globals, locals)
eval_code(
"from importlib import invalidate_caches; invalidate_caches()", globals, locals
)
with pytest.raises(NameError):
eval_code("invalidate_caches()", globals, globals)
eval_code("invalidate_caches()", globals, locals)
@pytest.mark.skip_pyproxy_check
def test_monkeypatch_eval_code(selenium):
try:
selenium.run(
"""
import pyodide
old_eval_code = pyodide.eval_code
x = 3
def eval_code(code, ns):
return [ns["x"], old_eval_code(code, ns)]
pyodide.eval_code = eval_code
"""
)
assert selenium.run("x = 99; 5") == [3, 5]
assert selenium.run("7") == [99, 7]
finally:
selenium.run(
"""
pyodide.eval_code = old_eval_code
"""
)
def test_hiwire_is_promise(selenium):
for s in [
"0",
"1",
"'x'",
"''",
"false",
"undefined",
"null",
"NaN",
"0n",
"[0,1,2]",
"[]",
"{}",
"{a : 2}",
"(()=>{})",
"((x) => x*x)",
"(function(x, y){ return x*x + y*y; })",
"Array",
"Map",
"Set",
"Promise",
"new Array()",
"new Map()",
"new Set()",
]:
assert selenium.run_js(
f"return pyodide._module.hiwire.isPromise({s}) === false;"
)
if not selenium.browser == "node":
assert selenium.run_js(
f"return pyodide._module.hiwire.isPromise(document.all) === false;"
)
assert selenium.run_js(
"return pyodide._module.hiwire.isPromise(Promise.resolve()) === true;"
)
assert selenium.run_js(
"""
return pyodide._module.hiwire.isPromise(new Promise((resolve, reject) => {}));
"""
)
assert not selenium.run_js(
"""
let d = pyodide.runPython("{}");
try {
return pyodide._module.hiwire.isPromise(d);
} finally {
d.destroy();
}
"""
)
def test_keyboard_interrupt(selenium):
x = selenium.run_js(
"""
x = new Int8Array(1)
pyodide._module.setInterruptBuffer(x)
self.triggerKeyboardInterrupt = function(){
x[0] = 2;
}
try {
pyodide.runPython(`
from js import triggerKeyboardInterrupt
x = 0
while True:
x += 1
if x == 2000:
triggerKeyboardInterrupt()
`)
} catch(e){}
return pyodide.runPython('x')
"""
)
assert 2000 < x < 2500
def test_run_python_async_toplevel_await(selenium):
selenium.run_js(
"""
await pyodide.runPythonAsync(`
from js import fetch
resp = await fetch("packages.json")
json = (await resp.json()).to_py()["packages"]
assert "micropip" in json
`);
"""
)
def test_run_python_proxy_leak(selenium):
selenium.run_js(
"""
pyodide.runPython("")
await pyodide.runPythonAsync("")
"""
)
def test_run_python_last_exc(selenium):
selenium.run_js(
"""
try {
pyodide.runPython("x = ValueError(77); raise x");
} catch(e){}
pyodide.runPython(`
import sys
assert sys.last_value is x
assert sys.last_type is type(x)
assert sys.last_traceback is x.__traceback__
`);
"""
)
def test_async_leak(selenium):
assert 0 == selenium.run_js(
"""
pyodide.runPython(`d = 888.888`);
pyodide.runPython(`async def test(): return d`);
async function test(){
let t = pyodide.runPython(`test()`);
await t;
t.destroy();
}
await test();
let init_refcount = pyodide.runPython(`from sys import getrefcount; getrefcount(d)`);
await test(); await test(); await test(); await test();
let new_refcount = pyodide.runPython(`getrefcount(d)`);
return new_refcount - init_refcount;
"""
)
def test_run_python_js_error(selenium):
selenium.run_js(
"""
function throwError(){
throw new Error("blah!");
}
self.throwError = throwError;
pyodide.runPython(`
from js import throwError
from unittest import TestCase
from pyodide import JsException
raises = TestCase().assertRaisesRegex
with raises(JsException, "blah!"):
throwError()
`);
"""
)
def test_create_once_callable(selenium):
selenium.run_js(
"""
self.call7 = function call7(f){
return f(7);
}
pyodide.runPython(`
from pyodide import create_once_callable, JsException
from js import call7;
from unittest import TestCase
raises = TestCase().assertRaisesRegex
class Square:
def __call__(self, x):
return x*x
def __del__(self):
global destroyed
destroyed = True
f = Square()
import sys
assert sys.getrefcount(f) == 2
proxy = create_once_callable(f)
assert sys.getrefcount(f) == 3
assert call7(proxy) == 49
assert sys.getrefcount(f) == 2
with raises(JsException, "can only be called once"):
call7(proxy)
destroyed = False
del f
assert destroyed == True
del proxy
`);
"""
)
def test_create_proxy(selenium):
selenium.run_js(
"""
self.testAddListener = function(f){
self.listener = f;
}
self.testCallListener = function(f){
return self.listener();
}
self.testRemoveListener = function(f){
return self.listener === f;
}
pyodide.runPython(`
from pyodide import create_proxy
from js import testAddListener, testCallListener, testRemoveListener;
class Test:
def __call__(self):
return 7
def __del__(self):
global destroyed
destroyed = True
f = Test()
import sys
assert sys.getrefcount(f) == 2
proxy = create_proxy(f)
assert sys.getrefcount(f) == 3
assert proxy() == 7
testAddListener(proxy)
assert sys.getrefcount(f) == 3
assert testCallListener() == 7
assert sys.getrefcount(f) == 3
assert testCallListener() == 7
assert sys.getrefcount(f) == 3
assert testRemoveListener(proxy)
assert sys.getrefcount(f) == 3
proxy.destroy()
assert sys.getrefcount(f) == 2
destroyed = False
del f
assert destroyed == True
`);
"""
)
def test_docstrings_a():
from _pyodide.docstring import get_cmeth_docstring, dedent_docstring
from pyodide import JsProxy
jsproxy = JsProxy()
c_docstring = get_cmeth_docstring(jsproxy.then)
assert c_docstring == "then(onfulfilled, onrejected)\n--\n\n" + dedent_docstring(
jsproxy.then.__doc__
)
def test_docstrings_b(selenium):
from pyodide import create_once_callable, JsProxy
from _pyodide.docstring import dedent_docstring
jsproxy = JsProxy()
ds_then_should_equal = dedent_docstring(jsproxy.then.__doc__)
sig_then_should_equal = "(onfulfilled, onrejected)"
ds_once_should_equal = dedent_docstring(create_once_callable.__doc__)
sig_once_should_equal = "(obj)"
selenium.run_js("self.a = Promise.resolve();")
[ds_then, sig_then, ds_once, sig_once] = selenium.run(
"""
from js import a
from pyodide import create_once_callable as b
[
a.then.__doc__, a.then.__text_signature__,
b.__doc__, b.__text_signature__
]
"""
)
assert ds_then == ds_then_should_equal
assert sig_then == sig_then_should_equal
assert ds_once == ds_once_should_equal
assert sig_once == sig_once_should_equal
@pytest.mark.skip_refcount_check
@pytest.mark.skip_pyproxy_check
def test_restore_state(selenium):
selenium.run_js(
"""
pyodide.registerJsModule("a", {somefield : 82});
pyodide.registerJsModule("b", { otherfield : 3 });
pyodide.runPython("x = 7; from a import somefield");
let state = pyodide._module.saveState();
pyodide.registerJsModule("c", { thirdfield : 9 });
pyodide.runPython("y = 77; from b import otherfield; import c;");
pyodide._module.restoreState(state);
state.destroy();
"""
)
selenium.run(
"""
from unittest import TestCase
raises = TestCase().assertRaises
import sys
assert x == 7
assert "a" in sys.modules
assert somefield == 82
with raises(NameError):
y
with raises(NameError):
otherfield
assert "b" not in sys.modules
import b
with raises(ModuleNotFoundError):
import c
"""
)
@pytest.mark.skip_refcount_check
def test_fatal_error(selenium_standalone):
assert selenium_standalone.run_js(
"""
try {
pyodide.runPython(`
from _pyodide_core import trigger_fatal_error
def f():
g()
def g():
h()
def h():
trigger_fatal_error()
f()
`);
} catch(e){
return e.toString();
}
"""
)
import re
def strip_stack_trace(x):
x = re.sub("\n.*site-packages.*", "", x)
x = re.sub("/lib/python.*/", "", x)
x = re.sub("/lib/python.*/", "", x)
x = re.sub("warning: no [bB]lob.*\n", "", x)
x = re.sub("Error: intentionally triggered fatal error!\n", "", x)
x = re.sub(" +at .*\n", "", x)
x = re.sub(".*@https?://[0-9.:]*/.*\n", "", x)
x = x.replace("\n\n", "\n")
return x
assert (
strip_stack_trace(selenium_standalone.logs)
== dedent(
strip_stack_trace(
"""
Python initialization complete
Pyodide has suffered a fatal error. Please report this to the Pyodide maintainers.
The cause of the fatal error was:
Stack (most recent call first):
File "<exec>", line 8 in h
File "<exec>", line 6 in g
File "<exec>", line 4 in f
File "<exec>", line 9 in <module>
File "/lib/pythonxxx/site-packages/pyodide/_base.py", line 242 in run
File "/lib/pythonxxx/site-packages/pyodide/_base.py", line 344 in eval_code
"""
)
).strip()
)
selenium_standalone.run_js(
"""
assertThrows(() => pyodide.runPython, "Error", "Pyodide already fatally failed and can no longer be used.")
assertThrows(() => pyodide.globals, "Error", "Pyodide already fatally failed and can no longer be used.")
assert(() => pyodide._module.runPython("1+1") === 2);
"""
)
def test_reentrant_error(selenium):
caught = selenium.run_js(
"""
function a(){
pyodide.globals.get("pyfunc")();
}
let caught = false;
try {
pyodide.runPython(`
def pyfunc():
raise KeyboardInterrupt
from js import a
try:
a()
except Exception as e:
pass
`);
} catch(e){
caught = true;
}
return caught;
"""
)
assert caught