forked from pyodide/pyodide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_typeconversions.py
309 lines (273 loc) · 8.91 KB
/
test_typeconversions.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
# See also test_pyproxy, test_jsproxy, and test_python.
import pytest
from selenium.common.exceptions import WebDriverException
def test_python2js(selenium):
assert selenium.run_js('return pyodide.runPython("None") === undefined')
assert selenium.run_js('return pyodide.runPython("True") === true')
assert selenium.run_js('return pyodide.runPython("False") === false')
assert selenium.run_js('return pyodide.runPython("42") === 42')
assert selenium.run_js('return pyodide.runPython("3.14") === 3.14')
# Need to test all three internal string representations in Python: UCS1,
# UCS2 and UCS4
assert selenium.run_js('return pyodide.runPython("\'ascii\'") === "ascii"')
assert selenium.run_js('return pyodide.runPython("\'ιωδιούχο\'") === "ιωδιούχο"')
assert selenium.run_js('return pyodide.runPython("\'碘化物\'") === "碘化物"')
assert selenium.run_js('return pyodide.runPython("\'🐍\'") === "🐍"')
assert selenium.run_js(
"let x = pyodide.runPython(\"b'bytes'\");\n"
"return (x instanceof window.Uint8ClampedArray) && "
"(x.length === 5) && "
"(x[0] === 98)"
)
assert selenium.run_js(
"""
let x = pyodide.runPython("[1, 2, 3]");
return ((x instanceof window.Array) && (x.length === 3) &&
(x[0] == 1) && (x[1] == 2) && (x[2] == 3))
"""
)
assert selenium.run_js(
"""
let x = pyodide.runPython("{42: 64}");
return (typeof x === "object") && (x[42] === 64)
"""
)
assert selenium.run_js(
"""
let x = pyodide.runPython("open('/foo.txt', 'wb')")
return (x.tell() === 0)
"""
)
def test_python2js_long_ints(selenium):
assert selenium.run("2**30") == 2 ** 30
assert selenium.run("2**31") == 2 ** 31
assert selenium.run("2**30 - 1 + 2**30") == (2 ** 30 - 1 + 2 ** 30)
assert selenium.run("2**32 / 2**4") == (2 ** 32 / 2 ** 4)
assert selenium.run("-2**30") == -(2 ** 30)
assert selenium.run("-2**31") == -(2 ** 31)
def test_pythonexc2js(selenium):
msg = "ZeroDivisionError"
with pytest.raises(selenium.JavascriptException, match=msg):
selenium.run_js('return pyodide.runPython("5 / 0")')
def test_js2python(selenium):
selenium.run_js(
"""
window.jsstring_ucs1 = "pyodidé";
window.jsstring_ucs2 = "碘化物";
window.jsstring_ucs4 = "🐍";
window.jsnumber0 = 42;
window.jsnumber1 = 42.5;
window.jsundefined = undefined;
window.jsnull = null;
window.jstrue = true;
window.jsfalse = false;
window.jsarray0 = [];
window.jsarray1 = [1, 2, 3];
window.jspython = pyodide.pyimport("open");
window.jsbytes = new Uint8Array([1, 2, 3]);
window.jsfloats = new Float32Array([1, 2, 3]);
window.jsobject = new XMLHttpRequest();
"""
)
assert selenium.run("from js import jsstring_ucs1\n" 'jsstring_ucs1 == "pyodidé"')
assert selenium.run("from js import jsstring_ucs2\n" 'jsstring_ucs2 == "碘化物"')
assert selenium.run("from js import jsstring_ucs4\n" 'jsstring_ucs4 == "🐍"')
assert selenium.run(
"from js import jsnumber0\n" "jsnumber0 == 42 and isinstance(jsnumber0, int)"
)
assert selenium.run(
"from js import jsnumber1\n"
"jsnumber1 == 42.5 and isinstance(jsnumber1, float)"
)
assert selenium.run("from js import jsundefined\n" "jsundefined is None")
assert selenium.run("from js import jstrue\n" "jstrue is True")
assert selenium.run("from js import jsfalse\n" "jsfalse is False")
assert selenium.run("from js import jspython\n" "jspython is open")
assert selenium.run(
"""
from js import jsbytes
((jsbytes.tolist() == [1, 2, 3])
and (jsbytes.tobytes() == b"\x01\x02\x03"))
"""
)
assert selenium.run(
"""
from js import jsfloats
import struct
expected = struct.pack("fff", 1, 2, 3)
(jsfloats.tolist() == [1, 2, 3]) and (jsfloats.tobytes() == expected)
"""
)
assert selenium.run(
"from js import jsobject\n" 'str(jsobject) == "[object XMLHttpRequest]"'
)
assert selenium.run(
"""
from js import jsobject
bool(jsobject) == True
"""
)
assert selenium.run(
"""
from js import jsarray0
bool(jsarray0) == False
"""
)
assert selenium.run(
"""
from js import jsarray1
bool(jsarray1) == True
"""
)
@pytest.mark.parametrize("wasm_heap", (False, True))
@pytest.mark.parametrize(
"jstype, pytype",
(
("Int8Array", "b"),
("Uint8Array", "B"),
("Uint8ClampedArray", "B"),
("Int16Array", "h"),
("Uint16Array", "H"),
("Int32Array", "i"),
("Uint32Array", "I"),
("Float32Array", "f"),
("Float64Array", "d"),
),
)
def test_typed_arrays(selenium, wasm_heap, jstype, pytype):
if not wasm_heap:
selenium.run_js(f"window.array = new {jstype}([1, 2, 3, 4]);\n")
else:
selenium.run_js(
f"""
var buffer = pyodide._module._malloc(
4 * {jstype}.BYTES_PER_ELEMENT);
window.array = new {jstype}(
pyodide._module.HEAPU8.buffer, buffer, 4);
window.array[0] = 1;
window.array[1] = 2;
window.array[2] = 3;
window.array[3] = 4;
"""
)
assert selenium.run(
f"""
from js import array
import struct
expected = struct.pack("{pytype*4}", 1, 2, 3, 4)
print(array.format, array.tolist(), array.tobytes())
((array.format == "{pytype}")
and array.tolist() == [1, 2, 3, 4]
and array.tobytes() == expected
and array.obj._has_bytes() is {not wasm_heap})
"""
)
def test_array_buffer(selenium):
selenium.run_js("window.array = new ArrayBuffer(100);\n")
assert (
selenium.run(
"""
from js import array
len(array.tobytes())
"""
)
== 100
)
def assert_js_to_py_to_js(selenium, name):
selenium.run_js(f"window.obj = {name};")
selenium.run("from js import obj")
assert selenium.run_js("return pyodide.globals['obj'] === obj")
def assert_py_to_js_to_py(selenium, name):
selenium.run_js(f"window.obj = pyodide.globals['{name}'];")
assert selenium.run(
f"""
from js import obj
obj is {name}
"""
)
def test_recursive_list_to_js(selenium_standalone):
selenium_standalone.run(
"""
x = []
x.append(x)
"""
)
selenium_standalone.run_js("x = pyodide.pyimport('x')")
def test_recursive_dict_to_js(selenium_standalone):
selenium_standalone.run(
"""
x = {}
x[0] = x
"""
)
selenium_standalone.run_js("x = pyodide.pyimport('x')")
def test_list_from_js(selenium):
selenium.run_js("window.x = [1,2,3];")
assert_js_to_py_to_js(selenium, "x")
def test_dict_from_js(selenium):
selenium.run_js("window.x = { a : 1, b : 2, 0 : 3 };")
assert_js_to_py_to_js(selenium, "x")
def test_error_from_js(selenium):
selenium.run_js("window.err = new Error('hello there?');")
assert_js_to_py_to_js(selenium, "err")
def test_error_from_python(selenium):
selenium.run("err = Exception('hello there?');")
assert_py_to_js_to_py(selenium, "err")
def test_jsproxy_attribute_error(selenium):
selenium.run_js(
"""
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
window.point = new Point(42, 43)
"""
)
selenium.run(
"""
from js import point
assert point.y == 43
"""
)
msg = "AttributeError: z"
with pytest.raises(WebDriverException, match=msg):
selenium.run("point.z")
selenium.run("del point.y")
msg = "AttributeError: y"
with pytest.raises(WebDriverException, match=msg):
selenium.run("point.y")
assert selenium.run_js("return point.y;") is None
def test_javascript_error(selenium):
msg = "JsException: Error: This is a js error"
with pytest.raises(WebDriverException, match=msg):
selenium.run(
"""
from js import Error
err = Error.new("This is a js error")
err2 = Error.new("This is another js error")
raise err
"""
)
def test_javascript_error_back_to_js(selenium):
selenium.run_js(
"""
window.err = new Error("This is a js error")
"""
)
assert (
selenium.run(
"""
from js import err
py_err = err
type(py_err).__name__
"""
)
== "JsException"
)
assert selenium.run_js(
"""
return pyodide.globals["py_err"] === err
"""
)