Differences Between MicroPython and CPython (1)

MicroPython implements a selection of features from Python 3.4 and later versions. The current status of these features is introduced below.

Python 3.5

The following is a list of PEPs that have been finalized/accepted for Python 3.5, grouped by their impact on MicroPython.

PEP Number

Name

Status

Syntax Extensions

PEP 448

Additional Unpacking Generalization

Partial Support

PEP 465

New Matrix Multiplication Operator

Full Support

PEP 492

Coroutines with async and await Syntax

Full Support

Runtime Extensions and Changes

PEP 461

% Formatting for Binary Strings

Full Support

PEP 475

Retrying Failed System Calls Due to EINTR

Full Support

PEP 479

Changing the Handling of StopIteration Inside Generators

Full Support

Standard Library Changes

PEP 471

os.scandir()

PEP 485

math.isclose() — Function for Testing Approximate Equality

Full Support

Other Changes

PEP 441

Improved Python zip Application Support

PEP 486

Let Python Launcher Know About Virtual Environments

Not Relevant

PEP 484

Type Hints (Advisory Only)

Full Support

PEP 488

Remove PYO Files

Not Relevant

PEP 489

Redesign the Loading of Extension Modules

Other Language Changes:

Change

Status

Added namereplace error handler. backslashreplace error handler is now available for decoding and conversion.

Property docstrings are now writable. This is especially useful for collections.namedtuple() docstrings.

Now supports circular imports involving relative imports.

New Modules:

  • typing

  • zipzap

Changes to Built-in Modules:

Change

Status

collections

OrderedDict class is now implemented in C, with speed improvements of 4 to 100 times.

OrderedDict.items(), OrderedDict.keys(), and OrderedDict.values() views now support reversed() iteration.

deque class now defines index(), insert(), and copy() methods and supports + and * operators.

namedtuple() generated docstrings are now updatable.

heapq

merge() element comparisons can now be customized by passing a key function via the new optional key keyword argument, and a new optional reverse keyword argument can be used to reverse element comparisons.

io

Added BufferedIOBase.readinto1() method, which uses at most one call to the underlying raw stream's RawIOBase.read() or RawIOBase.readinto() methods.

json

JSON decoder now raises JSONDecodeError instead of ValueError for more detailed error context.

math

math module now includes two constants: inf and nan.

Full Support

New isclose() function provides a way to test for approximate equality.

New gcd() function. fractions.gcd() function is now deprecated.

os

New scandir() function returns an iterator of DirEntry objects.

On Linux 3.17 and later, the urandom() function now uses the getrandom() system call, and on OpenBSD 5.6 and later, it uses getentropy(), eliminating the need to use /dev/urandom, avoiding failures due to potential file descriptor exhaustion.

New get_blocking() and set_blocking() functions allow getting and setting the blocking mode (O_NONBLOCK) of file descriptors.

New os.path.commonpath() function returns the longest common sub-path of each pathname passed in.

re

Now allows referencing and conditionally referencing fixed-length groups in lookbehind assertions.

The number of capturing groups in regular expressions is no longer limited to 100.

sub() and subn() functions now replace unmatched groups with an empty string instead of raising an exception.

re.error exception now has msg, pattern, pos, lineno, and colno attributes for more detailed error context.

socket

Functions with timeouts now use a monotonic clock instead of a system clock.

New socket.sendfile() method allows sending files over sockets using the high-performance os.sendfile() function on UNIX, making uploads 2 to 3 times faster than using regular socket.send().

socket.sendall() method no longer resets the socket timeout on each byte received or sent. The socket timeout is now the maximum total duration for sending all data.

socket.listen() method’s backlog parameter is now optional. By default, it is set to the smaller of SOMAXCONN or 128.

Full Support

ssl

Memory BIO support

Application Layer Protocol Negotiation support

New SSLSocket.version() method to query the actual protocol version in use.

SSLSocket class now implements SSLSocket.sendfile() method.

On non-blocking sockets, if an operation would block, the SSLSocket.send() method now raises ssl.SSLWantReadError or ssl.SSLWantWriteError exceptions. Previously, it would return 0.

According to RFC 5280, the cert_time_to_seconds() function now interprets input time as UTC instead of local time. Additionally, the return value is always an int.

New SSLObject.shared_ciphers() and SSLSocket.shared_ciphers() methods return the list of cipher suites sent by the client during the handshake.

SSLSocket class’s SSLSocket.do_handshake(), SSLSocket.read(), SSLSocket.shutdown(), and SSLSocket.write() methods no longer reset the socket timeout on each byte received or sent.

match_hostname() function now supports matching IP addresses.

sys

New set_coroutine_wrapper() function allows setting a global hook that is called when an async def function creates a coroutine object. The corresponding get_coroutine_wrapper() can be used to get the currently set wrapper.

