changeset: 91600:f8c9dd2626aa branch: 3.4 parent: 91598:047da19efdab user: Victor Stinner date: Tue Jul 08 00:00:30 2014 +0200 files: Lib/asynchat.py Lib/test/test_asynchat.py Misc/NEWS description: Issue #12523: asynchat.async_chat.push() now raises a TypeError if it doesn't get a bytes string diff -r 047da19efdab -r f8c9dd2626aa Lib/asynchat.py --- a/Lib/asynchat.py Mon Jul 07 23:06:15 2014 +0200 +++ b/Lib/asynchat.py Tue Jul 08 00:00:30 2014 +0200 @@ -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 -r 047da19efdab -r f8c9dd2626aa Lib/test/test_asynchat.py --- a/Lib/test/test_asynchat.py Mon Jul 07 23:06:15 2014 +0200 +++ b/Lib/test/test_asynchat.py Tue Jul 08 00:00:30 2014 +0200 @@ -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 -r 047da19efdab -r f8c9dd2626aa Misc/NEWS --- a/Misc/NEWS Mon Jul 07 23:06:15 2014 +0200 +++ b/Misc/NEWS Tue Jul 08 00:00:30 2014 +0200 @@ -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().