pyodide/docs/usage/streams.md

43 lines
2.3 KiB
Markdown
Raw Normal View History

Implement more detailed streams support (#3268) Resolves https://github.com/pyodide/pyodide/issues/3112 This adds a carefully designed API for controlling stdin, stdout, and stderr. It changes the default behavior to be a bit more useful, though in doing so introduces some mild backwards incompatibility. In particular: 1. By default, stdin reads directly from `process.stdin` in node (as before) and raises an error if in browser (not as before). 2. By default, stdout writes directly to `process.stdout` in node (before it called console.log) and calls console.log in browser (as before). 3. By default, stderr writes directly to `process.stderr` in node (before it called console.warn) and calls console.warn in browser (as before). 4. In all three cases, by default isatty(stdin/stdout/stderr) is true in node and false in browser (in the browser it used to be true). 5. As before, if you pass `stdin`, `stdout`, or `stderr` as arguments to `loadPyodide`, `isatty` of the corresponding stream is set to false. The stdin function is now more flexible: we now correctly handle the case where it returns an ArrayBuffer or ArrayBufferView. I also added 3 new functions to set streams after Pyodide is loaded which offer additional control: * `setStdin({stdin?, error?, isatty = false})` -- Sets the stdin function. The stdin function takes no arguments and should return null, undefined, a string, or a buffer. Sets and `isatty(stdin)` to `isatty` (by default `false`). If error is true, set stdin to always raise an EIO error when it is read. * `setStdout({raw?, batched?, isatty = false})` -- If neither raw nor batched is passed, restore default stdout behavior. If rwa is passed, the raw stdout function receives a byte which it should interpret as a utf8 character code. Sets `isatty(stdout)` to isatty (by default `false`). If batched is passed but not raw, it sets a batched stdout function. The stdout function receives a string and should do something with it. In this case it ignores isatty and sets isatty(stdout) to false. * `setStderr({raw?, batched?, isatty = false})` -- same but with stderr.
2022-12-18 23:55:52 +00:00
(streams)=
# Redirecting standard streams
2023-02-06 07:45:12 +00:00
Pyodide has three functions {js:func}`pyodide.setStdin`,
{js:func}`pyodide.setStdout`, and {js:func}`pyodide.setStderr` that change the
behavior of reading from {py:data}`~sys.stdin` and writing to {py:data}`~sys.stdout` and
{py:data}`~sys.stderr` respectively.
Implement more detailed streams support (#3268) Resolves https://github.com/pyodide/pyodide/issues/3112 This adds a carefully designed API for controlling stdin, stdout, and stderr. It changes the default behavior to be a bit more useful, though in doing so introduces some mild backwards incompatibility. In particular: 1. By default, stdin reads directly from `process.stdin` in node (as before) and raises an error if in browser (not as before). 2. By default, stdout writes directly to `process.stdout` in node (before it called console.log) and calls console.log in browser (as before). 3. By default, stderr writes directly to `process.stderr` in node (before it called console.warn) and calls console.warn in browser (as before). 4. In all three cases, by default isatty(stdin/stdout/stderr) is true in node and false in browser (in the browser it used to be true). 5. As before, if you pass `stdin`, `stdout`, or `stderr` as arguments to `loadPyodide`, `isatty` of the corresponding stream is set to false. The stdin function is now more flexible: we now correctly handle the case where it returns an ArrayBuffer or ArrayBufferView. I also added 3 new functions to set streams after Pyodide is loaded which offer additional control: * `setStdin({stdin?, error?, isatty = false})` -- Sets the stdin function. The stdin function takes no arguments and should return null, undefined, a string, or a buffer. Sets and `isatty(stdin)` to `isatty` (by default `false`). If error is true, set stdin to always raise an EIO error when it is read. * `setStdout({raw?, batched?, isatty = false})` -- If neither raw nor batched is passed, restore default stdout behavior. If rwa is passed, the raw stdout function receives a byte which it should interpret as a utf8 character code. Sets `isatty(stdout)` to isatty (by default `false`). If batched is passed but not raw, it sets a batched stdout function. The stdout function receives a string and should do something with it. In this case it ignores isatty and sets isatty(stdout) to false. * `setStderr({raw?, batched?, isatty = false})` -- same but with stderr.
2022-12-18 23:55:52 +00:00
`setStdin({stdin?, isatty?, error?})` takes a function which should take zero
arguments and return either a string or an ArrayBufferView of information read
2023-02-06 07:45:12 +00:00
from stdin. The `isatty` argument signals whether {py:func}`isatty(stdin) <os.isatty>` should be true
Implement more detailed streams support (#3268) Resolves https://github.com/pyodide/pyodide/issues/3112 This adds a carefully designed API for controlling stdin, stdout, and stderr. It changes the default behavior to be a bit more useful, though in doing so introduces some mild backwards incompatibility. In particular: 1. By default, stdin reads directly from `process.stdin` in node (as before) and raises an error if in browser (not as before). 2. By default, stdout writes directly to `process.stdout` in node (before it called console.log) and calls console.log in browser (as before). 3. By default, stderr writes directly to `process.stderr` in node (before it called console.warn) and calls console.warn in browser (as before). 4. In all three cases, by default isatty(stdin/stdout/stderr) is true in node and false in browser (in the browser it used to be true). 5. As before, if you pass `stdin`, `stdout`, or `stderr` as arguments to `loadPyodide`, `isatty` of the corresponding stream is set to false. The stdin function is now more flexible: we now correctly handle the case where it returns an ArrayBuffer or ArrayBufferView. I also added 3 new functions to set streams after Pyodide is loaded which offer additional control: * `setStdin({stdin?, error?, isatty = false})` -- Sets the stdin function. The stdin function takes no arguments and should return null, undefined, a string, or a buffer. Sets and `isatty(stdin)` to `isatty` (by default `false`). If error is true, set stdin to always raise an EIO error when it is read. * `setStdout({raw?, batched?, isatty = false})` -- If neither raw nor batched is passed, restore default stdout behavior. If rwa is passed, the raw stdout function receives a byte which it should interpret as a utf8 character code. Sets `isatty(stdout)` to isatty (by default `false`). If batched is passed but not raw, it sets a batched stdout function. The stdout function receives a string and should do something with it. In this case it ignores isatty and sets isatty(stdout) to false. * `setStderr({raw?, batched?, isatty = false})` -- same but with stderr.
2022-12-18 23:55:52 +00:00
or false. If you pass `error: true` then reading from stdin will return an
error. If `setStdin` is called with no arguments, the default value is restored.
2023-02-06 07:45:12 +00:00
In Node the default behavior is to read from {js:data}`process.stdin` and in the browser
Implement more detailed streams support (#3268) Resolves https://github.com/pyodide/pyodide/issues/3112 This adds a carefully designed API for controlling stdin, stdout, and stderr. It changes the default behavior to be a bit more useful, though in doing so introduces some mild backwards incompatibility. In particular: 1. By default, stdin reads directly from `process.stdin` in node (as before) and raises an error if in browser (not as before). 2. By default, stdout writes directly to `process.stdout` in node (before it called console.log) and calls console.log in browser (as before). 3. By default, stderr writes directly to `process.stderr` in node (before it called console.warn) and calls console.warn in browser (as before). 4. In all three cases, by default isatty(stdin/stdout/stderr) is true in node and false in browser (in the browser it used to be true). 5. As before, if you pass `stdin`, `stdout`, or `stderr` as arguments to `loadPyodide`, `isatty` of the corresponding stream is set to false. The stdin function is now more flexible: we now correctly handle the case where it returns an ArrayBuffer or ArrayBufferView. I also added 3 new functions to set streams after Pyodide is loaded which offer additional control: * `setStdin({stdin?, error?, isatty = false})` -- Sets the stdin function. The stdin function takes no arguments and should return null, undefined, a string, or a buffer. Sets and `isatty(stdin)` to `isatty` (by default `false`). If error is true, set stdin to always raise an EIO error when it is read. * `setStdout({raw?, batched?, isatty = false})` -- If neither raw nor batched is passed, restore default stdout behavior. If rwa is passed, the raw stdout function receives a byte which it should interpret as a utf8 character code. Sets `isatty(stdout)` to isatty (by default `false`). If batched is passed but not raw, it sets a batched stdout function. The stdout function receives a string and should do something with it. In this case it ignores isatty and sets isatty(stdout) to false. * `setStderr({raw?, batched?, isatty = false})` -- same but with stderr.
2022-12-18 23:55:52 +00:00
it is to throw an error.
`setStdout({batched?, raw?, isattty?})` sets the standard out handler and
similarly `setStderr` (same arguments) sets the stdandard error handler. If a
`raw` handler is provided then the handler is called with a `number` for each
byte of the output to stdout. The handler is expected to deal with this in
2023-02-06 07:45:12 +00:00
whatever way it prefers. `isattty` again controls whether {py:func}`isatty(stdout) <os.isatty>`
Implement more detailed streams support (#3268) Resolves https://github.com/pyodide/pyodide/issues/3112 This adds a carefully designed API for controlling stdin, stdout, and stderr. It changes the default behavior to be a bit more useful, though in doing so introduces some mild backwards incompatibility. In particular: 1. By default, stdin reads directly from `process.stdin` in node (as before) and raises an error if in browser (not as before). 2. By default, stdout writes directly to `process.stdout` in node (before it called console.log) and calls console.log in browser (as before). 3. By default, stderr writes directly to `process.stderr` in node (before it called console.warn) and calls console.warn in browser (as before). 4. In all three cases, by default isatty(stdin/stdout/stderr) is true in node and false in browser (in the browser it used to be true). 5. As before, if you pass `stdin`, `stdout`, or `stderr` as arguments to `loadPyodide`, `isatty` of the corresponding stream is set to false. The stdin function is now more flexible: we now correctly handle the case where it returns an ArrayBuffer or ArrayBufferView. I also added 3 new functions to set streams after Pyodide is loaded which offer additional control: * `setStdin({stdin?, error?, isatty = false})` -- Sets the stdin function. The stdin function takes no arguments and should return null, undefined, a string, or a buffer. Sets and `isatty(stdin)` to `isatty` (by default `false`). If error is true, set stdin to always raise an EIO error when it is read. * `setStdout({raw?, batched?, isatty = false})` -- If neither raw nor batched is passed, restore default stdout behavior. If rwa is passed, the raw stdout function receives a byte which it should interpret as a utf8 character code. Sets `isatty(stdout)` to isatty (by default `false`). If batched is passed but not raw, it sets a batched stdout function. The stdout function receives a string and should do something with it. In this case it ignores isatty and sets isatty(stdout) to false. * `setStderr({raw?, batched?, isatty = false})` -- same but with stderr.
2022-12-18 23:55:52 +00:00
returns `true` or `false`.
On the other hand, a `batched` handler is only called with complete lines of
text (or when the output is flushed). A `batched` handler cannot have `isatty`
set to `true` because it is impossible to use such a handler to make something
behave like a tty.
Passing neither `raw` nor `batched` sets the default behavior. In Node the
2023-02-06 07:45:12 +00:00
default behavior is to write directly to {js:data}`process.stdout` and
{js:data}`process.stderr` (in this case `isatty` depends on whether
{js:data}`process.stdout` and {js:data}`process.stderr` are ttys). In browser,
the default behavior is achieved with `pyodide.setStdout({batched: console.log})`
and `pyodide.setStderr({batched: console.warn})`.
Implement more detailed streams support (#3268) Resolves https://github.com/pyodide/pyodide/issues/3112 This adds a carefully designed API for controlling stdin, stdout, and stderr. It changes the default behavior to be a bit more useful, though in doing so introduces some mild backwards incompatibility. In particular: 1. By default, stdin reads directly from `process.stdin` in node (as before) and raises an error if in browser (not as before). 2. By default, stdout writes directly to `process.stdout` in node (before it called console.log) and calls console.log in browser (as before). 3. By default, stderr writes directly to `process.stderr` in node (before it called console.warn) and calls console.warn in browser (as before). 4. In all three cases, by default isatty(stdin/stdout/stderr) is true in node and false in browser (in the browser it used to be true). 5. As before, if you pass `stdin`, `stdout`, or `stderr` as arguments to `loadPyodide`, `isatty` of the corresponding stream is set to false. The stdin function is now more flexible: we now correctly handle the case where it returns an ArrayBuffer or ArrayBufferView. I also added 3 new functions to set streams after Pyodide is loaded which offer additional control: * `setStdin({stdin?, error?, isatty = false})` -- Sets the stdin function. The stdin function takes no arguments and should return null, undefined, a string, or a buffer. Sets and `isatty(stdin)` to `isatty` (by default `false`). If error is true, set stdin to always raise an EIO error when it is read. * `setStdout({raw?, batched?, isatty = false})` -- If neither raw nor batched is passed, restore default stdout behavior. If rwa is passed, the raw stdout function receives a byte which it should interpret as a utf8 character code. Sets `isatty(stdout)` to isatty (by default `false`). If batched is passed but not raw, it sets a batched stdout function. The stdout function receives a string and should do something with it. In this case it ignores isatty and sets isatty(stdout) to false. * `setStderr({raw?, batched?, isatty = false})` -- same but with stderr.
2022-12-18 23:55:52 +00:00
The arguments `stdin`, `stdout`, and `stderr` to `loadPyodide` provide a
diminished amount of control compared to `setStdin`, `setStdout`, and
`setStderr`. They all set `isatty` to `false` and use batched processing for
`setStdout` and `setStderr`. In most cases, nothing is written or read to any of
these streams while Pyodide is starting, so if you need the added flexibility
you can wait until Pyodide is loaded and then use the `pyodide.setStdxxx` apis.