New is_finalizing() function can be used to check if the Python interpreter is shutting down.

time

monotonic() function is now always available.

Python 3.6

Python 3.6 beta 1 was released on September 12, 2016, with a summary of new features available here:

PEP Number

Name

Status

New Syntax Features

PEP 498

Literal String Interpolation

Full Support

PEP 515

Underscores in Numeric Literals

Full Support

PEP 525

Asynchronous Generators

PEP 526

Variable Annotations Syntax (Tentative)

Full Support

PEP 530

Asynchronous Comprehensions

New Built-in Features

PEP 468

Preserving the Order of Keyword Arguments in Functions

PEP 487

More Concise Class Creation Customization

Partial Support

PEP 520

Preserving the Order of Class Attribute Definitions

Standard Library Changes

PEP 495

Handling Local Time Ambiguities

PEP 506

Adding secrets Module to the Standard Library

PEP 519

Adding File System Path Protocol

CPython Internal Mechanisms

PEP 509

Adding Private Version for Dictionaries

Not Supported

PEP 523

Adding Frame Evaluation API to CPython

Linux/Windows Changes

PEP 524

Making os.urandom() Blocking on Linux

PEP 528

Changing Windows Console Encoding to UTF-8

PEP 529

Changing Windows File System Encoding to UTF-8

Other Language Changes:

Change

Status

Global or nonlocal statements must now appear before the first use of affected names in the same scope. Previously, this was just a syntax warning.

Special methods can now be set to None to indicate that the corresponding operation is unavailable. For example, if a class sets __iter__() to None, then that class is not iterable.

Repeated long traceback line sequences are now abbreviated to [Previous line repeated {count} more times] (the previous line was repeated {count} times).

When an import fails to find a module, a new exception ModuleNotFoundError is now raised. Code that currently checks ImportError in try-except blocks still works.

Classes that depend on the no-argument super() method now work correctly when called from metaclass methods during class creation.

Changes to Built-in Modules:

Change

Status

array

array.array iterator will remain exhausted even if the iterated array is extended.

binascii

b2a_base64() function now accepts an optional newline keyword argument to control whether to append a newline to the return value.

Full Support

cmath

New cmath.tau (τ) constant.

New constants: cmath.inf and cmath.nan, matching math.inf and math.nan; also cmath.infj and cmath.nanj, matching the format used for complex repr.

collections

New Collection abstract base class for representing sized iterable container classes.

New Reversible abstract base class representing iterable classes that provide a __reversed__() method.

New AsyncGenerator abstract base class representing asynchronous generators.

namedtuple() function now accepts an optional keyword parameter module, which will be used for the returned named tuple class’s __module__ attribute.
namedtuple() verbose and rename parameters now only support keyword passing.

Recursive collections.deque instances can now be pickled.

hashlib

Module adds BLAKE2 hash functions. blake2b() and blake2s() are always available and support the full feature set of BLAKE2.

Added SHA-3 hash functions sha3_224(), sha3_256(), sha3_384(), sha3_512(), and SHAKE hash functions shake_128() and shake_256().

In OpenSSL 1.1.0 and later, the password-based key derivation function scrypt() is now available.

json

json.load() and json.loads() now support binary input. Encoded JSON should be represented using UTF-8, UTF-16, or UTF-32.

math

New math.tau (τ) constant.

Full Support

os

New close() method allows explicitly closing scandir() iterators. scandir() iterators now support the context manager protocol.

On Linux, os.urandom() now blocks until the system urandom entropy pool is initialized to improve security.

Linux’s getrandom() system call (to get random bytes) is now exposed via the new os.getrandom() function.

re

Added support for modifier ranges in regular expressions. For example:(?i:p)ython matches “python” and “Python”, but not “PYTHON”; (?i)g(?-i:v)r matches “GvR” and “gvr”, but not “GVR”.

Match object’s groups can be accessed via __getitem__, which is equivalent to group(). Thus, mo['name'] is now equivalent to mo.group('name').

Match objects now support class index objects as group indices.

socket

ioctl() function now supports SIO_LOOPBACK_FAST_PATH control code.

Now supports getsockopt() constants SO_DOMAIN, SO_PROTOCOL, SO_PEERSEC, and SO_PASSSEC.

setsockopt() now supports setsockopt(level, optname, None, optlen: int) form.

Socket module now supports address family AF_ALG to interact with the Linux kernel cryptography API. Added ALG_, SOL_ALG, and sendmsg_afalg().

New Linux constants TCP_USER_TIMEOUT and TCP_CONGESTION.

ssl

ssl supports OpenSSL 1.1.0. The recommended minimum version is 1.0.2.

3DES has been removed from the default cipher suite, and ChaCha20 Poly1305 cipher suite has been added.

