Add type checking when sendning data

We use this in various ways so add an early check to make things clear
in case anything is called incorrectly.
This commit is contained in:
Pierre Ossman 2020-09-30 14:53:19 +02:00
parent d72ace2ae6
commit 6caf23c067
1 changed files with 9 additions and 0 deletions

View File

@ -436,6 +436,9 @@ class WebSocket(object):
WebSocketWantWriteError can be raised if there is insufficient
space in the underlying socket.
"""
if not isinstance(msg, bytes):
raise TypeError
if not self._sent_close:
# Only called to flush?
self._sendmsg(0x2, msg)
@ -445,10 +448,16 @@ class WebSocket(object):
def ping(self, data=''.encode('ascii')):
"""Write a ping message to the WebSocket."""
if not isinstance(data, bytes):
raise TypeError
self._sendmsg(0x9, data)
def pong(self, data=''.encode('ascii')):
"""Write a pong message to the WebSocket."""
if not isinstance(data, bytes):
raise TypeError
self._sendmsg(0xA, data)
def shutdown(self, how, code=1000, reason=None):