pyduktape icon indicating copy to clipboard operation
pyduktape copied to clipboard

JS print statements & exceptions bypass stdout and stderr

Open TEParsons opened this issue 4 years ago • 0 comments

I'm trying to use pyduktape to create a JS shell in Python, which means capturing the output from JS calls and printing it to a GUI object in Python. Not sure if this is the intended behaviour, but any output generated by eval_js isn't caught by sys.stdout or sys.stderr - meaning the usual procedure for this sort of thing (using contextlib.rediect_stdout) doesn't work.

This can be seen with the following code:

import io
import pyduktape
from contextlib import redirect_stdout, redirect_stderr

buffer = io.StringIO()
context = pyduktape.DuktapeContext()

with redirect_stdout(buffer):
    context.eval_js("print('hello')")
with redirect_stderr(buffer):
    context.eval_js("print('hello')")

Whether this is me reporting a bug or suggesting a feature depends on how intentional this is, but being able to catch stdout from JS would certainly be handy!

TEParsons avatar Oct 15 '20 14:10 TEParsons