模块

Generated Thu 02 Sep 2021 01:44:06 UTC

array

Comparison between different typecodes not supported

原因: Code size

解决方案: Compare individual elements

样本代码:

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

CPy 输出:

uPy 输出:

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

Overflow checking is not implemented

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

解决方案: If CPython compatibility is needed then mask the value explicitly

样本代码:

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

CPy 输出:

uPy 输出:

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
OverflowError: signed char is greater than maximum
																
array('b', [1])
																

未实现查找整数

样本代码:

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

CPy 输出:

uPy 输出:

False
																
Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
NotImplementedError:
																

未实现数组删除

样本代码:

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

CPy 输出:

uPy 输出:

array('b', [1, 3])
																
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
TypeError: 'array' object doesn't support item deletion
																

Subscript with step != 1 is not yet implemented

样本代码:

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

CPy 输出:

uPy 输出:

array('b')
																
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
NotImplementedError: only slices with step=1 (aka None) are supported
																

builtins

Second argument to next() is not implemented

原因: MicroPython is optimised for code space.

解决方案: Instead of val = next(it, deflt) use:

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

样本代码:

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

CPy 输出:

uPy 输出:

42
																
Traceback (most recent call last):
  File "<stdin>", line 12, in <module>
TypeError: function takes 1 positional arguments but 2 were given
																

deque

Deque not implemented

解决方案: Use regular lists. micropython-lib has implementation of collections.deque.

样本代码:

import collections
D = collections.deque()
print(D)
												

CPy 输出:

uPy 输出:

deque([])
																
Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
TypeError: function missing 2 required positional arguments
																

json

JSON module does not throw exception when object is not serialisable

样本代码:

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 输出:

uPy 输出:

TypeError
																
Should not get here
																

os

environ attribute is not implemented

解决方案: 使用 getenv , putenv and unsetenv

样本代码:

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 输出:

uPy 输出:

None
VALUE
																
should not get here
None
VALUE
																

getenv returns actual value instead of cached value

原因: environ attribute is not implemented

样本代码:

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

CPy 输出:

uPy 输出:

None
None
																
None
VALUE
																

getenv only allows one argument

解决方案: Test that the return value is None

样本代码:

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 输出:

uPy 输出:

DEFAULT
																
should not get here
DEFAULT
																

random

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

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

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

样本代码:

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

CPy 输出:

uPy 输出:

4510159993864928489
																
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
ValueError: bits must be 32 or less
																

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

原因: 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.

解决方案: If you need integers larger than native wordsize use the random module from micropython-lib.

样本代码:

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

CPy 输出:

uPy 输出:

x=340282366920938463463374607431768211455
																
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
AttributeError: 'module' object has no attribute 'randint'
																

struct

Struct pack with too few args, not checked by uPy

样本代码:

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

CPy 输出:

uPy 输出:

struct.error
																
b'\x01\x00'
Should not get here
																

Struct pack with too many args, not checked by uPy

样本代码:

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

CPy 输出:

uPy 输出:

struct.error
																
b'\x01\x02'
Should not get here
																

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

原因: MicroPython is optimised for code size.

解决方案: Don’t use spaces in format strings.

样本代码:

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

CPy 输出:

uPy 输出:

b'\x01\x02'
Should have worked
																
struct.error
																

sys

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

原因: 它们被存储在只读内存中。

样本代码:

import sys
sys.stdin = None
print(sys.stdin)
												

CPy 输出:

uPy 输出:

None
																
Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
AttributeError: 'module' object has no attribute 'stdin'