Skip to content

Added Python context manager (with) support to the device class#200

Open
ratijas wants to merge 1 commit intotrezor:masterfrom
ratijas:work/ratijas/context
Open

Added Python context manager (with) support to the device class#200
ratijas wants to merge 1 commit intotrezor:masterfrom
ratijas:work/ratijas/context

Conversation

@ratijas
Copy link
Copy Markdown

@ratijas ratijas commented Mar 5, 2026

Based on PR #108, but only the simplified context manager part of it.

Based on PR trezor#108, but only the simplified context manager part of it.
@ratijas ratijas force-pushed the work/ratijas/context branch from eaa95f1 to ab27a2c Compare March 7, 2026 19:33
@ratijas
Copy link
Copy Markdown
Author

ratijas commented Mar 7, 2026

Updated. Those return self statements are needed in both open* and __enter__ functions to enable full chaining like this:

with hid.device().open(0x534C, 0x0001) as h:
	pass

While being only marginally better than what @RobertoRoos tried to achieve, at least it lets you open a device before binding it to a variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant