Builtin types, functions and exceptions (builtins)

All builtin functions and exceptions are described here. They are also available via builtins module.

Functions and types

abs()
all()
any()
bin()
class bool
class bytearray
class bytes

See CPython documentation: bytes.

callable()
chr()
classmethod()
compile()
class complex
delattr(obj, name)

The argument name should be a string, and this function deletes the named attribute from the object given by obj.

class dict
dir()
divmod()
enumerate()
eval()
exec()
filter()
class float
class frozenset
getattr()
globals()
hasattr()
hash()
hex()
id()
input()
class int
classmethod from_bytes(bytes, byteorder)

In Pycopy, byteorder parameter must be positional (this is compatible with CPython).

to_bytes(size, byteorder)

In Pycopy, byteorder parameter must be positional (this is compatible with CPython).

isinstance()
issubclass()
iter()
len()
class list
locals()
map()
max()
class memoryview(buffer)
class memoryview(buffer, offset, size)

See CPython documentation: memoryview.

Pycopy extension

A str object can be passed to memoryview() constructor, in which case underlying UTF-8 encoded data can be accessed by memoryview. memoryview("abc") is equivalent to memoryview("abc".encode()), but is more memory-efficient.

Pycopy extension

Additional offset and size parameters can be passed to constructor. mv = memoryview(buf, offset, size) is equivalent to mv1 = memoryview(buf); mv = mv1[offset:offset + size], but is more memory-efficient.

init(buffer[, offset, size])

Reinitialize existing memoryview object inplace with a new buffer, and optionally given offset and size into it.

Pycopy extension

This method is a Pycopy extension. It doesn’t require memory allocation. Just as the constructor, this method can be called for str objects too.

Example of usage of Pycopy extensions:

# A function with zero memory allocation which can write either
# bytes or str objects to a stream, in the presense of short writes.

# Pre-allocate static memoryview buffer
bufptr = memoryview(b"")

def write_any(stream, data):
    bufptr.init(data)
    # We need size in bytes, not in characters in case of Unicode
    # strings, and size of memoryview gives us that.
    sz = len(bufptr)
    offset = 0
    while sz:
        # We use another Pycopy extension, 3-argument stream
        # .write() method. We could use memoryview.init() method
        # to update "visible" part of underlying buffer instead.
        written = stream.write(bufptr, offset, sz)
        offset += written
        sz -= written
min()
next()
class object
oct()
open()
ord()
pow()
print()
property()
range()
repr()
reversed()
round()
class set
setattr()
class slice

The slice builtin is the type that slice objects have.

sorted()
staticmethod()
class str

In Pycopy, strings are backed by Unicode UTF-8 encoding. While this fact doesn’t have to be known by normal users, advanced users may use it to optimize applications/libraries.

sum()
super()
class tuple
type()
zip()

Exceptions

exception AssertionError
exception AttributeError
exception Exception
exception ImportError
exception IndexError
exception KeyboardInterrupt
exception KeyError
exception MemoryError
exception NameError
exception NotImplementedError
exception OSError

Represents system-level error, related to the operating system or hardware. See CPython documentation: OSError for general information.

Pycopy doesn’t implement errno attribute, instead use the standard way to access exception arguments: exc.args[0] (this is compatible with CPython).

Pycopy doesn’t implement subclasses of this exception for specific error codes. E.g., instead of TimeoutError, OSError(ETIMEDOUT) is used directly (this is compatible with CPython).

Pycopy also uses this exception in some cases where instead of standard system errno code, a different error code is provided (e.g. a more detailed/domain specific code). In this case, OSErrno arguments are a tuple of size 2, where first argument is an error code, and 2nd is first string, representing namespace of the error code. Whenever possible, error codes for such namespaced errors should be negative. This allows to easily distinguish errors in the default errno namespace from namespaced errors - checking exc.args[0] is then enough to distinguish system error from namespaced error, and then exc.args[1] can be consulted to get the namespace. Example of namespaced OSError from usocket.getaddrinfo() function:

>>> usocket.getaddrinfo("-doesnt-exist-", 80)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: (-3, 'gai')
exception RuntimeError
exception StopIteration
exception SyntaxError
exception SystemExit

See CPython documentation: SystemExit.

exception TypeError

See CPython documentation: TypeError.

exception ValueError
exception ZeroDivisionError