circuitpython/lib
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
..
axtls@43a6e6bd3b WIP: complete manual inspection of all significant changes 2018-07-23 21:34:25 -04:00
berkeley-db-1.xx@35aaec4418 berkeley-db-1.xx: Update, allow to override MINCACHE, DEFPSIZE. 2017-09-10 13:51:51 +03:00
cmsis/inc lib/cmsis: upgrade CMSIS-CORE to V4.30 2016-09-30 16:19:03 +10:00
embed Two fixes and translate more strings. 2018-08-09 13:29:30 -07:00
libc Initial merge of micropython v1.9.2 into circuitpython 2.0.0 (in development) master. 2017-08-25 22:17:07 -04:00
libffi@e9de7e35f2 lib/libffi: Add libffi as a submodule. 2015-10-17 15:52:35 +03:00
libm merge from 2.2.0 + fix up board defs 2018-01-02 21:25:41 -05:00
libm_dbl Fix a couple variable shadowing varnings. 2019-02-07 20:10:43 +01:00
memzip lib/memzip: Make lexer constructor raise exception when file not found. 2017-03-14 11:52:05 +11:00
mp-readline merge finished 2018-07-28 13:29:47 -04:00
netutils Fix esp and samd 2018-08-16 17:41:35 -07:00
nrfutil@9e7dfb28a5 update nrfutil to 0.5.2d with singlebank option 2018-05-17 15:20:07 +07:00
oofatfs Add fast seek support to file objects 2019-02-03 13:41:20 -08:00
timeutils Fix os.stat() to use 1970 epoch 2018-08-25 20:43:02 +02:00
tinytest lib/tinytest: Clean up test reporting in the presence of stdout output. 2017-12-15 19:41:08 +02:00
tinyusb@e413c9efa3 add dummy common_hal_mcu_processor_get_voltage() for spresense 2019-10-15 09:07:12 -04:00
upytesthelper lib/upytesthelper: MicroPython test helper layer on top of tinytest. 2017-12-13 09:07:51 +02:00
utils protocols: Allow them to be (optionally) type-safe 2019-12-04 09:29:57 -06:00
uzlib@f966da0fab uzlib: Upgrade to uzlib v2.9 release 2018-09-09 20:22:25 -05:00
README.md lib: Add basic README. 2014-09-18 00:13:03 +01:00

README.md

This directory contains standard, low-level C libraries with emphasis on being independent and efficient. They can be used by any port.