Skip to content

Commit

Permalink
fix object leak
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Sep 3, 2024
1 parent 61530d3 commit 58768dd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
27 changes: 18 additions & 9 deletions src/textual/_callback.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import asyncio
from functools import lru_cache, partial
from functools import partial
from inspect import isawaitable, signature
from typing import TYPE_CHECKING, Any, Callable

Expand All @@ -16,16 +16,25 @@

def count_parameters(func: Callable) -> int:
"""Count the number of parameters in a callable"""
if isinstance(func, partial):
return _count_parameters(func.func) + len(func.args)
if hasattr(func, "__self__"):
# Bound method
func = func.__func__ # type: ignore
return _count_parameters(func) - 1
return _count_parameters(func)
from ._profile import timer

with timer("count"):
try:
return func._param_count
except AttributeError:
pass
if isinstance(func, partial):
param_count = _count_parameters(func) + len(func.args)
elif hasattr(func, "__self__"):
# Bound method
func = func.__func__ # type: ignore
param_count = _count_parameters(func) - 1
else:
param_count = _count_parameters(func)
func._param_count = param_count
return param_count


@lru_cache(maxsize=2048)
def _count_parameters(func: Callable) -> int:
"""Count the number of parameters in a callable"""
return len(signature(func).parameters)
Expand Down
5 changes: 4 additions & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,8 @@ async def run_auto_pilot(
await asyncio.shield(app._shutdown())
except asyncio.CancelledError:
pass

active_app.set(None)
active_message_pump.set(None)
return app.return_value

def run(
Expand Down Expand Up @@ -3029,6 +3030,8 @@ async def _close_all(self) -> None:
if stack_screen._running:
await self._prune(stack_screen)
stack.clear()
self._installed_screens.clear()
self._modes.clear()

# Close any remaining nodes
# Should be empty by now
Expand Down
1 change: 1 addition & 0 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -3676,6 +3676,7 @@ async def _message_loop_exit(self) -> None:
parent._nodes._remove(self)
self.app._registry.discard(self)
self._detach()
self._arrangement_cache.clear()

async def _on_idle(self, event: events.Idle) -> None:
"""Called when there are no more events on the queue.
Expand Down

0 comments on commit 58768dd

Please sign in to comment.