websocket.*: [PEP 8] Use conventional line-length limit

Line length is set to 80 characters; tests are excluded.
This commit is contained in:
Allan Lewis
2016-04-27 12:21:25 +01:00
parent c5afde8d77
commit 9f138c94e7
7 changed files with 40 additions and 21 deletions

View File

@@ -145,7 +145,8 @@ class ABNF(object):
if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]): if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]):
raise WebSocketProtocolException("Invalid close frame.") raise WebSocketProtocolException("Invalid close frame.")
code = 256*six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2]) code = 256 * \
six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2])
if not self._is_valid_close_status(code): if not self._is_valid_close_status(code):
raise WebSocketProtocolException("Invalid close opcode.") raise WebSocketProtocolException("Invalid close opcode.")
@@ -155,8 +156,8 @@ class ABNF(object):
def __str__(self): def __str__(self):
return "fin=" + str(self.fin) \ return "fin=" + str(self.fin) \
+ " opcode=" + str(self.opcode) \ + " opcode=" + str(self.opcode) \
+ " data=" + str(self.data) + " data=" + str(self.data)
@staticmethod @staticmethod
def create_frame(data, opcode, fin=1): def create_frame(data, opcode, fin=1):
@@ -344,7 +345,8 @@ class frame_buffer(object):
# fragmenting the heap -- the number of bytes recv() actually # fragmenting the heap -- the number of bytes recv() actually
# reads is limited by socket buffer and is relatively small, # reads is limited by socket buffer and is relatively small,
# yet passing large numbers repeatedly causes lots of large # yet passing large numbers repeatedly causes lots of large
# buffers allocated and then shrunk, which results in fragmentation. # buffers allocated and then shrunk, which results in
# fragmentation.
bytes_ = self.recv(min(16384, shortage)) bytes_ = self.recv(min(16384, shortage))
self.recv_buffer.append(bytes_) self.recv_buffer.append(bytes_)
shortage -= len(bytes_) shortage -= len(bytes_)
@@ -392,6 +394,7 @@ class continuous_frame(object):
self.cont_data = None self.cont_data = None
frame.data = data[1] frame.data = data[1]
if not self.fire_cont_frame and data[0] == ABNF.OPCODE_TEXT and not self.skip_utf8_validation and not validate_utf8(frame.data): if not self.fire_cont_frame and data[0] == ABNF.OPCODE_TEXT and not self.skip_utf8_validation and not validate_utf8(frame.data):
raise WebSocketPayloadException("cannot decode: " + repr(frame.data)) raise WebSocketPayloadException(
"cannot decode: " + repr(frame.data))
return [data[0], frame] return [data[0], frame]

View File

@@ -114,7 +114,8 @@ class WebSocketApp(object):
""" """
if not self.sock or self.sock.send(data, opcode) == 0: if not self.sock or self.sock.send(data, opcode) == 0:
raise WebSocketConnectionClosedException("Connection is already closed.") raise WebSocketConnectionClosedException(
"Connection is already closed.")
def close(self): def close(self):
""" """
@@ -184,12 +185,14 @@ class WebSocketApp(object):
if ping_interval: if ping_interval:
event = threading.Event() event = threading.Event()
thread = threading.Thread(target=self._send_ping, args=(ping_interval, event)) thread = threading.Thread(
target=self._send_ping, args=(ping_interval, event))
thread.setDaemon(True) thread.setDaemon(True)
thread.start() thread.start()
while self.sock.connected: while self.sock.connected:
r, w, e = select.select((self.sock.sock, ), (), (), ping_timeout) r, w, e = select.select(
(self.sock.sock, ), (), (), ping_timeout)
if not self.keep_running: if not self.keep_running:
break break
@@ -204,8 +207,10 @@ class WebSocketApp(object):
self.last_pong_tm = time.time() self.last_pong_tm = time.time()
self._callback(self.on_pong, frame.data) self._callback(self.on_pong, frame.data)
elif op_code == ABNF.OPCODE_CONT and self.on_cont_message: elif op_code == ABNF.OPCODE_CONT and self.on_cont_message:
self._callback(self.on_data, data, frame.opcode, frame.fin) self._callback(self.on_data, data,
self._callback(self.on_cont_message, frame.data, frame.fin) frame.opcode, frame.fin)
self._callback(self.on_cont_message,
frame.data, frame.fin)
else: else:
data = frame.data data = frame.data
if six.PY3 and frame.opcode == ABNF.OPCODE_TEXT: if six.PY3 and frame.opcode == ABNF.OPCODE_TEXT:

View File

