Skip to content

Commit

Permalink
Revert to prompt_toolkit's background task cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
joouha committed Dec 1, 2022
1 parent acc485e commit 67f64f6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Upcoming
Fixed
=====

- Prevent ``background_tasks`` error with ``prompt_toolkit==3.0.30``
- Prevent error when adding a cell during initial render

----
Expand Down
15 changes: 0 additions & 15 deletions euporie/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,21 +823,6 @@ def _handle_exception(
# Log observed exceptions to the log
log.exception("An unhandled exception occurred", exc_info=exception)

async def cancel_and_wait_for_background_tasks(self) -> None:
"""Cancel all background tasks, and wait for the cancellation to be done.
Ignore :py:`RuntimeError`s, which result when tasks are attached to a different
event loop.
"""
for task in self.background_tasks:
task.cancel()

for task in self.background_tasks:
try:
await task
except (asyncio.CancelledError, RuntimeError):
pass

# ################################### Commands ####################################

@staticmethod
Expand Down
10 changes: 8 additions & 2 deletions euporie/core/convert/formats/formatted_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from prompt_toolkit.formatted_text.base import StyleAndTextTuples
from upath import UPath

_html_cache = SimpleCache(maxsize=20)
from euporie.core.formatted_text.html import HTML

_html_cache: "SimpleCache[int, HTML]" = SimpleCache(maxsize=20)


@register(
Expand All @@ -36,7 +38,11 @@ def html_to_ft(
from euporie.core.formatted_text.html import HTML

html = _html_cache.get(hash(data), partial(HTML, data, width=width, base=path))
if html.width != width or html.height != height:
if (
width is not None
and height is not None
and (html.width != width or html.height != height)
):
html.render(width, height)
return to_formatted_text(HTML(data, width=width, base=path))

Expand Down
5 changes: 3 additions & 2 deletions scripts/show.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""A application to show files on the command line."""

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

from prompt_toolkit.application.current import set_app

Expand All @@ -10,7 +10,7 @@
if TYPE_CHECKING:
from typing import Any, Callable, Optional

from prompt_toolkit.application.base import _AppResult
from prompt_toolkit.application.application import _AppResult


class ShowApp(BaseApp):
Expand Down Expand Up @@ -50,6 +50,7 @@ def run(
),
style=self.create_merged_style(),
)
return cast("_AppResult", None)


if __name__ == "__main__":
Expand Down

0 comments on commit 67f64f6

Please sign in to comment.