From 908375ac42cf1740d3b661db0fda838da9e03180 Mon Sep 17 00:00:00 2001 From: Alfian Pangetsu <50546233+salismazaya@users.noreply.github.com> Date: Mon, 3 Oct 2022 00:05:11 +0700 Subject: [PATCH] Fix get_running_loop usage in Python3.6 (#3941) Closes #3939. --- telethon/_updates/messagebox.py | 9 +++++---- telethon/client/updates.py | 4 +++- telethon/helpers.py | 7 +++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/telethon/_updates/messagebox.py b/telethon/_updates/messagebox.py index 536172a3..da10100f 100644 --- a/telethon/_updates/messagebox.py +++ b/telethon/_updates/messagebox.py @@ -22,6 +22,7 @@ import time from enum import Enum from .session import SessionState, ChannelState from ..tl import types as tl, functions as fn +from ..helpers import get_running_loop # Telegram sends `seq` equal to `0` when "it doesn't matter", so we use that value too. @@ -53,7 +54,7 @@ ENTRY_SECRET = object() _sentinel = object() def next_updates_deadline(): - return asyncio.get_running_loop().time() + NO_UPDATES_TIMEOUT + return get_running_loop().time() + NO_UPDATES_TIMEOUT class GapError(ValueError): @@ -237,7 +238,7 @@ class MessageBox: If a deadline expired, the corresponding entries will be marked as needing to get its difference. While there are entries pending of getting their difference, this method returns the current instant. """ - now = asyncio.get_running_loop().time() + now = get_running_loop().time() if self.getting_diff_for: return now @@ -283,7 +284,7 @@ class MessageBox: # Convenience to reset a channel's deadline, with optional timeout. def reset_channel_deadline(self, channel_id, timeout): - self.reset_deadline(channel_id, asyncio.get_running_loop().time() + (timeout or NO_UPDATES_TIMEOUT)) + self.reset_deadline(channel_id, get_running_loop().time() + (timeout or NO_UPDATES_TIMEOUT)) # Reset all the deadlines in `reset_deadlines_for` and then empty the set. def apply_deadlines_reset(self): @@ -496,7 +497,7 @@ class MessageBox: # TODO store chats too? if pts.entry not in self.possible_gaps: self.possible_gaps[pts.entry] = PossibleGap( - deadline=asyncio.get_running_loop().time() + POSSIBLE_GAP_TIMEOUT, + deadline=get_running_loop().time() + POSSIBLE_GAP_TIMEOUT, updates=[] ) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 802b99f9..ac206797 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -13,6 +13,8 @@ from .. import events, utils, errors from ..events.common import EventBuilder, EventCommon from ..tl import types, functions from .._updates import GapError, PrematureEndReason +from ..helpers import get_running_loop + if typing.TYPE_CHECKING: from .telegramclient import TelegramClient @@ -358,7 +360,7 @@ class UpdateMethods: continue deadline = self._message_box.check_deadlines() - deadline_delay = deadline - asyncio.get_running_loop().time() + deadline_delay = deadline - get_running_loop().time() if deadline_delay > 0: # Don't bother sleeping and timing out if the delay is already 0 (pollutes the logs). try: diff --git a/telethon/helpers.py b/telethon/helpers.py index aac058b0..9ddfe470 100644 --- a/telethon/helpers.py +++ b/telethon/helpers.py @@ -7,6 +7,7 @@ import struct import inspect import logging import functools +import sys from pathlib import Path from hashlib import sha1 @@ -423,3 +424,9 @@ class _FileStream(io.IOBase): pass # endregion + +def get_running_loop(): + if sys.version_info[:2] <= (3, 6): + return asyncio._get_running_loop() + + return asyncio.get_running_loop()