refactor(python): inline `ThpErrorCode.to_exception()`
What changed, and why it matters
This is a small internal code cleanup in the Python Trezor library. It removes a helper method and moves the same error-code-to-exception logic directly into the ThpError class constructor. There is no user-visible behavior change and no security relevance.
No action required. This is a non-security refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors ThpErrorCode.to_exception() into ThpError.__init__(). Previously, to_exception() converted an integer code into a ThpErrorCode enum member (or left it as an int if invalid) and returned a ThpError. Now ThpError.__init__() performs the same conversion internally, stores the enum member in self.code, sets self.name to the enum name or ‘unknown’, and passes the converted code and name to the parent exception. The functional behavior is preserved; only the internal structure changed.
Changed components
python/src/trezorlib/thp/exceptions.pyInspect captured patch +6 / −13
diff --git a/python/src/trezorlib/thp/exceptions.py b/python/src/trezorlib/thp/exceptions.py
index 35922b0d..a7fdb25a 100644
--- a/python/src/trezorlib/thp/exceptions.py
+++ b/python/src/trezorlib/thp/exceptions.py
@@ -27,20 +27,13 @@ class ThpErrorCode(IntEnum):
DECRYPTION_FAILED = 3
DEVICE_LOCKED = 5
- @classmethod
- def to_exception(cls, code: int) -> ThpError:
- try:
- valid_code = cls(code)
- return ThpError(valid_code)
- except ValueError:
- return ThpError(code)
-
class ThpError(exceptions.TrezorException):
- def __init__(self, code: ThpErrorCode | int) -> None:
+ def __init__(self, code: int) -> None:
self.code = code
- if isinstance(code, ThpErrorCode):
- self.name = code.name
- else:
+ try:
+ self.code = ThpErrorCode(code)
+ self.name = self.code.name
+ except ValueError:
self.name = "unknown"
- super().__init__(code, self.name)
+ super().__init__(self.code, self.name)
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.