ยปCore Development>Code coverage>Lib/asyncio/protocols.py

Python code coverage for Lib/asyncio/protocols.py

#countcontent
1n/a"""Abstract Protocol class."""
2n/a
3n/a__all__ = ['BaseProtocol', 'Protocol', 'DatagramProtocol',
4n/a 'SubprocessProtocol']
5n/a
6n/a
7n/aclass BaseProtocol:
8n/a """Common base class for protocol interfaces.
9n/a
10n/a Usually user implements protocols that derived from BaseProtocol
11n/a like Protocol or ProcessProtocol.
12n/a
13n/a The only case when BaseProtocol should be implemented directly is
14n/a write-only transport like write pipe
15n/a """
16n/a
17n/a def connection_made(self, transport):
18n/a """Called when a connection is made.
19n/a
20n/a The argument is the transport representing the pipe connection.
21n/a To receive data, wait for data_received() calls.
22n/a When the connection is closed, connection_lost() is called.
23n/a """
24n/a
25n/a def connection_lost(self, exc):
26n/a """Called when the connection is lost or closed.
27n/a
28n/a The argument is an exception object or None (the latter
29n/a meaning a regular EOF is received or the connection was
30n/a aborted or closed).
31n/a """
32n/a
33n/a def pause_writing(self):
34n/a """Called when the transport's buffer goes over the high-water mark.
35n/a
36n/a Pause and resume calls are paired -- pause_writing() is called
37n/a once when the buffer goes strictly over the high-water mark
38n/a (even if subsequent writes increases the buffer size even
39n/a more), and eventually resume_writing() is called once when the
40n/a buffer size reaches the low-water mark.
41n/a
42n/a Note that if the buffer size equals the high-water mark,
43n/a pause_writing() is not called -- it must go strictly over.
44n/a Conversely, resume_writing() is called when the buffer size is
45n/a equal or lower than the low-water mark. These end conditions
46n/a are important to ensure that things go as expected when either
47n/a mark is zero.
48n/a
49n/a NOTE: This is the only Protocol callback that is not called
50n/a through EventLoop.call_soon() -- if it were, it would have no
51n/a effect when it's most needed (when the app keeps writing
52n/a without yielding until pause_writing() is called).
53n/a """
54n/a
55n/a def resume_writing(self):
56n/a """Called when the transport's buffer drains below the low-water mark.
57n/a
58n/a See pause_writing() for details.
59n/a """
60n/a
61n/a
62n/aclass Protocol(BaseProtocol):
63n/a """Interface for stream protocol.
64n/a
65n/a The user should implement this interface. They can inherit from
66n/a this class but don't need to. The implementations here do
67n/a nothing (they don't raise exceptions).
68n/a
69n/a When the user wants to requests a transport, they pass a protocol
70n/a factory to a utility function (e.g., EventLoop.create_connection()).
71n/a
72n/a When the connection is made successfully, connection_made() is
73n/a called with a suitable transport object. Then data_received()
74n/a will be called 0 or more times with data (bytes) received from the
75n/a transport; finally, connection_lost() will be called exactly once
76n/a with either an exception object or None as an argument.
77n/a
78n/a State machine of calls:
79n/a
80n/a start -> CM [-> DR*] [-> ER?] -> CL -> end
81n/a
82n/a * CM: connection_made()
83n/a * DR: data_received()
84n/a * ER: eof_received()
85n/a * CL: connection_lost()
86n/a """
87n/a
88n/a def data_received(self, data):
89n/a """Called when some data is received.
90n/a
91n/a The argument is a bytes object.
92n/a """
93n/a
94n/a def eof_received(self):
95n/a """Called when the other end calls write_eof() or equivalent.
96n/a
97n/a If this returns a false value (including None), the transport
98n/a will close itself. If it returns a true value, closing the
99n/a transport is up to the protocol.
100n/a """
101n/a
102n/a
103n/aclass DatagramProtocol(BaseProtocol):
104n/a """Interface for datagram protocol."""
105n/a
106n/a def datagram_received(self, data, addr):
107n/a """Called when some datagram is received."""
108n/a
109n/a def error_received(self, exc):
110n/a """Called when a send or receive operation raises an OSError.
111n/a
112n/a (Other than BlockingIOError or InterruptedError.)
113n/a """
114n/a
115n/a
116n/aclass SubprocessProtocol(BaseProtocol):
117n/a """Interface for protocol for subprocess calls."""
118n/a
119n/a def pipe_data_received(self, fd, data):
120n/a """Called when the subprocess writes data into stdout/stderr pipe.
121n/a
122n/a fd is int file descriptor.
123n/a data is bytes object.
124n/a """
125n/a
126n/a def pipe_connection_lost(self, fd, exc):
127n/a """Called when a file descriptor associated with the child process is
128n/a closed.
129n/a
130n/a fd is the int file descriptor that was closed.
131n/a """
132n/a
133n/a def process_exited(self):
134n/a """Called when subprocess has exited."""