circuitpython/extmod
Jeff Epler 238e121236 protocols: Allow them to be (optionally) type-safe
Protocols are nice, but there is no way for C code to verify whether
a type's "protocol" structure actually implements some particular
protocol.  As a result, you can pass an object that implements the
"vfs" protocol to one that expects the "stream" protocol, and the
opposite of awesomeness ensues.

This patch adds an OPTIONAL (but enabled by default) protocol identifier
as the first member of any protocol structure.  This identifier is
simply a unique QSTR chosen by the protocol designer and used by each
protocol implementer.  When checking for protocol support, instead of
just checking whether the object's type has a non-NULL protocol field,
use `mp_proto_get` which implements the protocol check when possible.

The existing protocols are now named:
    protocol_framebuf
    protocol_i2c
    protocol_pin
    protocol_stream
    protocol_spi
    protocol_vfs
(most of these are unused in CP and are just inherited from MP; vfs and
stream are definitely used though)

I did not find any crashing examples, but here's one to give a flavor of what
is improved, using `micropython_coverage`.  Before the change,
the vfs "ioctl" protocol is invoked, and the result is not intelligible
as json (but it could have resulted in a hard fault, potentially):

    >>> import uos, ujson
    >>> u = uos.VfsPosix('/tmp')
    >>> ujson.load(u)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: syntax error in JSON

After the change, the vfs object is correctly detected as not supporting
the stream protocol:
    >>> ujson.load(p)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: stream operation not supported
2019-12-04 09:29:57 -06:00
..
crypto-algorithms extmod/crypto-algorithms/sha256: Remove non-standard memory.h header. 2017-04-27 15:01:01 +03:00
lwip-include WIP: complete manual inspection of all significant changes 2018-07-23 21:34:25 -04:00
re1.5 extmod/ure: Handle some escape sequences. 2019-02-14 15:42:25 +01:00
font_petme128_8x8.h Move font dependency to extmod from stm32 2019-10-13 19:33:03 -04:00
machine_i2c.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
machine_i2c.h protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
machine_mem.c Compress all translated strings with Huffman coding. 2018-08-16 17:40:57 -07:00
machine_mem.h all: Use the name MicroPython consistently in comments 2017-07-31 18:35:40 +10:00
machine_pinbase.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
machine_pinbase.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
machine_pulse.c extmod/machine_pulse: Make time_pulse_us() not throw exceptions. 2017-02-05 14:20:17 +03:00
machine_pulse.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
machine_signal.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
machine_signal.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
machine_spi.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
machine_spi.h protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
misc.h extmod/uos_dupterm: Update uos.dupterm() and helper funcs to have index. 2017-10-13 20:01:57 +11:00
modbtree.c py/runtime: Add MP_BINARY_OP_CONTAINS as reverse of MP_BINARY_OP_IN. 2017-11-24 14:48:23 +11:00
modframebuf.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
modlwip.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
modonewire.c extmod/mod{lwip,onewire,webrepl}: Convert to mp_rom_map_elem_t. 2017-07-29 18:24:16 +03:00
modubinascii.c Compress all translated strings with Huffman coding. 2018-08-16 17:40:57 -07:00
modubinascii.h all: Use the name MicroPython consistently in comments 2017-07-31 18:35:40 +10:00
moductypes.c More make_new fixes for unix build 2019-01-18 11:53:09 -08:00
moduhashlib.c fixup micropy 2019-01-14 18:09:02 -08:00
moduheapq.c Fix esp and samd 2018-08-16 17:41:35 -07:00
modujson.c Add CIRCUITPY macro; rename u* only when CIRCUITPY=1 2019-02-21 11:09:44 -05:00
modurandom.c all: Remove inclusion of internal py header files. 2017-10-04 12:37:50 +11:00
modure.c conditionalize re object type name 2019-02-21 15:11:54 -05:00
moduselect.c Initial merge of micropython v1.9.2 into circuitpython 2.0.0 (in development) master. 2017-08-25 22:17:07 -04:00
modussl_axtls.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
modussl_mbedtls.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
modutimeq.c More make_new fixes for unix build 2019-01-18 11:53:09 -08:00
moduzlib.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
modwebrepl.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
modwebsocket.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
modwebsocket.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
uos_dupterm.c Fix esp and samd 2018-08-16 17:41:35 -07:00
utime_mphal.c py,extmod: Some casts and minor refactors to quiet compiler warnings. 2017-07-07 11:32:22 +10:00
utime_mphal.h all: Unify header guard usage. 2017-07-18 11:57:39 +10:00
vfs.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
vfs.h protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
vfs_fat.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
vfs_fat.h Add option to disable the concurrent write protection 2019-02-21 10:45:41 -08:00
vfs_fat_diskio.c merge finished 2018-07-28 13:29:47 -04:00
vfs_fat_file.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
vfs_posix.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
vfs_posix.h continued WIP: almost compiling 2018-07-12 14:13:51 -04:00
vfs_posix_file.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
vfs_reader.c py/objstr: Remove "make_qstr_if_not_already" arg from mp_obj_new_str. 2017-11-16 13:17:51 +11:00
virtpin.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
virtpin.h protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00