-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-36889: Document Stream class and add docstrings #14488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7ed4629
e2a64ad
19798d5
8ac5116
479a860
df72ef4
99fefef
c191de6
623afb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,6 @@ and work with streams: | |
Connect to TCP socket on *host* : *port* address and return a :class:`Stream` | ||
object of mode :attr:`StreamMode.READWRITE`. | ||
|
||
|
||
*limit* determines the buffer size limit used by the returned :class:`Stream` | ||
instance. By default the *limit* is set to 64 KiB. | ||
|
||
|
@@ -367,13 +366,184 @@ Stream | |
|
||
Represents a Stream object that provides APIs to read and write data | ||
to the IO stream . It includes the API provided by :class:`StreamReader` | ||
and :class:`StreamWriter`. | ||
and :class:`StreamWriter`. It can also be used as :term:`asynchronous iterator` | ||
where :meth:`readline` is used. It raises :exc:`StopAsyncIteration` when | ||
:meth:`readline` returns empty data. | ||
|
||
Do not instantiate *Stream* objects directly; use API like :func:`connect` | ||
and :class:`StreamServer` instead. | ||
|
||
.. versionadded:: 3.8 | ||
|
||
.. attribute:: mode | ||
|
||
Returns the mode of the stream which is a :class:`StreamMode` value. It could | ||
be one of the below: | ||
|
||
* :attr:`StreamMode.READ` - Connection can receive data. | ||
* :attr:`StreamMode.WRITE` - Connection can send data. | ||
* :attr:`StreamMode.READWRITE` - Connection can send and receive data. | ||
|
||
.. coroutinemethod:: abort() | ||
|
||
Aborts the connection immediately, without waiting for the send buffer to drain. | ||
|
||
.. method:: at_eof() | ||
|
||
Return ``True`` if the buffer is empty. | ||
|
||
.. method:: can_write_eof() | ||
|
||
Return *True* if the underlying transport supports | ||
the :meth:`write_eof` method, *False* otherwise. | ||
|
||
.. method:: close() | ||
|
||
The method closes the stream and the underlying socket. | ||
|
||
It is possible to directly await on the `close()` method:: | ||
|
||
await stream.close() | ||
|
||
The ``await`` pauses the current coroutine until the stream and the underlying | ||
socket are closed (and SSL shutdown is performed for a secure connection). | ||
|
||
.. coroutinemethod:: drain() | ||
|
||
Wait until it is appropriate to resume writing to the stream. | ||
Example:: | ||
|
||
stream.write(data) | ||
await stream.drain() | ||
|
||
This is a flow control method that interacts with the underlying | ||
IO write buffer. When the size of the buffer reaches | ||
the high watermark, *drain()* blocks until the size of the | ||
buffer is drained down to the low watermark and writing can | ||
be resumed. When there is nothing to wait for, the :meth:`drain` | ||
returns immediately. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit more subtle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my perspective, people always should use The only reason why the method exists is backward compatibility for subprocess stdin/stdout/stderr pipes usage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should reflect it in docs but sorry, I cannot propose exact wording. |
||
|
||
.. deprecated:: 3.8 | ||
|
||
It is recommended to directly await on the `write()` method instead:: | ||
|
||
await stream.write(data) | ||
|
||
.. method:: get_extra_info(name, default=None) | ||
|
||
Access optional transport information; see | ||
:meth:`BaseTransport.get_extra_info` for details. | ||
|
||
.. method:: is_closing() | ||
|
||
Return ``True`` if the stream is closed or in the process of | ||
being closed. | ||
|
||
.. coroutinemethod:: read(n=-1) | ||
|
||
Read up to *n* bytes. If *n* is not provided, or set to ``-1``, | ||
read until EOF and return all read bytes. | ||
|
||
If EOF was received and the internal buffer is empty, | ||
return an empty ``bytes`` object. | ||
|
||
.. coroutinemethod:: readexactly(n) | ||
|
||
Read exactly *n* bytes. | ||
|
||
Raise an :exc:`IncompleteReadError` if EOF is reached before *n* | ||
can be read. Use the :attr:`IncompleteReadError.partial` | ||
attribute to get the partially read data. | ||
|
||
.. coroutinemethod:: readline() | ||
|
||
Read one line, where "line" is a sequence of bytes | ||
ending with ``\n``. | ||
|
||
If EOF is received and ``\n`` was not found, the method | ||
returns partially read data. | ||
|
||
If EOF is received and the internal buffer is empty, | ||
return an empty ``bytes`` object. | ||
|
||
.. coroutinemethod:: readuntil(separator=b'\\n') | ||
|
||
Read data from the stream until *separator* is found. | ||
|
||
On success, the data and separator will be removed from the | ||
internal buffer (consumed). Returned data will include the | ||
separator at the end. | ||
|
||
If the amount of data read exceeds the configured stream limit, a | ||
:exc:`LimitOverrunError` exception is raised, and the data | ||
is left in the internal buffer and can be read again. | ||
|
||
If EOF is reached before the complete separator is found, | ||
an :exc:`IncompleteReadError` exception is raised, and the internal | ||
buffer is reset. The :attr:`IncompleteReadError.partial` attribute | ||
may contain a portion of the separator. | ||
|
||
.. coroutinemethod:: sendfile(file, offset=0, count=None, *, fallback=True) | ||
|
||
Sends a *file* over the stream using an optimized syscall if available. | ||
|
||
For other parameters meaning please see :meth:`AbstractEventloop.sendfile`. | ||
|
||
.. coroutinemethod:: start_tls(sslcontext, *, server_hostname=None, \ | ||
ssl_handshake_timeout=None) | ||
|
||
Upgrades the existing transport-based connection to TLS. | ||
|
||
For other parameters meaning please see :meth:`AbstractEventloop.start_tls`. | ||
|
||
.. coroutinemethod:: wait_closed() | ||
|
||
Wait until the stream is closed. | ||
|
||
Should be called after :meth:`close` to wait until the underlying | ||
connection is closed. | ||
|
||
.. coroutinemethod:: write(data) | ||
|
||
Write *data* to the underlying socket; wait until the data is sent, e.g.:: | ||
|
||
await stream.write(data) | ||
|
||
.. method:: write(data) | ||
|
||
The method attempts to write the *data* to the underlying socket immediately. | ||
asvetlov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
If that fails, the data is queued in an internal write buffer until it can be | ||
sent. :meth:`drain` can be used to flush the underlying buffer once writing is | ||
available:: | ||
|
||
stream.write(data) | ||
await stream.drain() | ||
|
||
.. deprecated:: 3.8 | ||
|
||
It is recommended to directly await on the `write()` method instead:: | ||
|
||
await stream.write(data) | ||
|
||
.. method:: writelines(data) | ||
|
||
The method writes a list (or any iterable) of bytes to the underlying socket | ||
immediately. | ||
If that fails, the data is queued in an internal write buffer until it can be | ||
sent. | ||
|
||
It is possible to directly await on the `writelines()` method:: | ||
|
||
await stream.writelines(lines) | ||
|
||
The ``await`` pauses the current coroutine until the data is written to the | ||
socket. | ||
|
||
.. method:: write_eof() | ||
|
||
Close the write end of the stream after the buffered write | ||
data is flushed. | ||
|
||
|
||
StreamMode | ||
========== | ||
|
@@ -459,8 +629,7 @@ StreamReader | |
|
||
.. method:: at_eof() | ||
|
||
Return ``True`` if the buffer is empty and :meth:`feed_eof` | ||
was called. | ||
Return ``True`` if the buffer is empty. | ||
|
||
|
||
StreamWriter | ||
|
@@ -504,7 +673,7 @@ StreamWriter | |
If that fails, the data is queued in an internal write buffer until it can be | ||
sent. | ||
|
||
Starting with Python 3.8, it is possible to directly await on the `write()` | ||
Starting with Python 3.8, it is possible to directly await on the `writelines()` | ||
method:: | ||
|
||
await stream.writelines(lines) | ||
|
Uh oh!
There was an error while loading. Please reload this page.