Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.0.31 #42

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cantok/tokens/abstract/coroutine_wrapper.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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)
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]" },
]
Expand All @@ -13,6 +13,7 @@ readme = "README.md"
requires-python = ">=3.7"
dependencies = [
'typing_extensions ; python_version <= "3.9"',
'displayhooks>=0.0.2',
]
classifiers = [
"Operating System :: OS Independent",
Expand Down
28 changes: 28 additions & 0 deletions tests/units/tokens/abstract/test_coroutine_wrapper.py
Original file line number Diff line number Diff line change
@@ -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
Loading