<feed xmlns='http://www.w3.org/2005/Atom'>
<title>suspect-devices/circuitpython/py/stream.c, branch type_hints</title>
<subtitle>CircuitPython - a Python implementation for teaching coding with microcontrollers</subtitle>
<id>https://git.suspectdevices.com/suspect-devices/circuitpython/atom?h=type_hints</id>
<link rel='self' href='https://git.suspectdevices.com/suspect-devices/circuitpython/atom?h=type_hints'/>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/'/>
<updated>2019-12-04T15:29:57+00:00</updated>
<entry>
<title>protocols: Allow them to be (optionally) type-safe</title>
<updated>2019-12-04T15:29:57+00:00</updated>
<author>
<name>Jeff Epler</name>
<email>jepler@gmail.com</email>
</author>
<published>2019-12-03T20:50:37+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=238e12123690debaf2cf8488fb2a4d4003a06506'/>
<id>urn:sha1:238e12123690debaf2cf8488fb2a4d4003a06506</id>
<content type='text'>
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):

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

After the change, the vfs object is correctly detected as not supporting
the stream protocol:
    &gt;&gt;&gt; ujson.load(p)
    Traceback (most recent call last):
      File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    OSError: stream operation not supported
</content>
</entry>
<entry>
<title>In py/, must guard uses of RUN_BACKGROUND_TASKS</title>
<updated>2019-09-12T02:15:10+00:00</updated>
<author>
<name>jepler</name>
<email>jepler@gmail.com</email>
</author>
<published>2019-09-12T02:15:10+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=70daf007ae84240c1c512ef8e41f2e5540b6eb2e'/>
<id>urn:sha1:70daf007ae84240c1c512ef8e41f2e5540b6eb2e</id>
<content type='text'>
</content>
</entry>
<entry>
<title>stream_readall: This can be long-running, run background tasks</title>
<updated>2019-09-10T01:14:02+00:00</updated>
<author>
<name>jepler</name>
<email>jepler@gmail.com</email>
</author>
<published>2019-09-10T01:04:20+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=932ac0960b01029701e6e53bb5eb9adafb182d41'/>
<id>urn:sha1:932ac0960b01029701e6e53bb5eb9adafb182d41</id>
<content type='text'>
While finding sources of clicks and buzzes in nrf i2sout, I identified
this site as one which could be long running.  Reproducer code was to
play a 22.05kHz sample and repeatedly print `os.listdir('')`
</content>
</entry>
<entry>
<title>Support print("", flush=True)</title>
<updated>2019-02-16T00:53:19+00:00</updated>
<author>
<name>Scott Shawcroft</name>
<email>scott@tannewt.org</email>
</author>
<published>2019-02-16T00:53:19+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=e6b140e7a0cf07fa365269e7801bb111565cb478'/>
<id>urn:sha1:e6b140e7a0cf07fa365269e7801bb111565cb478</id>
<content type='text'>
Fixes #1127
</content>
</entry>
<entry>
<title>CharacteristicBuffer: make it be a stream class; add locking</title>
<updated>2019-01-20T00:45:35+00:00</updated>
<author>
<name>Dan Halbert</name>
<email>halbert@halwitz.org</email>
</author>
<published>2019-01-20T00:45:35+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=28cfd8a5135a1e420b56d693cef20fc1a4721081'/>
<id>urn:sha1:28cfd8a5135a1e420b56d693cef20fc1a4721081</id>
<content type='text'>
</content>
</entry>
<entry>
<title>UART changes: timeout in secs, write bytes, etc.</title>
<updated>2018-12-03T17:04:32+00:00</updated>
<author>
<name>Dan Halbert</name>
<email>halbert@halwitz.org</email>
</author>
<published>2018-12-03T16:00:35+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=80db2cec99ca5c995a0302b1d6d00ad77cfcd736'/>
<id>urn:sha1:80db2cec99ca5c995a0302b1d6d00ad77cfcd736</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Two fixes and translate more strings.</title>
<updated>2018-08-09T20:29:30+00:00</updated>
<author>
<name>Scott Shawcroft</name>
<email>scott@tannewt.org</email>
</author>
<published>2018-08-09T01:24:49+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=96ebf5bc3fb455162286b9bc31e763f3c4f1c457'/>
<id>urn:sha1:96ebf5bc3fb455162286b9bc31e763f3c4f1c457</id>
<content type='text'>
* Fix finding translations with escaped characters.
* Add back \r to translations since its needed by screen.
</content>
</entry>
<entry>
<title>py/stream: Remove stray empty line at start of file.</title>
<updated>2018-06-20T06:26:12+00:00</updated>
<author>
<name>Damien George</name>
<email>damien.p.george@gmail.com</email>
</author>
<published>2018-06-20T06:26:12+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=34344a413fb1939d476f43429a8a4e142f6fe3c0'/>
<id>urn:sha1:34344a413fb1939d476f43429a8a4e142f6fe3c0</id>
<content type='text'>
This was accidentally added in 6abede2ca9e221b6aefcaccbda0c89e367507df1
</content>
</entry>
<entry>
<title>py/stream: Update comment for mp_stream_write_adaptor.</title>
<updated>2018-06-20T05:56:32+00:00</updated>
<author>
<name>Damien George</name>
<email>damien.p.george@gmail.com</email>
</author>
<published>2018-06-20T05:56:32+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=2c8d130f702d07041e30d808b974b0ba2a1e51e0'/>
<id>urn:sha1:2c8d130f702d07041e30d808b974b0ba2a1e51e0</id>
<content type='text'>
</content>
</entry>
<entry>
<title>py/stream: Introduce and use efficient mp_get_stream to access stream_p.</title>
<updated>2018-06-18T02:35:56+00:00</updated>
<author>
<name>Damien George</name>
<email>damien.p.george@gmail.com</email>
</author>
<published>2018-06-13T01:54:44+00:00</published>
<link rel='alternate' type='text/html' href='https://git.suspectdevices.com/suspect-devices/circuitpython/commit/?id=6abede2ca9e221b6aefcaccbda0c89e367507df1'/>
<id>urn:sha1:6abede2ca9e221b6aefcaccbda0c89e367507df1</id>
<content type='text'>
The existing mp_get_stream_raise() helper does explicit checks that the
input object is a real pointer object, has a non-NULL stream protocol, and
has the desired stream C method (read/write/ioctl).  In most cases it is
not necessary to do these checks because it is guaranteed that the input
object has the stream protocol and desired C methods.  For example, native
objects that use the stream wrappers (eg mp_stream_readinto_obj) in their
locals dict always have the stream protocol (or else they shouldn't have
these wrappers in their locals dict).

This patch introduces an efficient mp_get_stream() which doesn't do any
checks and just extracts the stream protocol struct.  This should be used
in all cases where the argument object is known to be a stream.  The
existing mp_get_stream_raise() should be used primarily to verify that an
object does have the correct stream protocol methods.

All uses of mp_get_stream_raise() in py/stream.c have been converted to use
mp_get_stream() because the argument is guaranteed to be a proper stream
object.

This patch improves efficiency of stream operations and reduces code size.
</content>
</entry>
</feed>
