aboutsummaryrefslogtreecommitdiff
path: root/py/stream.c
AgeCommit message (Collapse)Author
2020-04-27py/stream: Remove mp_stream_errno and use system errno instead.Damien George
This change is made for two reasons: 1. A 3rd-party library (eg berkeley-db-1.xx, axtls) may use the system provided errno for certain errors, and yet MicroPython stream objects that it calls will be using the internal mp_stream_errno. So if the library returns an error it is not known whether the corresponding errno code is stored in the system errno or mp_stream_errno. Using the system errno in all cases (eg in the mp_stream_posix_XXX wrappers) fixes this ambiguity. 2. For systems that have threading the system-provided errno should always be used because the errno value is thread-local. For systems that do not have an errno, the new lib/embed/__errno.c file is provided.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-01-09py: Make mp_obj_get_type() return a const ptr to mp_obj_type_t.Damien George
Most types are in rodata/ROM, and mp_obj_base_t.type is a constant pointer, so enforce this const-ness throughout the code base. If a type ever needs to be modified (eg a user type) then a simple cast can be used.
2019-02-06py: Update my copyright info on some files.Paul Sokolovsky
Based on git history.
2018-08-14py/stream: Adjust mp_stream_posix_XXX to take void*, not mp_obj_t.Damien George
These POSIX wrappers are assumed to be passed a concrete stream object so it is more efficient (eg on nan-boxing builds) to pass in the pointer rather than mp_obj_t, because then the users of these functions only need to store a void* (and mp_obj_t may be wider than a pointer). And things would be further improved if the stream protocol functions eventually took a pointer as their first argument (instead of an mp_obj_t). This patch is a step to getting ussl/axtls compiling on nan-boxing builds. See issue #3085.
2018-06-20py/stream: Remove stray empty line at start of file.Damien George
This was accidentally added in 6abede2ca9e221b6aefcaccbda0c89e367507df1
2018-06-20py/stream: Update comment for mp_stream_write_adaptor.Damien George
2018-06-18py/stream: Introduce and use efficient mp_get_stream to access stream_p.Damien George
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.
2018-05-01py/stream: Use uPy errno instead of system's for non-blocking check.Ayke van Laethem
This is a more consistent use of errno codes. For example, it may be that a stream returns MP_EAGAIN but the mp_is_nonblocking_error() macro doesn't catch this value because it checks for EAGAIN instead (which may have a different value than MP_EAGAIN when MICROPY_USE_INTERNAL_ERRNO is enabled).
2018-04-10py/stream: Switch stream close operation from method to ioctl.Damien George
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.
2017-10-04all: Remove inclusion of internal py header files.Damien George
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-09-21py/stream: Remove unnecessary checks for NULL return from vstr_add_len.Damien George
The vstr argument to the calls to vstr_add_len are dynamically allocated (ie fixed_buf=false) and so vstr_add_len will never return NULL. So there's no need to check for it. Any out-of-memory errors are raised by the call to m_renew in vstr_ensure_extra.
2017-08-20py/stream: seek: Consistently handle negative offset for SEEK_SET.Paul Sokolovsky
Per POSIX, this is EINVAL, so raises OSError(EINVAL).
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-06-15all: Make more use of mp_raise_{msg,TypeError,ValueError} helpers.Damien George
2017-05-29various: Spelling fixesVille Skyttä
2016-11-14all: Remove readall() method, which is equivalent to read() w/o args.Paul Sokolovsky
Its addition was due to an early exploration on how to add CPython-like stream interface. It's clear that it's not needed and just takes up bytes in all ports.
2016-10-27py/stream: Typo fix in comment.Paul Sokolovsky
2016-10-17py: Use mp_raise_msg helper function where appropriate.Damien George
Saves the following number of bytes of code space: 176 for bare-arm, 352 for minimal, 272 for unix x86-64, 140 for stmhal, 120 for esp8266.
2016-10-07py: Add mp_raise_OSError(errno) helper function.Damien George
This is an often used code pattern, and its use reduces code size of the core by about 100 bytes.
2016-09-22py/stream: Remove unnecessary check for NULL return from vstr_extend.Damien George
vstr_extend will now only return NULL if the vstr is a fixed buffer, which in this case it is not.
2016-08-24py/stream.c: use mp_obj_get_type in mp_get_stream_raiseKrzysztof Blazewicz
In current state `mp_get_stream_raise` assumes that `self_in` is an object and always performs a pointer derefence which may cause a segfault. This function shall throw an exception whenever `self_in` does not implement a stream protocol, that includes qstr's and numbers. fixes #2331
2016-07-30py/mpconfig.h: Add MICROPY_STREAMS_POSIX_API setting.Paul Sokolovsky
To filter out even prototypes of mp_stream_posix_*() functions, which require POSIX types like ssize_t & off_t, which may be not available in some ports.
2016-07-30py/stream: Add adapter methods with POSIX-compatible signatures.Paul Sokolovsky
Previoussly such read() and write() methods were used by modussl_axtls, move to py/stream for reuse.
2016-07-27py/stream: Implement generic flush() method, in terms of C-level ioctl.Paul Sokolovsky
2016-07-14py/stream: Implement 2- and 3-arg write() method as an extension to CPython.Paul Sokolovsky
3-arg form: stream.write(data, offset, length) 2-arg form: stream.write(data, length) These allow efficient buffer writing without incurring extra memory allocation for slicing or creating memoryview() object, what is important for low-memory ports. All arguments must be positional. It might be not so bad idea to standardize on 3-arg form, but 2-arg case would need check and raising an exception anyway then, so instead it was just made to work.
2016-06-18all: Rename mp_obj_type_t::stream_p to protocol.Paul Sokolovsky
It's now used for more than just stream protocol (e.g. pin protocol), so don't use false names.
2016-05-20py/stream: Add mp_stream_close() helper function.Paul Sokolovsky
2016-05-18py/stream: Support both "exact size" and "one underlying call" operations.Paul Sokolovsky
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-04-10py/stream: Move uPy func obj wrappers to below their respective funcs.Damien George
2016-04-10py/stream: Simplify arg extraction logic for stream_ioctl.Damien George
Saves 16 bytes of code. Also, use mp_obj_get_int_truncated to allow integers as big as a machine word to be passed as the value.
2016-04-10py/stream: ioctl(): Properly support 2-arg form.Paul Sokolovsky
2016-04-10py/stream: Fix signed comparison issue.Paul Sokolovsky
2016-04-10py/stream: Add Python-level ioctl() method.Paul Sokolovsky
Will call underlying C virtual methods of stream interface. This isn't intended to be added to every stream object (it's not in CPython), but is convenient way to expose extra operation on Python side without adding bunch of Python-level methods.
2016-03-27py/stream: Fix stupid thinko with variable naming/shadowing.Paul Sokolovsky
2016-03-24py/stream: Fix object vs ptr usecase in mp_stream_writeall().Paul Sokolovsky
2016-03-24py/stream: Add mp_stream_writeall() helper function.Paul Sokolovsky
Spools entire output buffer to a blocking stream (chunk by chunk if needed).
2016-01-11py: Change type signature of builtin funs that take variable or kw args.Damien George
With this patch the n_args parameter is changed type from mp_uint_t to size_t.
2015-12-09py: Add mp_get_stream_raise to factor out check for stream methods.Damien George
2015-11-29py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.Damien George
This allows the mp_obj_t type to be configured to something other than a pointer-sized primitive type. This patch also includes additional changes to allow the code to compile when sizeof(mp_uint_t) != sizeof(void*), such as using size_t instead of mp_uint_t, and various casts.
2015-11-29py: Change mp_print_strn_t func type to use size_t for the str length.Damien George
2015-10-18py/stream: Allow to reuse is_nonblocking_error().Paul Sokolovsky
2015-08-13py: Add stream_tell method, and use for unix and stmhal file tell.blmorris
2015-05-12py: Add mp_obj_get_int_truncated and use it where appropriate.Damien George
mp_obj_get_int_truncated will raise a TypeError if the argument is not an integral type. Use mp_obj_int_get_truncated only when you know the argument is a small or big int.
2015-01-28py: Change vstr so that it doesn't null terminate buffer by default.Damien George
This cleans up vstr so that it's a pure "variable buffer", and the user can decide whether they need to add a terminating null byte. In most places where vstr is used, the vstr did not need to be null terminated and so this patch saves code size, a tiny bit of RAM, and makes vstr usage more efficient. When null termination is needed it must be done explicitly using vstr_null_terminate.
2015-01-24stream: readall(): Make sure there's a trailing NUL char.Paul Sokolovsky
2015-01-23stream: Fix readall() implementation in respect to NUL terminator bytes.Paul Sokolovsky
After vstr refactor. Fixes #1084.
2015-01-21py: Remove mp_obj_str_builder and use vstr instead.Damien George
With this patch str/bytes construction is streamlined. Always use a vstr to build a str/bytes object. If the size is known beforehand then use vstr_init_len to allocate only required memory. Otherwise use vstr_init and the vstr will grow as needed. Then use mp_obj_new_str_from_vstr to create a str/bytes object using the vstr memory. Saves code ROM: 68 bytes on stmhal, 108 bytes on bare-arm, and 336 bytes on unix x64.
2015-01-21py: Add mp_obj_new_str_from_vstr, and use it where relevant.Damien George
This patch allows to reuse vstr memory when creating str/bytes object. This improves memory usage. Also saves code ROM: 128 bytes on stmhal, 92 bytes on bare-arm, and 88 bytes on unix x64.