SSLContext has better default configurations for options and ciphers.

With the new SSLSession class, SSL sessions can be copied from one client connection to another. TLS session resumption can speed up initial handshake, reduce latency, and improve performance.

New get_ciphers() method can be used to get the list of enabled ciphers sorted by priority.

All constants and flags have been converted to IntEnum and IntFlags.

Added server-side and client-specific TLS protocols to SSLContext.

Added SSLContext.post_handshake_auth to enable, and ssl.SSLSocket.verify_client_post_handshake() to initiate post-handshake authentication for TLS 1.3.

struct

Now supports IEEE 754 half-precision floating-point numbers via the “e” format specifier.

sys

New getfilesystemencodeerrors() function returns the name of the error mode used for converting between Unicode filenames and byte filenames.

zlib

compress() and decompress() functions now accept keyword arguments.

Python 3.7

New Features:

PEP Number

Description

Status

PEP 538

Forcing Legacy C Locale to UTF-8 Based Locale

PEP 539

New C API for Thread Local Storage in CPython

PEP 540

UTF-8 Mode

PEP 552

Deterministic pyc Files

PEP 553

Built-in breakpoint() Function

PEP 557

Data Classes

PEP 560

Core Support for typing Module and Generic Types

PEP 562

Module’s __getattr__ and __dir__ Methods

Partial Support

PEP 563

Delayed Evaluation of Annotations

PEP 564

Time Functions with Nanosecond Resolution

Partial Support

PEP 565

Show DeprecationWarning in __main__

PEP 567

Context Variables

Other Language Changes:

Change

Status

async and await are now reserved keywords.

Full Support

Dictionary objects must preserve insertion order.

Now can pass more than 255 parameters to functions; functions can now have more than 255 parameters.

bytes.fromhex() and bytearray.fromhex() now ignore all ASCII whitespace characters, not just spaces.

str, bytes, and bytearray now support the isascii() method to test if a string or bytes contains only ASCII characters.

When from ... import ... fails, ImportError now shows the module name and the module’s __file__ path.

Now supports circular imports involving absolute imports that bind submodules to a name.

object.__format__(x, '') is now equivalent to str(x), rather than format(str(self), '').

To better support dynamic stack trace creation, it is now possible to instantiate types.TracebackType from Python code, and the tb_next attribute on tracebacks is now writable.

When using the -m switch, sys.path[0] now immediately expands to the full starting directory path instead of remaining an empty directory (allowing imports from the current working directory when imports occur).

New -X importtime option or PYTHONPROFILEIMPORTTIME environment variable can be used to display the time taken for each module import.

Changes to Built-in Modules:

Change

Status

asyncio

Changes are too numerous to list individually.

gc

New features include gc.freeze(), gc.unfreeze(), and gc.get_freeze_count().

math

New math.remainder() function implements IEEE 754 style remainder calculation.

re

Multiple organization features, including better support for splitting by empty strings, and support for copying compiled expressions and match objects.

sys

New sys.breakpointhook() function. New sys.get(/set)_coroutine_origin_tracking_depth() functions.

time

Updates primarily for supporting nanosecond resolution in PEP564, see above.

Python 3.8

Python 3.8.0 (final version) was released on October 14, 2019. The features of version 3.8 are defined in PEP 569, and detailed descriptions of the changes can be found in the new features of Python 3.8.

PEP Number

Description

Status

PEP 570

Positional-Only Parameters

PEP 572

Assignment Expressions

Full Support

PEP 574

Pickle Protocol 5 with Out-of-Band Data

PEP 578

Runtime Audit Hooks

PEP 587

Python Initialization Configuration

PEP 590

Vectorcall: A Fast Calling Protocol for CPython

Other

f-string support for self-documenting expressions and debugging

Full Support

Other Language Changes:

Change

Status

Due to implementation issues, using continue statements in finally clauses was previously illegal. This restriction has been lifted in Python 3.8.

Full Support

bool, int, and fractions.Fraction types now have an as_integer_ratio() method, similar to that in float and decimal.Decimal.

int, float, and complex constructors now use the __index__() special method (if it exists, and the corresponding __int__(), __float__(), or __complex__() methods do not).

Support for N{name} escape in regular expressions has been added.

Now can iterate dictionaries and dictionary views in reverse insertion order using reversed().

Function call syntax for keyword names has been further restricted. In particular, the form f((keyword)=arg) is no longer allowed.

yield and return statements’ general iterable unpacking no longer requires parentheses.

When a comma is missed in the code (e.g., [(10, 20) (30, 40)]), the compiler will display a SyntaxWarning and provide helpful suggestions.

datetime.date or datetime.datetime subclasses now return instances of the subclass rather than the base class when performing arithmetic operations with datetime.timedelta objects.

