Skip to content

Commit 2d1ec06

Browse files
committed
Issue #21364: remove recommendation of broken pattern.
1 parent fcbeab5 commit 2d1ec06

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

Doc/library/sys.rst

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,35 +1066,32 @@ always available.
10661066
statements and for the prompts of :func:`input`;
10671067
* The interpreter's own prompts and its error messages go to ``stderr``.
10681068

1069-
By default, these streams are regular text streams as returned by the
1070-
:func:`open` function. Their parameters are chosen as follows:
1069+
These streams are regular :term:`text files <text file>` like those
1070+
returned by the :func:`open` function. Their parameters are chosen as
1071+
follows:
10711072

10721073
* The character encoding is platform-dependent. Under Windows, if the stream
10731074
is interactive (that is, if its :meth:`isatty` method returns ``True``), the
10741075
console codepage is used, otherwise the ANSI code page. Under other
10751076
platforms, the locale encoding is used (see :meth:`locale.getpreferredencoding`).
10761077

10771078
Under all platforms though, you can override this value by setting the
1078-
:envvar:`PYTHONIOENCODING` environment variable.
1079+
:envvar:`PYTHONIOENCODING` environment variable before starting Python.
10791080

10801081
* When interactive, standard streams are line-buffered. Otherwise, they
10811082
are block-buffered like regular text files. You can override this
10821083
value with the :option:`-u` command-line option.
10831084

1084-
To write or read binary data from/to the standard streams, use the
1085-
underlying binary :data:`~io.TextIOBase.buffer`. For example, to write
1086-
bytes to :data:`stdout`, use ``sys.stdout.buffer.write(b'abc')``. Using
1087-
:meth:`io.TextIOBase.detach`, streams can be made binary by default. This
1088-
function sets :data:`stdin` and :data:`stdout` to binary::
1085+
.. note::
10891086

1090-
def make_streams_binary():
1091-
sys.stdin = sys.stdin.detach()
1092-
sys.stdout = sys.stdout.detach()
1087+
To write or read binary data from/to the standard streams, use the
1088+
underlying binary :data:`~io.TextIOBase.buffer` object. For example, to
1089+
write bytes to :data:`stdout`, use ``sys.stdout.buffer.write(b'abc')``.
10931090

1094-
Note that the streams may be replaced with objects (like :class:`io.StringIO`)
1095-
that do not support the :attr:`~io.BufferedIOBase.buffer` attribute or the
1096-
:meth:`~io.BufferedIOBase.detach` method and can raise :exc:`AttributeError`
1097-
or :exc:`io.UnsupportedOperation`.
1091+
However, if you are writing a library (and do not control in which
1092+
context its code will be executed), be aware that the standard streams
1093+
may be replaced with file-like objects like :class:`io.StringIO` which
1094+
do not support the :attr:`~io.BufferedIOBase.buffer` attribute.
10981095

10991096

11001097
.. data:: __stdin__

0 commit comments

Comments
 (0)