From dce8a5e47f980421482d26cf404b3a6033c33790 Mon Sep 17 00:00:00 2001 From: Evgeniy Blinov Date: Tue, 27 Aug 2024 12:54:23 +0300 Subject: [PATCH 1/5] do not display the WaitCoroutineWrapper --- cantok/tokens/abstract/coroutine_wrapper.py | 9 +++++++-- pyproject.toml | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cantok/tokens/abstract/coroutine_wrapper.py b/cantok/tokens/abstract/coroutine_wrapper.py index 6016273..db27651 100644 --- a/cantok/tokens/abstract/coroutine_wrapper.py +++ b/cantok/tokens/abstract/coroutine_wrapper.py @@ -1,11 +1,13 @@ +import sys import weakref -from sys import getrefcount from typing import Dict, Union, Optional, Any from types import TracebackType from collections.abc import Coroutine from time import sleep as sync_sleep from asyncio import sleep as async_sleep +from displayhooks import not_display + class WaitCoroutineWrapper(Coroutine): # type: ignore[type-arg] def __init__(self, step: Union[int, float], token_for_wait: 'AbstractToken', token_for_check: 'AbstractToken') -> None: # type: ignore[name-defined] @@ -33,7 +35,7 @@ def close(self) -> None: @staticmethod def sync_wait(step: Union[int, float], flags: Dict[str, bool], token_for_wait: 'AbstractToken', token_for_check: 'AbstractToken', wrapped_coroutine: Coroutine) -> None: # type: ignore[type-arg, name-defined] if not flags.get('used', False): - if getrefcount(wrapped_coroutine) < 5: + if sys.getrefcount(wrapped_coroutine) < 5: wrapped_coroutine.close() while token_for_wait: @@ -51,3 +53,6 @@ async def async_wait(step: Union[int, float], flags: Dict[str, bool], token_for_ await async_sleep(0) token_for_check.check() + + +not_display(WaitCoroutineWrapper) diff --git a/pyproject.toml b/pyproject.toml index ca2affd..ef4a2ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ readme = "README.md" requires-python = ">=3.7" dependencies = [ 'typing_extensions ; python_version <= "3.9"', + 'displayhooks>=0.0.1', ] classifiers = [ "Operating System :: OS Independent", From 42aa153ca152496b0e6d6036e38e2b0e5ca84c2a Mon Sep 17 00:00:00 2001 From: Evgeniy Blinov Date: Tue, 27 Aug 2024 13:28:14 +0300 Subject: [PATCH 2/5] new test --- .../tokens/abstract/test_coroutine_wrapper.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/units/tokens/abstract/test_coroutine_wrapper.py diff --git a/tests/units/tokens/abstract/test_coroutine_wrapper.py b/tests/units/tokens/abstract/test_coroutine_wrapper.py new file mode 100644 index 0000000..957b089 --- /dev/null +++ b/tests/units/tokens/abstract/test_coroutine_wrapper.py @@ -0,0 +1,28 @@ +import io +import sys +from contextlib import redirect_stdout + +import pytest + +from cantok import SimpleToken, TimeoutToken, ConditionToken, CounterToken + + +@pytest.mark.parametrize( + ['create_value', 'expected_string'], + [ + (lambda: SimpleToken(cancelled=True).wait(), ''), + (lambda: TimeoutToken(0.0001).wait(), ''), + (lambda: ConditionToken(lambda: True).wait(), ''), + (lambda: CounterToken(0).wait(), ''), + (lambda: 1, '1\n'), + (lambda: 'kek', f'{repr('kek')}\n'), + ], +) +def test_displayhook_printing_coroutine_wrappers_and_other_objects(create_value, expected_string): + buffer = io.StringIO() + with redirect_stdout(buffer): + sys.displayhook(create_value()) + + output = buffer.getvalue() + + assert output == expected_string From a5e54ef62d8df9533d9fb6ab29809df0c1c995ed Mon Sep 17 00:00:00 2001 From: Evgeniy Blinov Date: Tue, 27 Aug 2024 13:30:49 +0300 Subject: [PATCH 3/5] quotes --- tests/units/tokens/abstract/test_coroutine_wrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/units/tokens/abstract/test_coroutine_wrapper.py b/tests/units/tokens/abstract/test_coroutine_wrapper.py index 957b089..0b42e2b 100644 --- a/tests/units/tokens/abstract/test_coroutine_wrapper.py +++ b/tests/units/tokens/abstract/test_coroutine_wrapper.py @@ -15,7 +15,7 @@ (lambda: ConditionToken(lambda: True).wait(), ''), (lambda: CounterToken(0).wait(), ''), (lambda: 1, '1\n'), - (lambda: 'kek', f'{repr('kek')}\n'), + (lambda: 'kek', f'{repr("kek")}\n'), ], ) def test_displayhook_printing_coroutine_wrappers_and_other_objects(create_value, expected_string): From 05d4add0ee062ffe14d352749acd4f1f5a1adb87 Mon Sep 17 00:00:00 2001 From: Evgeniy Blinov Date: Tue, 27 Aug 2024 13:50:21 +0300 Subject: [PATCH 4/5] new version of displayhooks --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ef4a2ef..7c9df57 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ readme = "README.md" requires-python = ">=3.7" dependencies = [ 'typing_extensions ; python_version <= "3.9"', - 'displayhooks>=0.0.1', + 'displayhooks>=0.0.2', ] classifiers = [ "Operating System :: OS Independent", From c31265f41d45b46627bfc3b1c0aa6e3d31c81695 Mon Sep 17 00:00:00 2001 From: Evgeniy Blinov Date: Tue, 27 Aug 2024 14:08:22 +0300 Subject: [PATCH 5/5] new version tag --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 7c9df57..70a9caa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "cantok" -version = "0.0.30" +version = "0.0.31" authors = [ { name="Evgeniy Blinov", email="zheni-b@yandex.ru" }, ]