diff --git a/Lib/asynchat.py b/Lib/asynchat.py index f1a5731bae802026920abb1ab46ada8882f2e2d7..0cc91a8d574f99d82b7ac7e8db4ae53ba9f2db5a 100644 --- a/Lib/asynchat.py +++ b/Lib/asynchat.py @@ -181,6 +181,9 @@ class async_chat (asyncore.dispatcher): 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 index f93a52d8c98973add312f5eec7e07777ff19bb4c..fd50c033e51503a6cd1a8f8cc0ab69dd0f1171c5 100644 --- a/Lib/test/test_asynchat.py +++ b/Lib/test/test_asynchat.py @@ -249,6 +249,22 @@ class TestAsynchat(unittest.TestCase): # (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 index caa30ed74f308d2db4361d4c74e18ba63b7ba683..b7e73e9d048268248c380c1bf897b8b2ed2e7395 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -27,6 +27,9 @@ Core and Builtins 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().