Finally, a way to sandbox Python in pure Python. :-)
I have tried running an old release of the Python 3 implementation Brython in micro-javascript and got a micro-javascript exception. I picked an old release to guarantee ES5.
Here are the steps to reproduce the exception:
$ wget https://github.com/brython-dev/brython/releases/download/2.2.1/Brython2.2.1-20140915-210107.zip
$ unzip Brython2.2.1-20140915-210107.zip
$ cd Brython2.2.1-20140915-210107/
$ # Fix incompatible JS syntax. Change to `-i.bak` for FreeBSD sed.
$ sed -i 's|return *$|return;|;s|0\.)|0.0)|' brython.js
$ cat > test.py
from pathlib import Path
from microjs import Context
ctx = Context(memory_limit=1024*1024, time_limit=5.0)
result = ctx.eval(Path("brython.js").read_text())
print(result)
$ uv run --with micro-javascript python3 test.py
Traceback (most recent call last):
File "/tmp/Brython2.2.1-20140915-210107/test.py", line 6, in <module>
result = ctx.eval(Path("brython.js").read_text())
File "/home/dbohdan/.cache/uv/archive-v0/qRjrQTsy8XxabUpgxFIky/lib/python3.14/site-packages/microjs/context.py", line 1208, in eval
compiled = compiler.compile(ast)
File "/home/dbohdan/.cache/uv/archive-v0/qRjrQTsy8XxabUpgxFIky/lib/python3.14/site-packages/microjs/compiler.py", line 122, in compile
self._compile_statement(stmt)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "/home/dbohdan/.cache/uv/archive-v0/qRjrQTsy8XxabUpgxFIky/lib/python3.14/site-packages/microjs/compiler.py", line 384, in _compile_statement
self._compile_expression(node.expression)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/home/dbohdan/.cache/uv/archive-v0/qRjrQTsy8XxabUpgxFIky/lib/python3.14/site-packages/microjs/compiler.py", line 1494, in _compile_expression
self._compile_expression(node.callee)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "/home/dbohdan/.cache/uv/archive-v0/qRjrQTsy8XxabUpgxFIky/lib/python3.14/site-packages/microjs/compiler.py", line 1507, in _compile_expression
func = self._compile_function(
name, node.params, node.body, is_expression=True
)
File "/home/dbohdan/.cache/uv/archive-v0/qRjrQTsy8XxabUpgxFIky/lib/python3.14/site-packages/microjs/compiler.py", line 1092, in _compile_function
bytecode=bytes(self.bytecode),
~~~~~^^^^^^^^^^^^^^^
ValueError: bytes must be in range(0, 256)
The same thing happens with other large, minified JavaScript source files, like an old version of jQuery.
Finally, a way to sandbox Python in pure Python. :-)
I have tried running an old release of the Python 3 implementation Brython in micro-javascript and got a micro-javascript exception. I picked an old release to guarantee ES5.
Here are the steps to reproduce the exception:
The same thing happens with other large, minified JavaScript source files, like an old version of jQuery.