Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8ff01c

Browse files
committed
Issue #13597: Improve documentation of standard streams.
2 parents e0e2735 + 7158e06 commit a8ff01c

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

Doc/library/sys.rst

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -941,31 +941,42 @@ always available.
941941
stdout
942942
stderr
943943

944-
:term:`File objects <file object>` corresponding to the interpreter's standard
945-
input, output and error streams. ``stdin`` is used for all interpreter input
946-
except for scripts but including calls to :func:`input`. ``stdout`` is used
947-
for the output of :func:`print` and :term:`expression` statements and for the
948-
prompts of :func:`input`. The interpreter's own prompts
949-
and (almost all of) its error messages go to ``stderr``. ``stdout`` and
950-
``stderr`` needn't be built-in file objects: any object is acceptable as long
951-
as it has a :meth:`write` method that takes a string argument. (Changing these
952-
objects doesn't affect the standard I/O streams of processes executed by
953-
:func:`os.popen`, :func:`os.system` or the :func:`exec\*` family of functions in
954-
the :mod:`os` module.)
955-
956-
The standard streams are in text mode by default. To write or read binary
957-
data to these, use the underlying binary buffer. For example, to write bytes
958-
to :data:`stdout`, use ``sys.stdout.buffer.write(b'abc')``. Using
959-
:meth:`io.TextIOBase.detach` streams can be made binary by default. This
944+
:term:`File objects <file object>` used by the interpreter for standard
945+
input, output and errors:
946+
947+
* ``stdin`` is used for all interactive input (including calls to
948+
:func:`input`);
949+
* ``stdout`` is used for the output of :func:`print` and :term:`expression`
950+
statements and for the prompts of :func:`input`;
951+
* The interpreter's own prompts and its error messages go to ``stderr``.
952+
953+
By default, these streams are regular text streams as returned by the
954+
:func:`open` function. Their parameters are chosen as follows:
955+
956+
* The character encoding is platform-dependent. Under Windows, if the stream
957+
is interactive (that is, if its :meth:`isatty` method returns True), the
958+
console codepage is used, otherwise the ANSI code page. Under other
959+
platforms, the locale encoding is used (see :meth:`locale.getpreferredencoding`).
960+
961+
Under all platforms though, you can override this value by setting the
962+
:envvar:`PYTHONIOENCODING` environment variable.
963+
964+
* When interactive, standard streams are line-buffered. Otherwise, they
965+
are block-buffered like regular text files. You can override this
966+
value with the :option:`-u` command-line option.
967+
968+
To write or read binary data from/to the standard streams, use the
969+
underlying binary :data:`~io.TextIOBase.buffer`. For example, to write
970+
bytes to :data:`stdout`, use ``sys.stdout.buffer.write(b'abc')``. Using
971+
:meth:`io.TextIOBase.detach`, streams can be made binary by default. This
960972
function sets :data:`stdin` and :data:`stdout` to binary::
961973

962974
def make_streams_binary():
963975
sys.stdin = sys.stdin.detach()
964976
sys.stdout = sys.stdout.detach()
965977

966-
Note that the streams can be replaced with objects (like
967-
:class:`io.StringIO`) that do not support the
968-
:attr:`~io.BufferedIOBase.buffer` attribute or the
978+
Note that the streams may be replaced with objects (like :class:`io.StringIO`)
979+
that do not support the :attr:`~io.BufferedIOBase.buffer` attribute or the
969980
:meth:`~io.BufferedIOBase.detach` method and can raise :exc:`AttributeError`
970981
or :exc:`io.UnsupportedOperation`.
971982

0 commit comments

Comments
 (0)