]> granicus.if.org Git - python/commitdiff
asyncio: Add Transport.is_closing()
authorYury Selivanov <yselivanov@sprymix.com>
Mon, 16 Nov 2015 17:43:21 +0000 (12:43 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Mon, 16 Nov 2015 17:43:21 +0000 (12:43 -0500)
See https://github.com/python/asyncio/pull/291 for details.

Lib/asyncio/base_subprocess.py
Lib/asyncio/proactor_events.py
Lib/asyncio/selector_events.py
Lib/asyncio/sslproto.py
Lib/asyncio/streams.py
Lib/asyncio/transports.py
Lib/asyncio/unix_events.py
Lib/test/test_asyncio/test_proactor_events.py
Lib/test/test_asyncio/test_selector_events.py
Lib/test/test_asyncio/test_subprocess.py
Lib/test/test_asyncio/test_unix_events.py

index 6851cd2b88e30740aa28cc5c52fa08fd65741912..73425d9bbcc9640b923ce7960a0412400cbf0224 100644 (file)
@@ -87,6 +87,9 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
     def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
         raise NotImplementedError
 
+    def is_closing(self):
+        return self._closed
+
     def close(self):
         if self._closed:
             return
index abe4c129c9bd579ab386ce63d12bc845956d7439..9c514c8345de3bf030340c3884ea108c26de777b 100644 (file)
@@ -65,6 +65,9 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
     def _set_extra(self, sock):
         self._extra['pipe'] = sock
 
+    def is_closing(self):
+        return self._closing
+
     def close(self):
         if self._closing:
             return
index 0060912219e9cf8f6c6142797559ef6410e2826c..236f7b36a8d36698b41f447120173531f633da2c 100644 (file)
@@ -556,6 +556,9 @@ class _SelectorTransport(transports._FlowControlMixin,
     def abort(self):
         self._force_close(None)
 
+    def is_closing(self):
+        return self._closing
+
     def close(self):
         if self._closing:
             return
index 9e08b6f88bcc2f570263a6f77b9968458e4f3c84..dde980b68f835e73e53e2c10cbe8f126946aea53 100644 (file)
@@ -304,6 +304,9 @@ class _SSLProtocolTransport(transports._FlowControlMixin,
         """Get optional transport information."""
         return self._ssl_protocol._get_extra_info(name, default)
 
+    def is_closing(self):
+        return self._closed
+
     def close(self):
         """Close the transport.
 
index 64d10203d248182105a71d3bafb3954629026a88..6b5e96aea2c9caa559393f015ff1124a7eb41b0b 100644 (file)
@@ -302,7 +302,7 @@ class StreamWriter:
             if exc is not None:
                 raise exc
         if self._transport is not None:
-            if self._transport._closing:
+            if self._transport.is_closing():
                 # Yield to the event loop so connection_lost() may be
                 # called.  Without this, _drain_helper() would return
                 # immediately, and code that calls
index 03099e3e54129c5aee5ecce57d539063a89ef617..9a6d9197d9a0aa350b8f00b8bfe4e1235abe50f5 100644 (file)
@@ -19,6 +19,10 @@ class BaseTransport:
         """Get optional transport information."""
         return self._extra.get(name, default)
 
+    def is_closing(self):
+        """Return True if the transport is closing or closed."""
+        raise NotImplementedError
+
     def close(self):
         """Close the transport.
 
index bf3b0844fda842a2148df8f8aac52473fa019bbb..f75e89f3175e706853d544c855e3273331db97b9 100644 (file)
@@ -364,6 +364,9 @@ class _UnixReadPipeTransport(transports.ReadTransport):
     def resume_reading(self):
         self._loop.add_reader(self._fileno, self._read_ready)
 
+    def is_closing(self):
+        return self._closing
+
     def close(self):
         if not self._closing:
             self._close(None)
@@ -548,6 +551,9 @@ class _UnixWritePipeTransport(transports._FlowControlMixin,
             self._loop.remove_reader(self._fileno)
             self._loop.call_soon(self._call_connection_lost, None)
 
+    def is_closing(self):
+        return self._closing
+
     def close(self):
         if self._pipe is not None and not self._closing:
             # write_eof is all what we needed to close the write pipe
index fcd9ab1e18f68943cfb83415b368636f2a518a41..5a0f0881e4c65c2f6c0545982bb49c85aee812f2 100644 (file)
@@ -204,7 +204,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
         tr.close()
         test_utils.run_briefly(self.loop)
         self.protocol.connection_lost.assert_called_with(None)
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertEqual(tr._conn_lost, 1)
 
         self.protocol.connection_lost.reset_mock()
@@ -298,7 +298,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
             self.loop, self.sock, self.protocol)
         self.assertTrue(tr.can_write_eof())
         tr.write_eof()
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.loop._run_once()
         self.assertTrue(self.sock.close.called)
         tr.close()
@@ -309,7 +309,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
         tr._loop._proactor.send.return_value = f
         tr.write(b'data')
         tr.write_eof()
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertFalse(self.sock.shutdown.called)
         tr._loop._proactor.send.assert_called_with(self.sock, b'data')
         f.set_result(4)
index f0fcdd22ae626bd45855dd83e4176785fc6f52f3..135b5abf1013f864e8c708e10df15b4bed2141ce 100644 (file)
@@ -698,7 +698,7 @@ class SelectorTransportTests(test_utils.TestCase):
         tr = self.create_transport()
         tr.close()
 
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertEqual(1, self.loop.remove_reader_count[7])
         self.protocol.connection_lost(None)
         self.assertEqual(tr._conn_lost, 1)
@@ -723,7 +723,7 @@ class SelectorTransportTests(test_utils.TestCase):
         self.loop.add_writer(7, mock.sentinel)
         tr._force_close(None)
 
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertEqual(tr._buffer, list_to_buffer())
         self.assertFalse(self.loop.readers)
         self.assertFalse(self.loop.writers)
@@ -1436,7 +1436,7 @@ class SelectorSslTransportTests(test_utils.TestCase):
         tr = self._make_one()
         tr.close()
 
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertEqual(1, self.loop.remove_reader_count[1])
         self.assertEqual(tr._conn_lost, 1)
 
index 38f0ceeb1f2bd561862be5a10c3360fef41ce5ec..e90f17dda63a798cb56fb3c817151dc83a71a75b 100644 (file)
@@ -61,7 +61,7 @@ class SubprocessTransportTests(test_utils.TestCase):
         self.assertTrue(protocol.connection_lost.called)
         self.assertEqual(protocol.connection_lost.call_args[0], (None,))
 
-        self.assertFalse(transport._closed)
+        self.assertFalse(transport.is_closing())
         self.assertIsNone(transport._loop)
         self.assertIsNone(transport._proc)
         self.assertIsNone(transport._protocol)
index dc0835c527d1284cbddc40496f36a6ec967fb70b..22dc6880360a41180c58d58b4f30d35b0837cee4 100644 (file)
@@ -440,7 +440,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
         tr = self.read_pipe_transport()
         err = object()
         tr._close(err)
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertFalse(self.loop.readers)
         test_utils.run_briefly(self.loop)
         self.protocol.connection_lost.assert_called_with(err)
@@ -598,7 +598,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
         tr._read_ready()
         self.assertFalse(self.loop.readers)
         self.assertFalse(self.loop.writers)
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         test_utils.run_briefly(self.loop)
         self.protocol.connection_lost.assert_called_with(None)
 
@@ -658,7 +658,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
         self.assertFalse(self.loop.writers)
         self.assertFalse(self.loop.readers)
         self.assertEqual([], tr._buffer)
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         m_logexc.assert_called_with(
             test_utils.MockPattern(
                 'Fatal write error on pipe transport'
@@ -694,7 +694,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
         self.assertFalse(self.loop.readers)
         self.assertFalse(self.loop.writers)
         self.assertEqual([], tr._buffer)
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         test_utils.run_briefly(self.loop)
         self.protocol.connection_lost.assert_called_with(None)
 
@@ -743,7 +743,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
     def test_write_eof(self):
         tr = self.write_pipe_transport()
         tr.write_eof()
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertFalse(self.loop.readers)
         test_utils.run_briefly(self.loop)
         self.protocol.connection_lost.assert_called_with(None)
@@ -752,7 +752,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
         tr = self.write_pipe_transport()
         tr._buffer = [b'data']
         tr.write_eof()
-        self.assertTrue(tr._closing)
+        self.assertTrue(tr.is_closing())
         self.assertFalse(self.protocol.connection_lost.called)