circuitpython/ports/unix
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
..
coverage-frzmpy
coverage-frzstr
modules
.gitignore
alloc.c
coverage.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
fatfs_port.c
fdfile.h
file.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
gccollect.c
input.c
input.h
main.c WIP: after merge; before testing 2018-07-11 16:45:30 -04:00
Makefile Fix unix build by using safe mode stub 2019-03-12 11:56:35 -07:00
modffi.c More make_new fixes for unix build 2019-01-18 11:53:09 -08:00
modjni.c py/objstr: Remove "make_qstr_if_not_already" arg from mp_obj_new_str. 2017-11-16 13:17:51 +11:00
modmachine.c Fix nrf and unix 2018-08-16 17:41:38 -07:00
modos.c unix/moduos_vfs: Add missing uos functions from traditional uos module. 2018-06-06 14:28:23 +10:00
modtermios.c
modtime.c Merge commit 'f869d6b2e339c04469c6c9ea3fb2fabd7bbb2d8c' into nrf2_merge 2017-10-24 22:31:16 -07:00
moduos_vfs.c unix/moduos_vfs: Add missing uos functions from traditional uos module. 2018-06-06 14:28:23 +10:00
moduselect.c unix/moduselect: Fix nanbox build after adding .dump() method. 2017-11-07 01:13:19 +02:00
modusocket.c protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
mpconfigport_coverage.h WIP: after merge; before testing 2018-07-11 16:45:30 -04:00
mpconfigport_fast.h
mpconfigport_freedos.h
mpconfigport_minimal.h
mpconfigport_nanbox.h
mpconfigport.h ports: Enable IOBase on unix, stm32, esp8266 and esp32. 2018-06-12 12:29:26 +10:00
mpconfigport.mk
mphalport.h
mpthreadport.c unix: Add support for using the Python stack. 2017-12-11 13:49:09 +11:00
mpthreadport.h
qstrdefsport.h
unix_mphal.c extmod/uos_dupterm: Update uos.dupterm() and helper funcs to have index. 2017-10-13 20:01:57 +11:00