.. This document was generated by tools/gen-cpydiff.py Syntax ====== Generated Thu 19 Jun 2025 07:21:24 UTC Literals -------- .. _cpydiff_syntax_literal_underscore: MicroPython accepts underscores in numeric literals where CPython doesn't ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Cause:** Different parser implementation MicroPython's tokenizer ignores underscores in numeric literals, while CPython rejects multiple consecutive underscores and underscores after the last digit. **Workaround:** Remove the underscores not accepted by CPython. Sample code:: try: print(eval("1__1")) except SyntaxError: print("Should not work") try: print(eval("1_")) except SyntaxError: print("Should not work") +---------------------+-------------+ | CPy output: | uPy output: | +---------------------+-------------+ | :: | :: | | | | | Should not work | 11 | | Should not work | 1 | +---------------------+-------------+ .. _cpydiff_syntax_spaces: MicroPython requires spaces between literal numbers and keywords or ".", CPython doesn't ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Cause:** Different parser implementation MicroPython's tokenizer treats a sequence like ``1and`` as a single token, while CPython treats it as two tokens. Since CPython 3.11, when the literal number is followed by a token, this syntax causes a ``SyntaxWarning`` for an "invalid literal". When a literal number is followed by a "." denoting attribute access, CPython does not warn. **Workaround:** Add a space between the integer literal and the intended next token. This also fixes the ``SyntaxWarning`` in CPython. Sample code:: try: print(eval("1and 0")) except SyntaxError: print("Should have worked") try: print(eval("1or 0")) except SyntaxError: print("Should have worked") try: print(eval("1if 1else 0")) except SyntaxError: print("Should have worked") try: print(eval("0x1.to_bytes(1)")) except SyntaxError: print("Should have worked") +--------------------------------------------------------+------------------------+ | CPy output: | uPy output: | +--------------------------------------------------------+------------------------+ | :: | :: | | | | | :1: SyntaxWarning: invalid decimal literal | Should have worked | | 0 | Should have worked | | :1: SyntaxWarning: invalid decimal literal | Should have worked | | 1 | Should have worked | | :1: SyntaxWarning: invalid decimal literal | | | :1: SyntaxWarning: invalid decimal literal | | | 1 | | | b'\x01' | | +--------------------------------------------------------+------------------------+ Operators --------- .. _cpydiff_syntax_assign_expr: MicroPython allows := to assign to the iteration variable in nested comprehensions, CPython does not. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Cause:** MicroPython is optimised for code size. Although it is a syntax error to assign to the iteration variable in a standard comprehension (same as CPython), it doesn't check if an inner nested comprehension assigns to the iteration variable of the outer comprehension. **Workaround:** Do not use := to assign to the iteration variable of a comprehension. Sample code:: print([[(j := i) for i in range(2)] for j in range(2)]) +-------------------------------------------------------------------------------------------+----------------------+ | CPy output: | uPy output: | +-------------------------------------------------------------------------------------------+----------------------+ | :: | :: | | | | | File "", line 8 | [[0, 1], [0, 1]] | | SyntaxError: assignment expression cannot rebind comprehension iteration variable 'j' | | +-------------------------------------------------------------------------------------------+----------------------+ Unicode ------- .. _cpydiff_syntax_unicode_nameesc: Unicode name escapes are not implemented ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sample code:: print("\N{LATIN SMALL LETTER A}") +-------------+-----------------------------------------------+ | CPy output: | uPy output: | +-------------+-----------------------------------------------+ | :: | :: | | | | | a | NotImplementedError: unicode name escapes | +-------------+-----------------------------------------------+ Unpacking --------- .. _cpydiff_syntax_arg_unpacking: Argument unpacking does not work if the argument being unpacked is the nth or greater argument where n is the number of bits in an MP_SMALL_INT. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Cause:** The implementation uses an MP_SMALL_INT to flag args that need to be unpacked. **Workaround:** Use fewer arguments. Sample code:: def example(*args): print(len(args)) MORE = ["a", "b", "c"] example( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, *MORE, ) +-------------+--------------------------------------------+ | CPy output: | uPy output: | +-------------+--------------------------------------------+ | :: | :: | | | | | 67 | Traceback (most recent call last): | | | File "", line 21, in | | | SyntaxError: too many args | +-------------+--------------------------------------------+