|
1 | 1 | """ |
2 | 2 | categories: Types,bytes |
3 | | -description: bytes.decode() only supports 'utf-8' and 'ascii' encodings, not other encodings like 'latin-1' |
4 | | -cause: MicroPython is optimized for embedded systems and only includes UTF-8 and ASCII codec support to save memory. Other encodings would require additional codec tables. |
5 | | -workaround: Convert data to UTF-8 before processing, or implement custom encoding/decoding if needed. |
| 3 | +description: bytes.decode() only supports encoding arguments 'utf8', 'utf-8' and 'ascii'. Other encodings like 'latin-1' are not supported. Other string forms such as 'UTF8' are not supported. |
| 4 | +cause: MicroPython is optimised for embedded systems and only includes UTF-8 and ASCII codec support with simple matching to save memory and code size. `The same restriction applies to str constructor <cpydiff_types_str_encoding>`. See also `unicode_support`. |
| 5 | +workaround: Convert data to UTF-8 before processing, or implement custom encoding/decoding if needed. Ensure encoding argument is one of the accepted forms. |
6 | 6 | """ |
7 | 7 |
|
8 | | -# CPython supports many encodings, MicroPython only utf-8 and ascii |
| 8 | +# Both CPython and MicroPython support utf8 and ascii encodings |
| 9 | +print(b"caf\xc3\xa9".decode("utf8")) # codespell:ignore caf |
| 10 | +print(b"cafe".decode("ascii")) |
| 11 | + |
| 12 | +# MicroPython does not support additional encodings |
9 | 13 | try: |
10 | 14 | b"\xe9".decode("latin-1") # 'é' in latin-1 |
11 | 15 | print("latin-1 supported") |
|
17 | 21 | print("cp1252 supported") |
18 | 22 | except (ValueError, NotImplementedError, LookupError) as e: |
19 | 23 | print("cp1252 not supported:", type(e).__name__) |
| 24 | + |
| 25 | +# Encoding arguments must match exactly in MicroPython |
| 26 | +try: |
| 27 | + b"hello".decode("ASCII") |
| 28 | + print("Capital letters ASCII supported") |
| 29 | +except (ValueError, NotImplementedError, LookupError) as e: |
| 30 | + print("Capital letters ASCII not supported:", type(e).__name__) |
0 commit comments