def test_wrong_way_conversions4(selenium): from pyodide import to_js s = "avafhjpa" t = 55 assert to_js(s) is s assert to_js(t) is t
def test_to_js_default_converter2(selenium): import json import pytest from js import JSON, Array from pyodide import JsException, run_js, to_js class Pair: def __init__(self, first, second): self.first = first self.second = second p1 = Pair(1, 2) p2 = Pair(1, 2) p2.first = p2 def default_converter(value, convert, cacheConversion): result = Array.new() cacheConversion(value, result) result.push(convert(value.first)) result.push(convert(value.second)) return result p1js = to_js(p1, default_converter=default_converter) p2js = to_js(p2, default_converter=default_converter) assert json.loads(JSON.stringify(p1js)) == [1, 2] with pytest.raises(JsException, match="TypeError"): JSON.stringify(p2js) assert run_js("(x) => x[0] === x")(p2js) assert run_js("(x) => x[1] === 2")(p2js)
def test_tojs8(selenium): import pytest from pyodide import ConversionError, to_js msg = r"Cannot use \(2, 2\) as a key for a Javascript" with pytest.raises(ConversionError, match=msg): to_js({(2, 2): 0}) with pytest.raises(ConversionError, match=msg): to_js({(2, 2)})
async def install(self, requirements: Union[str, List[str]], ctx=None): if isinstance(requirements, str): requirements = [requirements] transaction: Dict[str, Any] = { "wheels": [], "pyodide_packages": [], "locked": dict(self.installed_packages), } requirement_promises = [] for requirement in requirements: requirement_promises.append( self.add_requirement(requirement, ctx, transaction)) await gather(*requirement_promises) wheel_promises = [] # Install built-in packages pyodide_packages = transaction["pyodide_packages"] if len(pyodide_packages): # Note: branch never happens in out-of-browser testing because in # that case builtin_packages is empty. self.installed_packages.update(pyodide_packages) wheel_promises.append( asyncio.ensure_future( pyodide_js.loadPackage( to_js([name for [name, _] in pyodide_packages])))) # Now install PyPI packages for name, wheel, ver in transaction["wheels"]: wheel_promises.append(_install_wheel(name, wheel)) self.installed_packages[name] = ver await gather(*wheel_promises)
def test_wrong_way_conversions2(selenium): from pyodide import run_js, to_js [astr, bstr] = run_js(""" (a) => { b = [1,2,3]; return [JSON.stringify(a), JSON.stringify(b)] } """)(to_js([1, 2, 3])) assert astr == bstr
def test_wrong_way_conversions3(selenium): from pyodide import run_js, to_js class Test: pass t1 = Test() t2 = to_js(t1) t3 = run_js("(t2) => t2.copy()")(t2) assert t1 is t3 t2.destroy()
def test_dict_converter3(selenium): import json from js import Object from pyodide import run_js, to_js d = {x: x + 2 for x in range(5)} res = to_js(d, dict_converter=Object.fromEntries) constructor, serialized = run_js(""" (res) => [res.constructor.name, JSON.stringify(res)] """)(res) assert constructor == "Object" assert json.loads(serialized) == {str(k): v for k, v in d.items()}
def test_dict_converter1(selenium): import json from pyodide import run_js, to_js arrayFrom = run_js("Array.from") d = {x: x + 2 for x in range(5)} res = to_js(d, dict_converter=arrayFrom) constructor, serialized = run_js(""" (res) => { return [res.constructor.name, JSON.stringify(res)]; } """)(res) assert constructor == "Array" assert json.loads(serialized) == [list(x) for x in d.items()]
async def install(self, requirements: Union[str, List[str]], ctx=None): transaction = await self.gather_requirements(requirements, ctx) wheel_promises = [] # Install built-in packages pyodide_packages = transaction["pyodide_packages"] if len(pyodide_packages): # Note: branch never happens in out-of-browser testing because in # that case builtin_packages is empty. self.installed_packages.update(pyodide_packages) wheel_promises.append( asyncio.ensure_future( pyodide_js.loadPackage( to_js([name for [name, _] in pyodide_packages])))) # Now install PyPI packages for name, wheel, ver in transaction["wheels"]: wheel_promises.append(_install_wheel(name, wheel)) self.installed_packages[name] = ver await gather(*wheel_promises)
def _link_docs(pydoc, jsdoc): def jssync(event): if (getattr(event, 'setter_id', None) is not None): return events = [event] json_patch = jsdoc.create_json_patch_string(pyodide.to_js(events)) pydoc.apply_json_patch(json.loads(json_patch)) jsdoc.on_change(pyodide.create_proxy(jssync), pyodide.to_js(False)) def pysync(event): json_patch, buffers = process_document_events([event], use_buffers=True) buffer_map = {} for (ref, buffer) in buffers: buffer_map[ref['id']] = pyodide.to_js(buffer).buffer jsdoc.apply_json_patch(JSON.parse(json_patch), pyodide.to_js(buffer_map), setter_id='js') pydoc.on_change(pysync)
def test_wrong_way_track_proxies(selenium): from pyodide import run_js checkDestroyed = run_js(""" function checkDestroyed(l){ for(let e of l){ if(pyodide.isPyProxy(e)){ console.log("\\n\\n", "!!!!!!!!!", e.$$.ptr); assert(() => e.$$.ptr === 0); } else { checkDestroyed(e); } } }; checkDestroyed """) from unittest import TestCase from js import Array, Object from pyodide import ConversionError, destroy_proxies, to_js raises = TestCase().assertRaises class T: pass x = [[T()], [T()], [[[T()], [T()]], [T(), [], [[T()]], T()], T(), T()], T()] proxylist = Array.new() r = to_js(x, pyproxies=proxylist) assert len(proxylist) == 10 destroy_proxies(proxylist) checkDestroyed(r) with raises(TypeError): to_js(x, pyproxies=[]) with raises(TypeError): to_js(x, pyproxies=Object.new()) with raises(ConversionError): to_js(x, create_pyproxies=False)
def to_js(self): return to_js(self.config)
def test_recursive_dict_to_js(selenium): x: Any = {} x[0] = x from pyodide import to_js to_js(x)
def test_recursive_list_to_js(selenium): x: Any = [] x.append(x) from pyodide import to_js to_js(x)