When the Python interpreter is interrupted by Ctrl-C (SIGINT), and the resulting KeyboardInterrupt exception is not caught, the Python process now exits via the SIGINT signal or the correct exit code, allowing the calling process to detect that it was terminated due to Ctrl-C.

Some advanced programming styles require updating existing types.CodeType objects.

For integers, the three-parameter form of the pow() function now allows negative exponents when the base and modulus are coprime.

Dictionary comprehensions have been synchronized with dictionary literals, i.e., keys are computed first, then values.

object.__reduce__() method can now return tuples of length 2 to 6.

Changes to Built-in Modules:

Change

Status

asyncio

asyncio.run() has been upgraded from a temporary API to a stable API.

Full Support

Running python -m asyncio will start a native asynchronous REPL.

Exceptions asyncio.CancelledError now inherit from BaseException instead of Exception, and no longer inherit from concurrent.futures.CancelledError.

Full Support

New asyncio.Task.get_coro() to get the coroutine wrapped in asyncio.Task.

Now can name asyncio tasks by passing the name keyword argument to asyncio.create_task() or the event loop’s create_task() method, or by calling the task object’s set_name() method.

asyncio.loop.create_connection() now supports the “Happy Eyeballs” algorithm. To specify this behavior, two new parameters have been added: happy_eyeballs_delay and interleave.

gc

get_objects() can now accept an optional generation parameter to specify which generation to get objects from. (Note that although gc is a built-in module, MicroPython does not implement get_objects().)

math

New math.dist() function to calculate the Euclidean distance between two points.

Extended math.hypot() function to handle multidimensional cases.

New math.prod() function, similar to sum(), returning the product of the "start" value (default is 1) and an iterable of numbers.

New two combinatorial functions math.perm() and math.comb().

New math.isqrt() function to compute the exact integer square root without converting to a float.

math.factorial() function no longer accepts non-class integer arguments.

Full Support

sys

New sys.unraisablehook() function can control the handling of "unraisable exceptions" by overriding this function.

Python 3.9

Python 3.9.0 (final version) was released on October 5, 2020. The features of version 3.9 are defined in PEP 596, and detailed descriptions of the changes can be found in the new features of Python 3.9.

Features

PEP Number

Description

Status

PEP 573

Fast Access to Module State from C Extension Types

Not Relevant

PEP 584

Adding Union Operators to Dictionaries

Full Support

PEP 585

Type Hinting Generics in Standard Collections

PEP 593

Flexible Function and Variable Annotations

PEP 602

CPython Adopts Annual Release Cycle (instead of the previously planned biannual release cycle)

Not Relevant

PEP 614

Relaxing Syntax Restrictions on Decorators

PEP 615

IANA Time Zone Database Now Included in the Standard Library’s zoneinfo Module

PEP 616

String Methods for Removing Prefixes and Suffixes

PEP 617

CPython Now Uses a New PEG-Based Parser

Not Relevant

Other Language Changes:

Change

Status

__import__() now raises ImportError instead of ValueError.

Full Support

Python now obtains the absolute path of the script file name specified on the command line (e.g., python3 script.py): the __main__ module’s __file__ attribute becomes an absolute path instead of a relative path.

By default, for optimal performance, the errors parameter is only checked on the first encoding/decoding error, and for empty strings, the encoding parameter may sometimes be ignored.

For all non-zero n, "".replace("", s, n)" now returns s instead of an empty string. This is consistent with "".replace("", s).

Now any valid expression can be used as a decorator. Previously, syntax restrictions were stricter.

Now prohibits concurrent execution of aclose()/asend()/athrow(), and ag_running reflects the actual running state of the asynchronous generator.

In the in operator and the operator module’s contains(), indexOf(), and countOf() functions, unexpected errors that occur when calling the __iter__ method are no longer masked by TypeError.

In comprehensions and generator expressions, unparenthesized lambda expressions can no longer be used as expression parts.

Changes to Built-in Modules:

Change

Status

asyncio

Due to significant security issues, the asyncio.loop.create_datagram_endpoint() ‘s reuse_address parameter is no longer supported.

New coroutine shutdown_default_executor() to schedule the shutdown of the default executor, waiting for the ThreadPoolExecutor to complete shutdown. Additionally, asyncio.run() has been updated to use this new coroutine.

New asyncio.PidfdChildWatcher, a Linux-specific subprocess monitor implementation for polling process file descriptors.

New coroutine asyncio.to_thread().

When cancelling tasks due to timeout, asyncio.wait_for() now waits for cancellation to complete even in cases where the timeout is <= 0, just like when handling positive timeout values.

When calling incompatible methods on ssl.SSLSocket sockets, asyncio now raises TypeError.

Leave a Comment