Modules

Generated Tue 14 Dec 2021 16:55:24 UTC

array

Comparison between different typecodes not supported

Cause: Code size

Workaround: Compare individual elements

Sample code:

import array

array.array("b", [1, 2]) == array.array("i", [1, 2])

CPy output:

uPy output:

/bin/sh: 1: ../ports/unix/micropython: not found

Overflow checking is not implemented

Cause: MicroPython implements implicit truncation in order to reduce code size and execution time

Workaround: If CPython compatibility is needed then mask the value explicitly

Sample code:

import array

a = array.array("b", [257])
print(a)

CPy output:

uPy output:

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
OverflowError: signed char is greater than maximum
/bin/sh: 1: ../ports/unix/micropython: not found

Looking for integer not implemented

Sample code:

import array

print(1 in array.array("B", b"12"))

CPy output:

uPy output:

False
/bin/sh: 1: ../ports/unix/micropython: not found

Array deletion not implemented

Sample code:

import array

a = array.array("b", (1, 2, 3))
del a[1]
print(a)

CPy output:

uPy output:

array('b', [1, 3])
/bin/sh: 1: ../ports/unix/micropython: not found

Subscript with step != 1 is not yet implemented

Sample code:

import array

a = array.array("b", (1, 2, 3))
print(a[3:2:2])

CPy output:

uPy output:

array('b')
/bin/sh: 1: ../ports/unix/micropython: not found

builtins

Second argument to next() is not implemented

Cause: MicroPython is optimised for code space.

Workaround: Instead of val = next(it, deflt) use:

try:
    val = next(it)
except StopIteration:
    val = deflt

Sample code:

print(next(iter(range(0)), 42))

CPy output:

uPy output:

42
/bin/sh: 1: ../ports/unix/micropython: not found

deque

Deque not implemented

Workaround: Use regular lists. micropython-lib has implementation of collections.deque.

Sample code:

import collections

D = collections.deque()
print(D)

CPy output:

uPy output:

deque([])
/bin/sh: 1: ../ports/unix/micropython: not found

json

JSON module does not throw exception when object is not serialisable

Sample code:

import json

a = bytes(x for x in range(256))
try:
    z = json.dumps(a)
    x = json.loads(z)
    print("Should not get here")
except TypeError:
    print("TypeError")

CPy output:

uPy output:

TypeError
/bin/sh: 1: ../ports/unix/micropython: not found

os

environ attribute is not implemented

Workaround: Use getenv, putenv and unsetenv

Sample code:

import os

try:
    print(os.environ.get("NEW_VARIABLE"))
    os.environ["NEW_VARIABLE"] = "VALUE"
    print(os.environ["NEW_VARIABLE"])
except AttributeError:
    print("should not get here")
    print(os.getenv("NEW_VARIABLE"))
    os.putenv("NEW_VARIABLE", "VALUE")
    print(os.getenv("NEW_VARIABLE"))

CPy output:

uPy output:

None
VALUE
/bin/sh: 1: ../ports/unix/micropython: not found

getenv returns actual value instead of cached value

Cause: The environ attribute is not implemented

Sample code:

import os

print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))

CPy output:

uPy output:

None
None
/bin/sh: 1: ../ports/unix/micropython: not found

getenv only allows one argument

Workaround: Test that the return value is None

Sample code:

import os

try:
    print(os.getenv("NEW_VARIABLE", "DEFAULT"))
except TypeError:
    print("should not get here")
    # this assumes NEW_VARIABLE is never an empty variable
    print(os.getenv("NEW_VARIABLE") or "DEFAULT")

CPy output:

uPy output:

DEFAULT
/bin/sh: 1: ../ports/unix/micropython: not found

random

getrandbits method can only return a maximum of 32 bits at a time.

Cause: PRNG’s internal state is only 32bits so it can only return a maximum of 32 bits of data at a time.

Workaround: If you need a number that has more than 32 bits then utilize the random module from micropython-lib.

Sample code:

import random


x = random.getrandbits(64)
print("{}".format(x))

CPy output:

uPy output:

9151249466937607742
/bin/sh: 1: ../ports/unix/micropython: not found

randint method can only return an integer that is at most the native word size.

Cause: PRNG is only able to generate 32 bits of state at a time. The result is then cast into a native sized int instead of a full int object.

Workaround: If you need integers larger than native wordsize use the random module from micropython-lib.

Sample code:

import random


x = random.randint(2 ** 128 - 1, 2 ** 128)
print("x={}".format(x))

CPy output:

uPy output:

x=340282366920938463463374607431768211456
/bin/sh: 1: ../ports/unix/micropython: not found

struct

Struct pack with too few args, not checked by uPy

Sample code:

import struct

try:
    print(struct.pack("bb", 1))
    print("Should not get here")
except:
    print("struct.error")

CPy output:

uPy output:

struct.error
/bin/sh: 1: ../ports/unix/micropython: not found

Struct pack with too many args, not checked by uPy

Sample code:

import struct

try:
    print(struct.pack("bb", 1, 2, 3))
    print("Should not get here")
except:
    print("struct.error")

CPy output:

uPy output:

struct.error
/bin/sh: 1: ../ports/unix/micropython: not found

Struct pack with whitespace in format, whitespace ignored by CPython, error on uPy

Cause: MicroPython is optimised for code size.

Workaround: Don’t use spaces in format strings.

Sample code:

import struct

try:
    print(struct.pack("b b", 1, 2))
    print("Should have worked")
except:
    print("struct.error")

CPy output:

uPy output:

b'\x01\x02'
Should have worked
/bin/sh: 1: ../ports/unix/micropython: not found

sys

Overriding sys.stdin, sys.stdout and sys.stderr not possible

Cause: They are stored in read-only memory.

Sample code:

import sys

sys.stdin = None
print(sys.stdin)

CPy output:

uPy output:

None
/bin/sh: 1: ../ports/unix/micropython: not found