Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8209da

Browse files
committed
(Merge 3.4) Issue #12523: asynchat.async_chat.push() now raises a TypeError if
it doesn't get a bytes string
2 parents ac7d80c + d9e810a commit e8209da

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

Lib/asynchat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ def handle_close (self):
181181
self.close()
182182

183183
def push (self, data):
184+
if not isinstance(data, (bytes, bytearray, memoryview)):
185+
raise TypeError('data argument must be byte-ish (%r)',
186+
type(data))
184187
sabs = self.ac_out_buffer_size
185188
if len(data) > sabs:
186189
for i in range(0, len(data), sabs):

Lib/test/test_asynchat.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ def test_close_when_done(self):
250250
# (which could still result in the client not having received anything)
251251
self.assertGreater(len(s.buffer), 0)
252252

253+
def test_push(self):
254+
# Issue #12523: push() should raise a TypeError if it doesn't get
255+
# a bytes string
256+
s, event = start_echo_server()
257+
c = echo_client(b'\n', s.port)
258+
data = b'bytes\n'
259+
c.push(data)
260+
c.push(bytearray(data))
261+
c.push(memoryview(data))
262+
self.assertRaises(TypeError, c.push, 10)
263+
self.assertRaises(TypeError, c.push, 'unicode')
264+
c.push(SERVER_QUIT)
265+
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
266+
s.join(timeout=TIMEOUT)
267+
self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes'])
268+
253269

254270
class TestAsynchat_WithPoll(TestAsynchat):
255271
usepoll = True

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Core and Builtins
108108
Library
109109
-------
110110

111+
- Issue #12523: asynchat.async_chat.push() now raises a TypeError if it doesn't
112+
get a bytes string
113+
111114
- Issue #21707: Add missing kwonlyargcount argument to
112115
ModuleFinder.replace_paths_in_code().
113116

0 commit comments

Comments
 (0)