You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
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
4 years ago
..
_bleio protocols: Allow them to be (optionally) type-safe 4 years ago
_pew Refactor deinit check to reduce code size. 4 years ago
_pixelbuf pixelbuf: correct trivial typos 4 years ago
_stage Fix transactions in _stage after displayio changes 4 years ago
analogio Fix unsafe floating point comparison 4 years ago
audiobusio shared-bindings: I2SOut: Ensure object is deinitialised 4 years ago
audiocore enable Mixer backwards compatibility (when audiomixer is available) 4 years ago
audioio audioio: correct trivial typo 4 years ago
audiomixer doc cleanup 4 years ago
audiopwmio docs: correct audiomixer refs 4 years ago
bitbangio More size_t usage 4 years ago
board make UART.write be blocking on SAMD; add timeout property 4 years ago
busio protocols: Allow them to be (optionally) type-safe 4 years ago
digitalio Fix docs in digitalio. 4 years ago
displayio Merge pull request #2101 from matthewnewberg/display_io_dither 4 years ago
fontio Fix reference to Bitmap in fontio.Glyph docs. 4 years ago
frequencyio Refactor deinit check to reduce code size. 4 years ago
gamepad When clearing gamepad buffer, use the last button state, not 0 4 years ago
gamepadshift When clearing gamepad buffer, use the last button state, not 0 4 years ago
i2cslave shared-bindings: Use RUN_BACKGROUND_TASKS 4 years ago
math Support internationalisation. 5 years ago
microcontroller revert error 4 years ago
multiterminal Support internationalisation. 5 years ago
neopixel_write Improve rST consistency for rst2pyi use 4 years ago
network Improve rST consistency for rst2pyi use 4 years ago
nvm Fix up single-byte access to nvm.ByteArray 4 years ago
os Compress all translated strings with Huffman coding. 5 years ago
ps2io Refactor deinit check to reduce code size. 4 years ago
pulseio Note behavior in documentation 4 years ago
random Support internationalisation. 5 years ago
rotaryio Refactor deinit check to reduce code size. 4 years ago
rtc Improve documentation for `rtc`. 4 years ago
socket protocols: Allow them to be (optionally) type-safe 4 years ago
storage Improve rST consistency for rst2pyi use 4 years ago
struct Improve rST consistency for rst2pyi use 4 years ago
supervisor Remove nRF52832 support 4 years ago
terminalio protocols: Allow them to be (optionally) type-safe 4 years ago
time time: struct_time: allow construction like a namedtuple, too 4 years ago
touchio Detect lack of pulldown; check for pin in use 4 years ago
uheap Improve rST consistency for rst2pyi use 4 years ago
usb_hid A variety of displayio improvements 4 years ago
usb_midi protocols: Allow them to be (optionally) type-safe 4 years ago
ustack Improve rST consistency for rst2pyi use 4 years ago
wiznet Improve rST consistency for rst2pyi use 4 years ago
help.c Improve rST consistency for rst2pyi use 4 years ago
index.rst update rST ref link for support matrix 4 years ago
support_matrix.rst update rST ref link for support matrix 4 years ago
util.c Refactor deinit check to reduce code size. 4 years ago
util.h Refactor deinit check to reduce code size. 4 years ago