Fix Python 3.14 support

By lazily initializing loop-related properties.
Closes #4712.
This commit is contained in:
Lonami Exo 2025-10-18 20:53:14 +02:00
parent c92c1ae6b1
commit baf35e9e7e
2 changed files with 27 additions and 21 deletions

View File

@ -320,24 +320,6 @@ class TelegramBaseClient(abc.ABC):
self.api_id = int(api_id) self.api_id = int(api_id)
self.api_hash = api_hash self.api_hash = api_hash
# Current proxy implementation requires `sock_connect`, and some
# event loops lack this method. If the current loop is missing it,
# bail out early and suggest an alternative.
#
# TODO A better fix is obviously avoiding the use of `sock_connect`
#
# See https://github.com/LonamiWebs/Telethon/issues/1337 for details.
if not callable(getattr(self.loop, 'sock_connect', None)):
raise TypeError(
'Event loop of type {} lacks `sock_connect`, which is needed to use proxies.\n\n'
'Change the event loop in use to use proxies:\n'
'# https://github.com/LonamiWebs/Telethon/issues/1337\n'
'import asyncio\n'
'asyncio.set_event_loop(asyncio.SelectorEventLoop())'.format(
self.loop.__class__.__name__
)
)
if local_addr is not None: if local_addr is not None:
if use_ipv6 is False and ':' in local_addr: if use_ipv6 is False and ':' in local_addr:
raise TypeError( raise TypeError(
@ -537,6 +519,24 @@ class TelegramBaseClient(abc.ABC):
elif self._loop != helpers.get_running_loop(): elif self._loop != helpers.get_running_loop():
raise RuntimeError('The asyncio event loop must not change after connection (see the FAQ for details)') raise RuntimeError('The asyncio event loop must not change after connection (see the FAQ for details)')
# Current proxy implementation requires `sock_connect`, and some
# event loops lack this method. If the current loop is missing it,
# bail out early and suggest an alternative.
#
# TODO A better fix is obviously avoiding the use of `sock_connect`
#
# See https://github.com/LonamiWebs/Telethon/issues/1337 for details.
if not callable(getattr(self._loop, 'sock_connect', None)):
raise TypeError(
'Event loop of type {} lacks `sock_connect`, which is needed to use proxies.\n\n'
'Change the event loop in use to use proxies:\n'
'# https://github.com/LonamiWebs/Telethon/issues/1337\n'
'import asyncio\n'
'asyncio.set_event_loop(asyncio.SelectorEventLoop())'.format(
self._loop.__class__.__name__
)
)
# ':' in session.server_address is True if it's an IPv6 address # ':' in session.server_address is True if it's an IPv6 address
if (not self.session.server_address or if (not self.session.server_address or
(':' in self.session.server_address) != self._use_ipv6): (':' in self.session.server_address) != self._use_ipv6):

View File

@ -70,8 +70,7 @@ class MTProtoSender:
# pending futures should be cancelled. # pending futures should be cancelled.
self._user_connected = False self._user_connected = False
self._reconnecting = False self._reconnecting = False
self._disconnected = helpers.get_running_loop().create_future() self.__disconnected = None
self._disconnected.set_result(None)
# We need to join the loops upon disconnection # We need to join the loops upon disconnection
self._send_loop_handle = None self._send_loop_handle = None
@ -217,6 +216,13 @@ class MTProtoSender:
""" """
return asyncio.shield(self._disconnected) return asyncio.shield(self._disconnected)
@property
def _disconnected(self):
if self.__disconnected is None:
self.__disconnected = helpers.get_running_loop().create_future()
self.__disconnected.set_result(None)
return self.__disconnected
# Private methods # Private methods
async def _connect(self): async def _connect(self):
@ -274,7 +280,7 @@ class MTProtoSender:
# or errors after which the sender cannot continue such # or errors after which the sender cannot continue such
# as failing to reconnect or any unexpected error. # as failing to reconnect or any unexpected error.
if self._disconnected.done(): if self._disconnected.done():
self._disconnected = loop.create_future() self.__disconnected = loop.create_future()
self._log.info('Connection to %s complete!', self._connection) self._log.info('Connection to %s complete!', self._connection)