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

Python code coverage for Lib/asyncio/transports.py

#countcontent
1n/a"""Abstract Transport class."""
2n/a
3n/afrom asyncio import compat
4n/a
5n/a__all__ = ['BaseTransport', 'ReadTransport', 'WriteTransport',
6n/a 'Transport', 'DatagramTransport', 'SubprocessTransport',
7n/a ]
8n/a
9n/a
10n/aclass BaseTransport:
11n/a """Base class for transports."""
12n/a
13n/a def __init__(self, extra=None):
14n/a if extra is None:
15n/a extra = {}
16n/a self._extra = extra
17n/a
18n/a def get_extra_info(self, name, default=None):
19n/a """Get optional transport information."""
20n/a return self._extra.get(name, default)
21n/a
22n/a def is_closing(self):
23n/a """Return True if the transport is closing or closed."""
24n/a raise NotImplementedError
25n/a
26n/a def close(self):
27n/a """Close the transport.
28n/a
29n/a Buffered data will be flushed asynchronously. No more data
30n/a will be received. After all buffered data is flushed, the
31n/a protocol's connection_lost() method will (eventually) called
32n/a with None as its argument.
33n/a """
34n/a raise NotImplementedError
35n/a
36n/a def set_protocol(self, protocol):
37n/a """Set a new protocol."""
38n/a raise NotImplementedError
39n/a
40n/a def get_protocol(self):
41n/a """Return the current protocol."""
42n/a raise NotImplementedError
43n/a
44n/a
45n/aclass ReadTransport(BaseTransport):
46n/a """Interface for read-only transports."""
47n/a
48n/a def pause_reading(self):
49n/a """Pause the receiving end.
50n/a
51n/a No data will be passed to the protocol's data_received()
52n/a method until resume_reading() is called.
53n/a """
54n/a raise NotImplementedError
55n/a
56n/a def resume_reading(self):
57n/a """Resume the receiving end.
58n/a
59n/a Data received will once again be passed to the protocol's
60n/a data_received() method.
61n/a """
62n/a raise NotImplementedError
63n/a
64n/a
65n/aclass WriteTransport(BaseTransport):
66n/a """Interface for write-only transports."""
67n/a
68n/a def set_write_buffer_limits(self, high=None, low=None):
69n/a """Set the high- and low-water limits for write flow control.
70n/a
71n/a These two values control when to call the protocol's
72n/a pause_writing() and resume_writing() methods. If specified,
73n/a the low-water limit must be less than or equal to the
74n/a high-water limit. Neither value can be negative.
75n/a
76n/a The defaults are implementation-specific. If only the
77n/a high-water limit is given, the low-water limit defaults to an
78n/a implementation-specific value less than or equal to the
79n/a high-water limit. Setting high to zero forces low to zero as
80n/a well, and causes pause_writing() to be called whenever the
81n/a buffer becomes non-empty. Setting low to zero causes
82n/a resume_writing() to be called only once the buffer is empty.
83n/a Use of zero for either limit is generally sub-optimal as it
84n/a reduces opportunities for doing I/O and computation
85n/a concurrently.
86n/a """
87n/a raise NotImplementedError
88n/a
89n/a def get_write_buffer_size(self):
90n/a """Return the current size of the write buffer."""
91n/a raise NotImplementedError
92n/a
93n/a def write(self, data):
94n/a """Write some data bytes to the transport.
95n/a
96n/a This does not block; it buffers the data and arranges for it
97n/a to be sent out asynchronously.
98n/a """
99n/a raise NotImplementedError
100n/a
101n/a def writelines(self, list_of_data):
102n/a """Write a list (or any iterable) of data bytes to the transport.
103n/a
104n/a The default implementation concatenates the arguments and
105n/a calls write() on the result.
106n/a """
107n/a data = compat.flatten_list_bytes(list_of_data)
108n/a self.write(data)
109n/a
110n/a def write_eof(self):
111n/a """Close the write end after flushing buffered data.
112n/a
113n/a (This is like typing ^D into a UNIX program reading from stdin.)
114n/a
115n/a Data may still be received.
116n/a """
117n/a raise NotImplementedError
118n/a
119n/a def can_write_eof(self):
120n/a """Return True if this transport supports write_eof(), False if not."""
121n/a raise NotImplementedError
122n/a
123n/a def abort(self):
124n/a """Close the transport immediately.
125n/a
126n/a Buffered data will be lost. No more data will be received.
127n/a The protocol's connection_lost() method will (eventually) be
128n/a called with None as its argument.
129n/a """
130n/a raise NotImplementedError
131n/a
132n/a
133n/aclass Transport(ReadTransport, WriteTransport):
134n/a """Interface representing a bidirectional transport.
135n/a
136n/a There may be several implementations, but typically, the user does
137n/a not implement new transports; rather, the platform provides some
138n/a useful transports that are implemented using the platform's best
139n/a practices.
140n/a
141n/a The user never instantiates a transport directly; they call a
142n/a utility function, passing it a protocol factory and other
143n/a information necessary to create the transport and protocol. (E.g.
144n/a EventLoop.create_connection() or EventLoop.create_server().)
145n/a
146n/a The utility function will asynchronously create a transport and a
147n/a protocol and hook them up by calling the protocol's
148n/a connection_made() method, passing it the transport.
149n/a
150n/a The implementation here raises NotImplemented for every method
151n/a except writelines(), which calls write() in a loop.
152n/a """
153n/a
154n/a
155n/aclass DatagramTransport(BaseTransport):
156n/a """Interface for datagram (UDP) transports."""
157n/a
158n/a def sendto(self, data, addr=None):
159n/a """Send data to the transport.
160n/a
161n/a This does not block; it buffers the data and arranges for it
162n/a to be sent out asynchronously.
163n/a addr is target socket address.
164n/a If addr is None use target address pointed on transport creation.
165n/a """
166n/a raise NotImplementedError
167n/a
168n/a def abort(self):
169n/a """Close the transport immediately.
170n/a
171n/a Buffered data will be lost. No more data will be received.
172n/a The protocol's connection_lost() method will (eventually) be
173n/a called with None as its argument.
174n/a """
175n/a raise NotImplementedError
176n/a
177n/a
178n/aclass SubprocessTransport(BaseTransport):
179n/a
180n/a def get_pid(self):
181n/a """Get subprocess id."""
182n/a raise NotImplementedError
183n/a
184n/a def get_returncode(self):
185n/a """Get subprocess returncode.
186n/a
187n/a See also
188n/a http://docs.python.org/3/library/subprocess#subprocess.Popen.returncode
189n/a """
190n/a raise NotImplementedError
191n/a
192n/a def get_pipe_transport(self, fd):
193n/a """Get transport for pipe with number fd."""
194n/a raise NotImplementedError
195n/a
196n/a def send_signal(self, signal):
197n/a """Send signal to subprocess.
198n/a
199n/a See also:
200n/a docs.python.org/3/library/subprocess#subprocess.Popen.send_signal
201n/a """
202n/a raise NotImplementedError
203n/a
204n/a def terminate(self):
205n/a """Stop the subprocess.
206n/a
207n/a Alias for close() method.
208n/a
209n/a On Posix OSs the method sends SIGTERM to the subprocess.
210n/a On Windows the Win32 API function TerminateProcess()
211n/a is called to stop the subprocess.
212n/a
213n/a See also:
214n/a http://docs.python.org/3/library/subprocess#subprocess.Popen.terminate
215n/a """
216n/a raise NotImplementedError
217n/a
218n/a def kill(self):
219n/a """Kill the subprocess.
220n/a
221n/a On Posix OSs the function sends SIGKILL to the subprocess.
222n/a On Windows kill() is an alias for terminate().
223n/a
224n/a See also:
225n/a http://docs.python.org/3/library/subprocess#subprocess.Popen.kill
226n/a """
227n/a raise NotImplementedError
228n/a
229n/a
230n/aclass _FlowControlMixin(Transport):
231n/a """All the logic for (write) flow control in a mix-in base class.
232n/a
233n/a The subclass must implement get_write_buffer_size(). It must call
234n/a _maybe_pause_protocol() whenever the write buffer size increases,
235n/a and _maybe_resume_protocol() whenever it decreases. It may also
236n/a override set_write_buffer_limits() (e.g. to specify different
237n/a defaults).
238n/a
239n/a The subclass constructor must call super().__init__(extra). This
240n/a will call set_write_buffer_limits().
241n/a
242n/a The user may call set_write_buffer_limits() and
243n/a get_write_buffer_size(), and their protocol's pause_writing() and
244n/a resume_writing() may be called.
245n/a """
246n/a
247n/a def __init__(self, extra=None, loop=None):
248n/a super().__init__(extra)
249n/a assert loop is not None
250n/a self._loop = loop
251n/a self._protocol_paused = False
252n/a self._set_write_buffer_limits()
253n/a
254n/a def _maybe_pause_protocol(self):
255n/a size = self.get_write_buffer_size()
256n/a if size <= self._high_water:
257n/a return
258n/a if not self._protocol_paused:
259n/a self._protocol_paused = True
260n/a try:
261n/a self._protocol.pause_writing()
262n/a except Exception as exc:
263n/a self._loop.call_exception_handler({
264n/a 'message': 'protocol.pause_writing() failed',
265n/a 'exception': exc,
266n/a 'transport': self,
267n/a 'protocol': self._protocol,
268n/a })
269n/a
270n/a def _maybe_resume_protocol(self):
271n/a if (self._protocol_paused and
272n/a self.get_write_buffer_size() <= self._low_water):
273n/a self._protocol_paused = False
274n/a try:
275n/a self._protocol.resume_writing()
276n/a except Exception as exc:
277n/a self._loop.call_exception_handler({
278n/a 'message': 'protocol.resume_writing() failed',
279n/a 'exception': exc,
280n/a 'transport': self,
281n/a 'protocol': self._protocol,
282n/a })
283n/a
284n/a def get_write_buffer_limits(self):
285n/a return (self._low_water, self._high_water)
286n/a
287n/a def _set_write_buffer_limits(self, high=None, low=None):
288n/a if high is None:
289n/a if low is None:
290n/a high = 64*1024
291n/a else:
292n/a high = 4*low
293n/a if low is None:
294n/a low = high // 4
295n/a if not high >= low >= 0:
296n/a raise ValueError('high (%r) must be >= low (%r) must be >= 0' %
297n/a (high, low))
298n/a self._high_water = high
299n/a self._low_water = low
300n/a
301n/a def set_write_buffer_limits(self, high=None, low=None):
302n/a self._set_write_buffer_limits(high=high, low=low)
303n/a self._maybe_pause_protocol()
304n/a
305n/a def get_write_buffer_size(self):
306n/a raise NotImplementedError