circuitpython/shared-bindings
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
..
_bleio protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
_pew Refactor deinit check to reduce code size. 2019-06-12 11:36:43 -07:00
_pixelbuf pixelbuf: correct trivial typos 2019-10-27 16:41:56 -05:00
_stage Fix transactions in _stage after displayio changes 2019-09-04 16:41:58 +02:00
analogio Fix unsafe floating point comparison 2019-10-14 09:13:35 +02:00
audiobusio shared-bindings: I2SOut: Ensure object is deinitialised 2019-11-25 09:53:55 -06:00
audiocore enable Mixer backwards compatibility (when audiomixer is available) 2019-08-31 16:06:16 -05:00
audioio audioio: correct trivial typo 2019-10-27 16:42:18 -05:00
audiomixer doc cleanup 2019-09-03 22:44:43 -05:00
audiopwmio docs: correct audiomixer refs 2019-08-25 14:57:29 -05:00
bitbangio More size_t usage 2019-08-27 12:49:46 -07:00
board make UART.write be blocking on SAMD; add timeout property 2019-11-27 13:05:29 -05:00
busio protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
digitalio Fix docs in digitalio. 2019-06-18 16:59:05 -05:00
displayio Merge pull request #2101 from matthewnewberg/display_io_dither 2019-09-09 10:11:07 -07:00
fontio Fix reference to Bitmap in fontio.Glyph docs. 2019-06-18 18:44:50 -05:00
frequencyio Refactor deinit check to reduce code size. 2019-06-12 11:36:43 -07:00
gamepad When clearing gamepad buffer, use the last button state, not 0 2019-06-12 11:42:41 -07:00
gamepadshift When clearing gamepad buffer, use the last button state, not 0 2019-06-12 11:42:41 -07:00
i2cslave shared-bindings: Use RUN_BACKGROUND_TASKS 2019-08-11 08:53:02 -05:00
math Support internationalisation. 2018-08-07 14:58:57 -07:00
microcontroller revert error 2019-11-15 13:04:25 -05:00
multiterminal Support internationalisation. 2018-08-07 14:58:57 -07:00
neopixel_write Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
network Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
nvm Fix up single-byte access to nvm.ByteArray 2019-04-16 14:53:44 +10:00
os Compress all translated strings with Huffman coding. 2018-08-16 17:40:57 -07:00
ps2io Refactor deinit check to reduce code size. 2019-06-12 11:36:43 -07:00
pulseio Note behavior in documentation 2019-10-25 10:56:41 -07:00
random Support internationalisation. 2018-08-07 14:58:57 -07:00
rotaryio Refactor deinit check to reduce code size. 2019-06-12 11:36:43 -07:00
rtc Improve documentation for `rtc`. 2019-10-23 15:48:34 -07:00
socket protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
storage Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
struct Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
supervisor Remove nRF52832 support 2018-12-30 22:49:20 -05:00
terminalio protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
time time: struct_time: allow construction like a namedtuple, too 2019-11-26 08:45:44 -06:00
touchio Detect lack of pulldown; check for pin in use 2019-08-19 23:40:34 -04:00
uheap Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
usb_hid A variety of displayio improvements 2019-02-11 20:55:05 -08:00
usb_midi protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
ustack Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
wiznet Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
help.c Improve rST consistency for rst2pyi use 2019-05-30 19:02:47 -07:00
index.rst update rST ref link for support matrix 2019-07-28 21:25:43 -05:00
support_matrix.rst update rST ref link for support matrix 2019-07-28 21:25:43 -05:00
util.c Refactor deinit check to reduce code size. 2019-06-12 11:36:43 -07:00
util.h Refactor deinit check to reduce code size. 2019-06-12 11:36:43 -07:00