Lawouach / WebSocket-for-Python

WebSocket client and server library for Python 2 and 3 as well as PyPy (ws4py 0.5.1)
https://ws4py.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
1.12k stars 289 forks source link

TypeError from websocket.py #292

Open sslupsky opened 1 week ago

sslupsky commented 1 week ago

Recent commit appears to be causing a TypeError: https://github.com/Lawouach/WebSocket-for-Python/commit/cb60792322c55e22896ff8955b220641895dad46

Here is the error:

Exception in thread WebSocketClient:
Traceback (most recent call last):
  File "/Users/stevenslupsky/.local/share/mise/installs/python/3.8.18/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/stevenslupsky/.local/share/mise/installs/python/3.8.18/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/stevenslupsky/.local/share/mise/installs/python/3.8.18/lib/python3.8/site-packages/ws4py/websocket.py", line 528, in run
    if not self.once():
  File "/Users/stevenslupsky/.local/share/mise/installs/python/3.8.18/lib/python3.8/site-packages/ws4py/websocket.py", line 410, in once
    if not self.process(self.buf[:requested]):
  File "/Users/stevenslupsky/.local/share/mise/installs/python/3.8.18/lib/python3.8/site-packages/ws4py/websocket.py", line 465, in process
    logger.debug("Closing message received (%d): %s" % (s.closing.code, s.closing.reason.decode() if isinstance(s.closing.reason, bytes) else s.closing.reason))
TypeError: isinstance() arg 2 must be a type or tuple of types

See also: https://github.com/klattimer/LGWebOSRemote/issues/172

Seems like the error might be from this line: https://github.com/Lawouach/WebSocket-for-Python/blob/cb60792322c55e22896ff8955b220641895dad46/ws4py/websocket.py#L410

The second parameter is null.

auvipy commented 1 week ago

thanks, i got them in the CI as well test_parse_unix_schemes (test.test_client.BasicClientTest) ... /home/runner/work/WebSocket-for-Python/WebSocket-for-Python/test/test_client.py:37: ResourceWarning: unclosed c = WebSocketBaseClient(url="wss+unix:///my.socket") /opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/unittest/case.py:605: ResourceWarning: unclosed testMethod() ok test_parse_ws_scheme (test.test_client.BasicClientTest) ... /opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/unittest/case.py:605: ResourceWarning: unclosed <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 0)> testMethod() ok test_parse_ws_scheme_when_missing_resource (test.test_client.BasicClientTest) ... ok test_parse_ws_scheme_with_port (test.test_client.BasicClientTest) ... ok test_parse_ws_scheme_with_query_string (test.test_client.BasicClientTest) ... ok test_parse_wss_scheme (test.test_client.BasicClientTest) ... ok test_parse_wss_scheme_when_missing_resource (test.test_client.BasicClientTest) ... ok test_parse_wss_scheme_with_port (test.test_client.BasicClientTest) ... ok test_parse_wss_scheme_with_query_string (test.test_client.BasicClientTest) ... ok test_thread_is_started_once_connected (test.test_client.ThreadedClientTest) ... Exception in thread WebSocketClient: Traceback (most recent call last): File "/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "/home/runner/work/WebSocket-for-Python/WebSocket-for-Python/ws4py/websocket.py", line 528, in run if not self.once(): File "/home/runner/work/WebSocket-for-Python/WebSocket-for-Python/ws4py/websocket.py", line 410, in once if not self.process(self.buf[:requested]): File "/home/runner/work/WebSocket-for-Python/WebSocket-for-Python/ws4py/websocket.py", line 465, in process logger.debug("Closing message received (%d): %s" % (s.closing.code, s.closing.reason.decode() if isinstance(s.closing.reason, bytes) else s.closing.reason)) TypeError: isinstance() arg 2 must be a type or tuple of types

ok test_thread_is_started_once_connected_secure (test.test_client.ThreadedClientTest) Same as the above test, but with SSL socket ... Exception in thread WebSocketClient: Traceback (most recent call last): File "/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "/home/runner/work/WebSocket-for-Python/WebSocket-for-Python/ws4py/websocket.py", line 528, in run if not self.once(): File "/home/runner/work/WebSocket-for-Python/WebSocket-for-Python/ws4py/websocket.py", line 410, in once if not self.process(self.buf[:requested]): File "/home/runner/work/WebSocket-for-Python/WebSocket-for-Python/ws4py/websocket.py", line 465, in process logger.debug("Closing message received (%d): %s" % (s.closing.code, s.closing.reason.decode() if isinstance(s.closing.reason, bytes) else s.closing.reason)) TypeError: isinstance() arg 2 must be a type or tuple of types

jolaf commented 1 week ago

I wonder why bytes is not recognized as a type.

Maybe it's overridden by some local variable?

jolaf commented 1 week ago

The second parameter is null.

This should not cause any problems by itself.

sslupsky commented 1 week ago

I think the problem is that when process() is called at line 410, it makes the call without including the second parameter, namely bytes and the definition of process() does not define the default argument. So, there is no "bytes" object (it is None).

jolaf commented 1 week ago

The problem is process() second argument is named bytes, which is Python name for built-in type. Of course, isinstance(anything, bytes) would fail in this situation, because bytes is not a type.

To fix this problem, the process() argument should be renamed to something more reasonable, like data.

jolaf commented 22 hours ago

It looks like this issue is fixed and can be closed now.