Whole file
trezor/python-mnemonic
The author described this change as “Detect language unambiguously, or raise explanatory exception”. It counts as a record because the check below fails on the code as it stood at 6b7ebdb36 and passes on 9e311a665, with nothing else changed between the two runs.
Projecttrezor/python-mnemonic
Fix saved2022-04-28
Sharing licenceMIT · LICENSE
Change size+9 −9
What the code was meant to do, written into the code itself as a save note
Detect language unambiguously, or raise explanatory exception
The change
| 101 | 101 | ||
| 102 | 102 | @classmethod | |
| 103 | 103 | def detect_language(cls, code: str) -> str: | |
| 104 | + | """Scan the Mnemonic until the language becomes unambiguous.""" | |
| 104 | 105 | code = cls.normalize_string(code) | |
| 105 | - | first = code.split(" ")[0] | |
| 106 | - | languages = cls.list_languages() | |
| 107 | - | ||
| 108 | - | for lang in languages: | |
| 109 | - | mnemo = cls(lang) | |
| 110 | - | if first in mnemo.wordlist: | |
| 111 | - | return lang | |
| 112 | - | ||
| 113 | - | raise ConfigurationError("Language not detected") | |
| 106 | + | possible = set(cls(lang) for lang in cls.list_languages()) | |
| 107 | + | for word in code.split(): | |
| 108 | + | possible = set(p for p in possible if word in p.wordlist) | |
| 109 | + | if not possible: | |
| 110 | + | raise ConfigurationError(f"Language unrecognized: {word!r}") | |
| 111 | + | if len( possible ) == 1: | |
| 112 | + | return possible.pop().language | |
| 113 | + | raise ConfigurationError(f"Language ambiguous: {', '.join( p.language for p in possible)}") | |
| 114 | 114 | ||
| 115 | 115 | def generate(self, strength: int = 128) -> str: | |
| 116 | 116 | """ |
The check that tells the two apart
fail→pass·tests/test_mnemonic.py::MnemonicTest::test_detection
Check file tests/test_mnemonic.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it6b7ebdb3624bbcae1a7b3c5485427a5587795120
Broken version dated2021-11-24
Modulemnemonic.mnemonic
Units changedMnemonic
Fingerprint239cec513816c673
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.