[Python-checkins] cpython (3.4): Issue #12523: asynchat.async_chat.push() now raises a TypeError if it doesn't

victor.stinner python-checkins at python.org
Tue Jul 8 00:01:56 CEST 2014


http://hg.python.org/cpython/rev/f8c9dd2626aa
changeset:   91600:f8c9dd2626aa
branch:      3.4
parent:      91598:047da19efdab
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Jul 08 00:00:30 2014 +0200
summary:
  Issue #12523: asynchat.async_chat.push() now raises a TypeError if it doesn't
get a bytes string

files:
  Lib/asynchat.py           |   3 +++
  Lib/test/test_asynchat.py |  16 ++++++++++++++++
  Misc/NEWS                 |   3 +++
  3 files changed, 22 insertions(+), 0 deletions(-)


diff --git a/Lib/asynchat.py b/Lib/asynchat.py
--- a/Lib/asynchat.py
+++ b/Lib/asynchat.py
@@ -181,6 +181,9 @@
         self.close()
 
     def push (self, data):
+        if not isinstance(data, (bytes, bytearray, memoryview)):
+            raise TypeError('data argument must be byte-ish (%r)',
+                            type(data))
         sabs = self.ac_out_buffer_size
         if len(data) > sabs:
             for i in range(0, len(data), sabs):
diff --git a/Lib/test/test_asynchat.py b/Lib/test/test_asynchat.py
--- a/Lib/test/test_asynchat.py
+++ b/Lib/test/test_asynchat.py
@@ -249,6 +249,22 @@
         # (which could still result in the client not having received anything)
         self.assertGreater(len(s.buffer), 0)
 
+    def test_push(self):
+        # Issue #12523: push() should raise a TypeError if it doesn't get
+        # a bytes string
+        s, event = start_echo_server()
+        c = echo_client(b'\n', s.port)
+        data = b'bytes\n'
+        c.push(data)
+        c.push(bytearray(data))
+        c.push(memoryview(data))
+        self.assertRaises(TypeError, c.push, 10)
+        self.assertRaises(TypeError, c.push, 'unicode')
+        c.push(SERVER_QUIT)
+        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
+        s.join(timeout=TIMEOUT)
+        self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes'])
+
 
 class TestAsynchat_WithPoll(TestAsynchat):
     usepoll = True
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
 Library
 -------
 
+- Issue #12523: asynchat.async_chat.push() now raises a TypeError if it doesn't
+  get a bytes string
+
 - Issue #21707: Add missing kwonlyargcount argument to
   ModuleFinder.replace_paths_in_code().
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list