[utils] WebSocketsWrapper: Ignore warnings at websockets instantiation

This also fixes crash caused by moving asyncio to .compat.

Authored by: Lesmiscore
Thanks: J.Chung at Discord (581418557871620106)
This commit is contained in:
Lesmiscore 2022-04-25 00:44:30 +09:00
parent 96b49af01c
commit acbc642250
No known key found for this signature in database
GPG Key ID: 0EC2B52CF86236FF
1 changed files with 13 additions and 6 deletions

View File

@ -36,6 +36,7 @@ import tempfile
import time import time
import traceback import traceback
import urllib.parse import urllib.parse
import warnings
import xml.etree.ElementTree import xml.etree.ElementTree
import zlib import zlib
@ -5221,17 +5222,23 @@ class WebSocketsWrapper():
pool = None pool = None
def __init__(self, url, headers=None, connect=True): def __init__(self, url, headers=None, connect=True):
self.loop = asyncio.events.new_event_loop() self.loop = asyncio.new_event_loop()
self.conn = websockets.connect( with warnings.catch_warnings():
url, extra_headers=headers, ping_interval=None, warnings.simplefilter("ignore")
close_timeout=float('inf'), loop=self.loop, ping_timeout=float('inf')) # https://github.com/aaugustin/websockets/blob/9c87d43f1d7bbf6847350087aae74fd35f73a642/src/websockets/legacy/client.py#L480
# the reason to keep giving `loop` parameter: we aren't in async function
self.conn = websockets.connect(
url, extra_headers=headers, ping_interval=None,
close_timeout=float('inf'), loop=self.loop, ping_timeout=float('inf'))
if connect: if connect:
self.__enter__() self.__enter__()
atexit.register(self.__exit__, None, None, None) atexit.register(self.__exit__, None, None, None)
def __enter__(self): def __enter__(self):
if not self.pool: if not self.pool:
self.pool = self.run_with_loop(self.conn.__aenter__(), self.loop) with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.pool = self.run_with_loop(self.conn.__aenter__(), self.loop)
return self return self
def send(self, *args): def send(self, *args):
@ -5251,7 +5258,7 @@ class WebSocketsWrapper():
# for contributors: If there's any new library using asyncio needs to be run in non-async, move these function out of this class # for contributors: If there's any new library using asyncio needs to be run in non-async, move these function out of this class
@staticmethod @staticmethod
def run_with_loop(main, loop): def run_with_loop(main, loop):
if not asyncio.coroutines.iscoroutine(main): if not asyncio.iscoroutine(main):
raise ValueError(f'a coroutine was expected, got {main!r}') raise ValueError(f'a coroutine was expected, got {main!r}')
try: try: