Builtin types

Generated Wed 11 Nov 2020 02:26:58 UTC

Exception

Exception chaining not implemented

Sample code:

try:
    raise TypeError
except TypeError:
    raise ValueError

CPy output:

uPy output:

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
TypeError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
ValueError
/bin/sh: ../ports/unix/micropython: No such file or directory

User-defined attributes for builtin exceptions are not supported

Cause: MicroPython is highly optimized for memory usage.

Workaround: Use user-defined exception subclasses.

Sample code:

e = Exception()
e.x = 0
print(e.x)

CPy output:

uPy output:

0
/bin/sh: ../ports/unix/micropython: No such file or directory

Exception in while loop condition may have unexpected line number

Cause: Condition checks are optimized to happen at the end of loop body, and that line number is reported.

Sample code:

l = ["-foo", "-bar"]

i = 0
while l[i][0] == "-":
    print("iter")
    i += 1

CPy output:

uPy output:

iter
iter
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
IndexError: list index out of range
/bin/sh: ../ports/unix/micropython: No such file or directory

Exception.__init__ method does not exist.

Cause: Subclassing native classes is not fully supported in MicroPython.

Workaround: Call using super() instead:

class A(Exception):
    def __init__(self):
        super().__init__()

Sample code:

class A(Exception):
    def __init__(self):
        Exception.__init__(self)

a = A()

CPy output:

uPy output:

/bin/sh: ../ports/unix/micropython: No such file or directory

bytearray

Array slice assignment with unsupported RHS

Sample code:

b = bytearray(4)
b[0:1] = [1, 2]
print(b)

CPy output:

uPy output:

bytearray(b'\x01\x02\x00\x00\x00')
/bin/sh: ../ports/unix/micropython: No such file or directory

bytes

bytes objects support .format() method

Cause: MicroPython strives to be a more regular implementation, so if both str and bytes support __mod__() (the % operator), it makes sense to support format() for both too. Support for __mod__ can also be compiled out, which leaves only format() for bytes formatting.

Workaround: If you are interested in CPython compatibility, don’t use .format() on bytes objects.

Sample code:

print(b'{}'.format(1))

CPy output:

uPy output:

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
AttributeError: 'bytes' object has no attribute 'format'
/bin/sh: ../ports/unix/micropython: No such file or directory

bytes() with keywords not implemented

Workaround: Pass the encoding as a positional parameter, e.g. print(bytes('abc', 'utf-8'))

Sample code:

print(bytes('abc', encoding='utf8'))

CPy output:

uPy output:

b'abc'
/bin/sh: ../ports/unix/micropython: No such file or directory

Bytes subscription with step != 1 not implemented

Cause: MicroPython is highly optimized for memory usage.

Workaround: Use explicit loop for this very rare operation.

Sample code:

print(b'123'[0:3:2])

CPy output:

uPy output:

b'13'
/bin/sh: ../ports/unix/micropython: No such file or directory

dict

Dictionary keys view does not behave as a set.

Cause: Not implemented.

Workaround: Explicitly convert keys to a set before using set operations.

Sample code:

print({1:2, 3:4}.keys() & {1})

CPy output:

uPy output:

{1}
/bin/sh: ../ports/unix/micropython: No such file or directory

float

uPy and CPython outputs formats may differ

Sample code:

print('%.1g' % -9.9)

CPy output:

uPy output:

-1e+01
/bin/sh: ../ports/unix/micropython: No such file or directory

int

No int conversion for int-derived types available

Workaround: Avoid subclassing builtin types unless really needed. Prefer https://en.wikipedia.org/wiki/Composition_over_inheritance .

Sample code:

class A(int):
    __add__ = lambda self, other: A(int(self) + other)

a = A(42)
print(a+a)

CPy output:

uPy output:

84
/bin/sh: ../ports/unix/micropython: No such file or directory

list

List delete with step != 1 not implemented

Workaround: Use explicit loop for this rare operation.

Sample code:

l = [1, 2, 3, 4]
del l[0:4:2]
print(l)

CPy output:

uPy output:

[2, 4]
/bin/sh: ../ports/unix/micropython: No such file or directory

List slice-store with non-iterable on RHS is not implemented

Cause: RHS is restricted to be a tuple or list

Workaround: Use list(<iter>) on RHS to convert the iterable to a list

Sample code:

l = [10, 20]
l[0:1] = range(4)
print(l)

CPy output:

uPy output:

[0, 1, 2, 3, 20]
/bin/sh: ../ports/unix/micropython: No such file or directory

List store with step != 1 not implemented

Workaround: Use explicit loop for this rare operation.

Sample code:

l = [1, 2, 3, 4]
l[0:4:2] = [5, 6]
print(l)

CPy output:

uPy output:

[5, 2, 6, 4]
/bin/sh: ../ports/unix/micropython: No such file or directory

str

Start/end indices such as str.endswith(s, start) not implemented

Sample code:

print('abc'.endswith('c', 1))

CPy output:

uPy output:

True
/bin/sh: ../ports/unix/micropython: No such file or directory

Attributes/subscr not implemented

Sample code:

print('{a[0]}'.format(a=[1, 2]))

CPy output:

uPy output:

1
/bin/sh: ../ports/unix/micropython: No such file or directory

str(…) with keywords not implemented

Workaround: Input the encoding format directly. eg print(bytes('abc', 'utf-8'))

Sample code:

print(str(b'abc', encoding='utf8'))

CPy output:

uPy output:

abc
/bin/sh: ../ports/unix/micropython: No such file or directory

str.ljust() and str.rjust() not implemented

Cause: MicroPython is highly optimized for memory usage. Easy workarounds available.

Workaround: Instead of s.ljust(10) use "%-10s" % s, instead of s.rjust(10) use "% 10s" % s. Alternatively, "{:<10}".format(s) or "{:>10}".format(s).

Sample code:

print('abc'.ljust(10))

CPy output:

uPy output:

abc
/bin/sh: ../ports/unix/micropython: No such file or directory

None as first argument for rsplit such as str.rsplit(None, n) not implemented

Sample code:

print('a a a'.rsplit(None, 1))

CPy output:

uPy output:

['a a', 'a']
/bin/sh: ../ports/unix/micropython: No such file or directory

Subscript with step != 1 is not yet implemented

Sample code:

print('abcdefghi'[0:9:2])

CPy output:

uPy output:

acegi
/bin/sh: ../ports/unix/micropython: No such file or directory

tuple

Tuple load with step != 1 not implemented

Sample code:

print((1, 2, 3, 4)[0:4:2])

CPy output:

uPy output:

(1, 3)
/bin/sh: ../ports/unix/micropython: No such file or directory