From 189e3c39fc923c43492b5a4a9378028cf6887104 Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Fri, 7 Feb 2025 12:15:58 +0300 Subject: [PATCH] Remove sync support --- README.md | 2 +- pyrogram/__init__.py | 3 +- pyrogram/client.py | 11 +-- pyrogram/methods/utilities/run.py | 11 +-- pyrogram/sync.py | 113 ------------------------------ 5 files changed, 7 insertions(+), 133 deletions(-) delete mode 100644 pyrogram/sync.py diff --git a/README.md b/README.md index 9d650a6fd5..56ece5a44f 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ If you'd like to support my fork, you can consider: - **Elegant**: Low-level details are abstracted and re-presented in a more convenient way. - **Fast**: Boosted up by [TgCrypto](https://github.com/pyrogram/tgcrypto), a high-performance cryptography library written in C. - **Type-hinted**: Types and methods are all type-hinted, enabling excellent editor support. -- **Async**: Fully asynchronous (also usable synchronously if wanted, for convenience). +- **Async**: Fully asynchronous. - **Powerful**: Full access to Telegram's API to execute any official client action and more. ### Installing diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 3aee121d91..678b3123ea 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -37,6 +37,7 @@ class ContinuePropagation(StopAsyncIteration): from . import raw, types, filters, handlers, emoji, enums from .client import Client -from .sync import idle, compose +from .methods.utilities.compose import compose +from .methods.utilities.idle import idle crypto_executor = ThreadPoolExecutor(1, thread_name_prefix="CryptoWorker") diff --git a/pyrogram/client.py b/pyrogram/client.py index abd9c987a9..cea48423b4 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -360,15 +360,6 @@ def __init__( self.loop = asyncio.get_event_loop() - def __enter__(self): - return self.start() - - def __exit__(self, *args): - try: - self.stop() - except ConnectionError: - pass - async def __aenter__(self): return await self.start() @@ -1243,7 +1234,7 @@ async def get_file( def guess_mime_type(self, filename: Union[str, BytesIO]) -> Optional[str]: if isinstance(filename, BytesIO): return self.mimetypes.guess_type(filename.name)[0] - + return self.mimetypes.guess_type(filename)[0] def guess_extension(self, mime_type: str) -> Optional[str]: diff --git a/pyrogram/methods/utilities/run.py b/pyrogram/methods/utilities/run.py index 6b68867f24..cf0e24e9f6 100644 --- a/pyrogram/methods/utilities/run.py +++ b/pyrogram/methods/utilities/run.py @@ -63,11 +63,6 @@ def run( loop = asyncio.get_event_loop() run = loop.run_until_complete - if inspect.iscoroutinefunction(self.start): - run(self.start(use_qr=use_qr, except_ids=except_ids)) - run(idle()) - run(self.stop()) - else: - self.start(use_qr=use_qr, except_ids=except_ids) - run(idle()) - self.stop() + run(self.start(use_qr=use_qr, except_ids=except_ids)) + run(idle()) + run(self.stop()) diff --git a/pyrogram/sync.py b/pyrogram/sync.py deleted file mode 100644 index 94c82a3dd6..0000000000 --- a/pyrogram/sync.py +++ /dev/null @@ -1,113 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-present Dan -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . - -import asyncio -import functools -import inspect -import threading - -from pyrogram import types -from pyrogram.methods import Methods -from pyrogram.methods.utilities import idle as idle_module, compose as compose_module - - -def async_to_sync(obj, name): - function = getattr(obj, name) - main_loop = asyncio.get_event_loop() - - def async_to_sync_gen(agen, loop, is_main_thread): - async def anext(agen): - try: - return await agen.__anext__(), False - except StopAsyncIteration: - return None, True - - while True: - if is_main_thread: - item, done = loop.run_until_complete(anext(agen)) - else: - item, done = asyncio.run_coroutine_threadsafe(anext(agen), loop).result() - - if done: - break - - yield item - - @functools.wraps(function) - def async_to_sync_wrap(*args, **kwargs): - coroutine = function(*args, **kwargs) - - try: - loop = asyncio.get_event_loop() - except RuntimeError: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - - if threading.current_thread() is threading.main_thread() or not main_loop.is_running(): - if loop.is_running(): - return coroutine - else: - if inspect.iscoroutine(coroutine): - return loop.run_until_complete(coroutine) - - if inspect.isasyncgen(coroutine): - return async_to_sync_gen(coroutine, loop, True) - else: - if inspect.iscoroutine(coroutine): - if loop.is_running(): - async def coro_wrapper(): - return await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(coroutine, main_loop)) - - return coro_wrapper() - else: - return asyncio.run_coroutine_threadsafe(coroutine, main_loop).result() - - if inspect.isasyncgen(coroutine): - if loop.is_running(): - return coroutine - else: - return async_to_sync_gen(coroutine, main_loop, False) - - setattr(obj, name, async_to_sync_wrap) - - -def wrap(source): - for name in dir(source): - method = getattr(source, name) - - if not name.startswith("_"): - if inspect.iscoroutinefunction(method) or inspect.isasyncgenfunction(method): - async_to_sync(source, name) - - -# Wrap all Client's relevant methods -wrap(Methods) - -# Wrap types' bound methods -for class_name in dir(types): - cls = getattr(types, class_name) - - if inspect.isclass(cls): - wrap(cls) - -# Special case for idle and compose, because they are not inside Methods -async_to_sync(idle_module, "idle") -idle = getattr(idle_module, "idle") - -async_to_sync(compose_module, "compose") -compose = getattr(compose_module, "compose")