@@ -91,7 +91,8 @@ class WebSocket(object):
self.get_mask_key = get_mask_key self.get_mask_key = get_mask_key
# These buffer over the build-up of a single frame. # These buffer over the build-up of a single frame.
self.frame_buffer = frame_buffer(self._recv, skip_utf8_validation) self.frame_buffer = frame_buffer(self._recv, skip_utf8_validation)
self.cont_frame = continuous_frame(fire_cont_frame, skip_utf8_validation) self.cont_frame = continuous_frame(
fire_cont_frame, skip_utf8_validation)
if enable_multithread: if enable_multithread:
self.lock = threading.Lock() self.lock = threading.Lock()
@@ -325,7 +326,8 @@ class WebSocket(object):
if not frame: if not frame:
# handle error: # handle error:
# 'NoneType' object has no attribute 'opcode' # 'NoneType' object has no attribute 'opcode'
raise WebSocketProtocolException("Not a valid frame %s" % frame) raise WebSocketProtocolException(
"Not a valid frame %s" % frame)
elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT): elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT):
self.cont_frame.validate(frame) self.cont_frame.validate(frame)
self.cont_frame.add(frame) self.cont_frame.add(frame)
@@ -340,7 +342,8 @@ class WebSocket(object):
if len(frame.data) < 126: if len(frame.data) < 126:
self.pong(frame.data) self.pong(frame.data)
else: else:
raise WebSocketProtocolException("Ping message is too long") raise WebSocketProtocolException(
"Ping message is too long")
if control_frame: if control_frame:
return frame.opcode, frame return frame.opcode, frame
elif frame.opcode == ABNF.OPCODE_PONG: elif frame.opcode == ABNF.OPCODE_PONG:
@@ -385,7 +388,8 @@ class WebSocket(object):
try: try:
self.connected = False self.connected = False
self.send(struct.pack('!H', status) + reason, ABNF.OPCODE_CLOSE) self.send(struct.pack('!H', status) +
reason, ABNF.OPCODE_CLOSE)
sock_timeout = self.sock.gettimeout() sock_timeout = self.sock.gettimeout()
self.sock.settimeout(timeout) self.sock.settimeout(timeout)
try: try:

View File

@@ -75,5 +75,6 @@ class WebSocketBadStatusException(WebSocketException):
""" """
def __init__(self, message, status_code): def __init__(self, message, status_code):
super(WebSocketBadStatusException, self).__init__(message % status_code) super(WebSocketBadStatusException, self).__init__(
message % status_code)
self.status_code = status_code self.status_code = status_code

View File

@@ -60,7 +60,8 @@ def connect(url, options, proxy, socket):
if socket: if socket:
return socket, (hostname, port, resource) return socket, (hostname, port, resource)
addrinfo_list, need_tunnel, auth = _get_addrinfo_list(hostname, port, is_secure, proxy) addrinfo_list, need_tunnel, auth = _get_addrinfo_list(
hostname, port, is_secure, proxy)
if not addrinfo_list: if not addrinfo_list:
raise WebSocketException( raise WebSocketException(
"Host not found.: " + hostname + ":" + str(port)) "Host not found.: " + hostname + ":" + str(port))
@@ -88,7 +89,8 @@ def _get_addrinfo_list(hostname, port, is_secure, proxy):
phost, pport, pauth = get_proxy_info( phost, pport, pauth = get_proxy_info(
hostname, is_secure, proxy.host, proxy.port, proxy.auth, proxy.no_proxy) hostname, is_secure, proxy.host, proxy.port, proxy.auth, proxy.no_proxy)
if not phost: if not phost:
addrinfo_list = socket.getaddrinfo(hostname, port, 0, 0, socket.SOL_TCP) addrinfo_list = socket.getaddrinfo(
hostname, port, 0, 0, socket.SOL_TCP)
return addrinfo_list, False, None return addrinfo_list, False, None
else: else:
pport = pport and pport or 80 pport = pport and pport or 80
@@ -140,7 +142,8 @@ def _wrap_sni_socket(sock, sslopt, hostname, check_hostname):
sslopt.get('keyfile', None), sslopt.get('keyfile', None),
sslopt.get('password', None), sslopt.get('password', None),
) )
# see https://github.com/liris/websocket-client/commit/b96a2e8fa765753e82eea531adb19716b52ca3ca#commitcomment-10803153 # see
# https://github.com/liris/websocket-client/commit/b96a2e8fa765753e82eea531adb19716b52ca3ca#commitcomment-10803153
context.verify_mode = sslopt['cert_reqs'] context.verify_mode = sslopt['cert_reqs']
if HAVE_CONTEXT_CHECK_HOSTNAME: if HAVE_CONTEXT_CHECK_HOSTNAME:
context.check_hostname = check_hostname context.check_hostname = check_hostname
@@ -166,7 +169,8 @@ def _ssl_socket(sock, user_sslopt, hostname):
os.path.dirname(__file__), "cacert.pem") os.path.dirname(__file__), "cacert.pem")
if os.path.isfile(certPath) and user_sslopt.get('ca_certs', None) is None: if os.path.isfile(certPath) and user_sslopt.get('ca_certs', None) is None:
sslopt['ca_certs'] = certPath sslopt['ca_certs'] = certPath
check_hostname = sslopt["cert_reqs"] != ssl.CERT_NONE and sslopt.pop('check_hostname', True) check_hostname = sslopt["cert_reqs"] != ssl.CERT_NONE and sslopt.pop(
'check_hostname', True)
if _can_use_sni(): if _can_use_sni():
sock = _wrap_sni_socket(sock, sslopt, hostname, check_hostname) sock = _wrap_sni_socket(sock, sslopt, hostname, check_hostname)

View File

@@ -89,7 +89,8 @@ def recv(sock, bufsize):
raise raise
if not bytes_: if not bytes_:
raise WebSocketConnectionClosedException("Connection is already closed.") raise WebSocketConnectionClosedException(
"Connection is already closed.")
return bytes_ return bytes_

View File

@@ -71,7 +71,8 @@ except ImportError:
def _decode(state, codep, ch): def _decode(state, codep, ch):
tp = _UTF8D[ch] tp = _UTF8D[ch]
codep = (ch & 0x3f) | (codep << 6) if (state != _UTF8_ACCEPT) else (0xff >> tp) & ch codep = (ch & 0x3f) | (codep << 6) if (
state != _UTF8_ACCEPT) else (0xff >> tp) & ch
state = _UTF8D[256 + state + tp] state = _UTF8D[256 + state + tp]
return state, codep return state, codep