Skip to content

Commit 6d6f372

Browse files
committed
refactor(core): use HomescreenBase as a context manager
Instead of explicitly invoking `__del__()` to drop its `LayoutObj`. [no changelog]
1 parent d2e80e3 commit 6d6f372

4 files changed

Lines changed: 26 additions & 21 deletions

File tree

core/src/apps/homescreen/__init__.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414

1515

1616
async def busyscreen() -> None:
17-
obj = Busyscreen(busy_expiry_ms())
18-
try:
17+
with Busyscreen(busy_expiry_ms()) as obj:
1918
await obj.get_result()
20-
finally:
21-
obj.__del__()
2219

2320

2421
async def homescreen() -> None:
@@ -69,15 +66,12 @@ async def homescreen() -> None:
6966
False,
7067
)
7168

72-
obj = Homescreen(
69+
with Homescreen(
7370
label=label,
7471
notification=notification,
7572
lockable=config.has_pin(),
76-
)
77-
try:
73+
) as obj:
7874
res = await obj.get_result()
79-
finally:
80-
obj.__del__()
8175

8276
if utils.INTERNAL_MODEL == "T3W1":
8377
if res is trezorui_api.INFO:
@@ -94,14 +88,11 @@ async def _lockscreen(screensaver: bool = False) -> None:
9488
# Only show the lockscreen UI if the device can in fact be locked, or if it is
9589
# and OLED device (in which case the lockscreen is a screensaver).
9690
if can_lock_device() or screensaver:
97-
obj = Lockscreen(
91+
with Lockscreen(
9892
label=storage.device.get_label(),
9993
coinjoin_authorized=is_set_any_session(MessageType.AuthorizeCoinJoin),
100-
)
101-
try:
94+
) as obj:
10295
await obj.get_result()
103-
finally:
104-
obj.__del__()
10596
# Otherwise proceed directly to unlock() call. If the device is already unlocked,
10697
# it should be a no-op storage-wise, but it resets the internal configuration
10798
# to an unlocked state.

core/src/boot.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ def enforce_welcome_screen_duration() -> None:
5555
if not utils.USE_POWER_MANAGER:
5656

5757
async def pin_unlock_sequence() -> None:
58-
lockscreen = Lockscreen(label=storage.device.get_label(), bootscreen=True)
59-
await lockscreen.get_result()
60-
lockscreen.__del__()
58+
with Lockscreen(
59+
label=storage.device.get_label(),
60+
bootscreen=True,
61+
) as lockscreen:
62+
await lockscreen.get_result()
6163
await verify_user_pin()
6264

6365
else:
@@ -70,9 +72,11 @@ async def wait_for_suspend() -> int:
7072

7173
async def pin_unlock_sequence() -> None:
7274
while True:
73-
lockscreen = Lockscreen(label=storage.device.get_label(), bootscreen=True)
74-
await lockscreen.get_result()
75-
lockscreen.__del__()
75+
with Lockscreen(
76+
label=storage.device.get_label(),
77+
bootscreen=True,
78+
) as lockscreen:
79+
await lockscreen.get_result()
7680
res = await loop.race(verify_user_pin(), wait_for_suspend())
7781
if res is _SUSPEND_MARKER:
7882
# make some delay for the suspend

core/src/trezor/ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ def _start_task(self, task: loop.Task[None]) -> None:
564564
loop.schedule(task, finalizer=self._task_finalizer)
565565

566566
def __del__(self) -> None:
567+
# safe to call even if `self.layout` has been already dropped.
567568
self.layout.__del__()
568569

569570

core/src/trezor/ui/layouts/homescreen.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Any, Callable, Iterator, ParamSpec, Tuple, TypeVar
1010

1111
from trezor import loop
12+
from typing_extensions import Self
1213

1314
P = ParamSpec("P")
1415
R = TypeVar("R")
@@ -57,7 +58,7 @@ def create_tasks(self) -> Iterator[loop.Task[None]]:
5758
class HomescreenBase(UsbAwareLayout):
5859
RENDER_INDICATOR: object | None = None
5960

60-
def __init__(self, layout: Any) -> None:
61+
def __init__(self, layout: trezorui_api.LayoutObj[trezorui_api.UiResult]) -> None:
6162
super().__init__(layout=layout)
6263
self.should_resume = self._should_resume()
6364

@@ -74,6 +75,14 @@ def _first_paint(self) -> None:
7475
else:
7576
self._paint()
7677

78+
def __enter__(self) -> Self:
79+
self.layout.__enter__()
80+
return self
81+
82+
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
83+
"""Drop internal Rust root component."""
84+
self.layout.__exit__(exc_type, exc_val, exc_tb)
85+
7786

7887
class Homescreen(HomescreenBase):
7988
RENDER_INDICATOR = storage_cache.HOMESCREEN_ON

0 commit comments

Comments
 (0)