Commit Graph

28 Commits

Author SHA1 Message Date
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
Scott Shawcroft 0318a9a9bc
More make_new fixes for unix build 2019-01-18 11:53:09 -08:00
Damien George e8398a5856 extmod: Update to use new mp_get_stream helper.
With this patch objects are only checked that they have the stream protocol
at the start of their use as a stream, and afterwards the efficient
mp_get_stream() helper is used to extract the stream protocol C methods.
2018-06-18 12:35:56 +10:00
Damien George cf31d384f1 py/stream: Switch stream close operation from method to ioctl.
This patch moves the implementation of stream closure from a dedicated
method to the ioctl of the stream protocol, for each type that implements
closing.  The benefits of this are:

1. Rounds out the stream ioctl function, which already includes flush,
   seek and poll (among other things).

2. Makes calling mp_stream_close() on an object slightly more efficient
   because it now no longer needs to lookup the close method and call it,
   rather it just delegates straight to the ioctl function (if it exists).

3. Reduces code size and allows future types that implement the stream
   protocol to be smaller because they don't need a dedicated close method.

Code size reduction is around 200 bytes smaller for x86 archs and around
30 bytes smaller for the bare-metal archs.
2018-04-10 13:41:32 +10:00
Damien George a3dc1b1957 all: Remove inclusion of internal py header files.
Header files that are considered internal to the py core and should not
normally be included directly are:
    py/nlr.h - internal nlr configuration and declarations
    py/bc0.h - contains bytecode macro definitions
    py/runtime0.h - contains basic runtime enums

Instead, the top-level header files to include are one of:
    py/obj.h - includes runtime0.h and defines everything to use the
        mp_obj_t type
    py/runtime.h - includes mpstate.h and hence nlr.h, obj.h, runtime0.h,
        and defines everything to use the general runtime support functions

Additional, specific headers (eg py/objlist.h) can be included if needed.
2017-10-04 12:37:50 +11:00
Damien George aa7be82a4d all: Don't include system errno.h when it's not needed. 2017-07-24 18:43:14 +10:00
Ville Skyttä ca16c38210 various: Spelling fixes 2017-05-29 11:36:05 +03:00
Damien George 93c4a6a3f7 all: Remove 'name' member from mp_obj_module_t struct.
One can instead lookup __name__ in the modules dict to get the value.
2016-09-22 00:23:16 +10:00
Paul Sokolovsky cbef0dba2e extmod/modwebsocket: Use mp_rom_map_elem_t and friends. 2016-08-06 16:18:03 +03:00
Paul Sokolovsky e0d49b7e1b extmod/modwebsocket: Make compatible with non-default object models. 2016-08-06 15:53:16 +03:00
Paul Sokolovsky a931c4eeec extmod/modwebsocket: Add readline method.
This goes bit against websocket nature (message-based communication),
as it ignores boundaries bertween messages, but may be very practical
to do simple things with websockets.
2016-08-06 15:13:26 +03:00
Paul Sokolovsky 03b0279c6d extmod/modwebsocket: Add readinto() method. 2016-07-02 14:03:49 +03:00
Paul Sokolovsky 07209f8592 all: Rename mp_obj_type_t::stream_p to protocol.
It's now used for more than just stream protocol (e.g. pin protocol), so
don't use false names.
2016-06-18 18:44:57 +03:00
Paul Sokolovsky ccf4e5ab7b extmod/modwebsocket: Add close() method. 2016-05-20 21:18:49 +03:00
Paul Sokolovsky 7f7c84b10a py/stream: Support both "exact size" and "one underlying call" operations.
Both read and write operations support variants where either a) a single
call is made to the undelying stream implementation and returned buffer
length may be less than requested, or b) calls are repeated until requested
amount of data is collected, shorter amount is returned only in case of
EOF or error.

These operations are available from the level of C support functions to be
used by other C modules to implementations of Python methods to be used in
user-facing objects.

The rationale of these changes is to allow to write concise and robust
code to work with *blocking* streams of types prone to short reads, like
serial interfaces and sockets. Particular object types may select "exact"
vs "once" types of methods depending on their needs. E.g., for sockets,
revc() and send() methods continue to be "once", while read() and write()
thus converted to "exactly" versions.

These changes don't affect non-blocking handling, e.g. trying "exact"
method on the non-blocking socket will return as much data as available
without blocking. No data available is continued to be signaled as None
return value to read() and write().

From the point of view of CPython compatibility, this model is a cross
between its io.RawIOBase and io.BufferedIOBase abstract classes. For
blocking streams, it works as io.BufferedIOBase model (guaranteeing
lack of short reads/writes), while for non-blocking - as io.RawIOBase,
returning None in case of lack of data (instead of raising expensive
exception, as required by io.BufferedIOBase). Such a cross-behavior
should be optimal for MicroPython needs.
2016-05-18 02:41:45 +03:00
Paul Sokolovsky 480c212009 extmod/modwebsocket: Handle CLOSE control frame.
This fixes situation when clients hangs waiting for disconnect and does
so only on timeout.
2016-04-27 12:49:30 +03:00
Paul Sokolovsky 59a4fee516 extmod/modwebsocket: Another case to propagate EOF. 2016-04-13 22:17:09 +03:00
Paul Sokolovsky f8fb4470a0 extmod/modwebsocket: write(): Support write size beyond 125 bytes. 2016-04-11 14:07:57 +03:00
Paul Sokolovsky b69f798c92 extmod/modwebsocket.h: Split websocket-related defines for reuse. 2016-04-10 13:42:51 +03:00
Paul Sokolovsky d6236e85c2 extmod/modwebsocket: Implement MP_STREAM_SET_DATA_OPTS ioctl.
Allows to set fragment type (txt/bin/etc.) for output records.
2016-04-10 13:19:26 +03:00
Paul Sokolovsky 6837dba6b8 extmod/modwebsocket: Allow to get type of last read data using ioctl(). 2016-04-10 12:50:46 +03:00
Paul Sokolovsky f38e8f5217 extmod/modwebsocket: Record current fragment type (binary/text/etc.)
Also, handle continuation frames (untested).
2016-04-09 16:14:47 +03:00
Paul Sokolovsky 5b1c221785 extmod/modwebsocket: Add option for blocking writes to non-blk sockets.
This is strange asymmetry which is sometimes needed, e.g. for WebREPL: we
want to process only available input and no more; but for output, we want
to get rid of all of it, because there's no other place to buffer/store
it. This asymmetry is akin to CPython's asyncio asymmetry, where reads are
asynchronous, but writes are synchronous (asyncio doesn't expect them to
block, instead expects there to be (unlimited) buffering for any sync write
to completely immediately).
2016-04-09 16:03:38 +03:00
Paul Sokolovsky 397b705647 extmod/modwebsocket: Reset mask between packets. 2016-04-09 12:29:18 +03:00
Paul Sokolovsky 7e9182f3aa extmod/modwebsocket: Make sure to propagate EOF. 2016-04-08 20:26:54 +03:00
Paul Sokolovsky 05ba2433f6 extmod/modwebsocket: Properly check number of args to constructor. 2016-04-08 16:05:48 +03:00
Paul Sokolovsky 3dbd2ee926 extmod/modwebsocket: Implement read support. 2016-03-25 00:51:51 +02:00
Paul Sokolovsky 24342dd65e extmod/modwebsocket: Start module for WebSocket helper functions.
Currently, only write support is implemented (of limited buffer size).
2016-03-24 19:16:00 +02:00