This commit adds support for optional custom argument type validation to
argparse.ArgumentParser, allowing for shorter argument validation code
for both simple builtins and complex types.
For example, assuming that a particular command line argument must be an
integer, using "parser.add_argument('-a', type=int)" will make sure that
any value passed to that argument that cannot be converted into an
integer will trigger an argument validation error.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Commit 2ca1527321 optimized
`FileSection.skip()` for memory use. But that introduced a dependency on
the MicroPython-extension to stream read methods for an additional argument
specifying a maximum read size. This optimization meant that all file-like
objects passed into TarFile must support the extended 2-argument `readinto`
form.
This is problematic for at least two use cases:
1. Nested tar files, because `FileSetion` itself doesn't support 2-argument
`readinto`.
2. Using `mpremote mount` and reading a tar file from the remote mount,
which also doesn't support 2-argument `readinto`.
Instead of requiring all file-like objects to implement this extended form
of `readinto`, this commit changes `FileSection.skip()` so that it doesn't
use this form.
A test is added for this case which fails without the fix here.
Signed-off-by: Damien George <damien@micropython.org>
This is a follow-up to b4565b41ea (PR #1043),
to support closures, async functions and generators in `inspect.signature`.
Signed-off-by: Damien George <damien@micropython.org>
Non-functional changes only:
- Fixed minor spelling mistakes in comments.
- Corrected typos in user-facing strings.
- No variables, logic, or functional code was modified.
Signed-off-by: Marcel Petrick <mail@marcelpetrick.it>
Optimizations applied here are:
- writing once-used helper functions inline in their place of use
- writing once-used constant tuples inline in their place of use (I would
have used `from micropython import const` but that renders the code not
runnable under CPython for testing, and also increases code size itself
for the import)
- renamed _tmod to _t
- renamed _format to _fmt
- optimised timedelta._tuple() slightly
Reduces datetime.mpy by: 8897 -> 8728, so saves 169 bytes.
Signed-off-by: Damien George <damien@micropython.org>
This commit applies the existing `localtz.patch` patch to add support for
naive datetime objects. That is, objects that don't have any info about
the current timezone.
This allows `datetime.datetime.now()` to work; prior to this patch it would
raise NotImplementedError.
Although we don't really have support for localtime vs gmtime on
bare-metal, ports such as the unix port and webassembly port do have this
distinction, and for them being able to do `datetime.datetime.now()` is
quite important (at least, that's what users expect to be able to do).
The associated unittest test has been updated.
This patch changes the size of datetime.mpy: 8466 -> 8897, so +431 bytes.
Signed-off-by: Damien George <damien@micropython.org>
This implements a very basic `inspect.signature()` function.
At the moment it returns only a simple `Signature` instance with a
`parameters` attribute that holds an `OrderedDict` whose length matches the
arity of the input function (the number of arguments it takes).
So, the following code works and is compatible with CPython:
def f(a, b, *, c):
pass
print(len(inspect.signature(f).parameters))
That should print 3.
Signed-off-by: Damien George <damien@micropython.org>
This trivial addition will allow less code differences between standard
Python classes and MicroPython code.
Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
Although `Logger.exception` supports passing exception info with
`exc_info`, when you use `logging.exception` keyword arguments are not
forwarded to the root logger, which makes passing `exc_info` raise
`TypeError`.
Signed-off-by: Nick Budak <thatbudakguy@gmail.com>
Also optimise both `isgenerator()` and `isgeneratorfunction()` so they use
the same lambda, and don't have to create it each time they are called.
Fixes issue #997.
Signed-off-by: Damien George <damien@micropython.org>
So this code can be compiled with the MicroPython native emitter, which
does not support "raise" without any arguments.
Signed-off-by: Damien George <damien@micropython.org>
This base64 library only uses `struct.unpack` which is available in the
built-in `struct` module, so no need for the micropython-lib extras.
Signed-off-by: Damien George <damien@micropython.org>
When running tests from subfolders, import by "full dotted path" rather
than just module name, removing the need to add the test parent folder to
`sys.path`.
This matches CPython more closely, which places `abspath(top)` at the start
of `sys.path` but doesn't include the test file parent dir at all.
It fixes issues where projects may include a `test_xxx.py` file in their
distribution which would (prior to this change) be unintentionally found by
unittest-discover.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Since deque was removed from this repository the built-in one needs to be
used, and that doesn't have unbounded growth. So use a list instead, which
is adequate becasue contextlib only needs append and pop, not double ended
behaviour (the previous pure-Python implementation of deque that was used
here anyway used a list as its storage container).
Also tweak the excessive-nesting test so it uses less memory and can run on
the unix port.
Signed-off-by: Damien George <damien@micropython.org>
This changes almost all uses of "u-module" to just "module" for the
following built-in modules:
- binascii
- collections
- errno
- io
- json
- socket
- struct
- sys
- time
There are some remaining uses of "u-module" naming, for the cases where the
built-in module is extended in Python, eg `python-stdlib/os` uses `uos`.
Also, there are remaining uses of `utime` when non-standard (compared to
CPython) functions are used, like `utime.ticks_ms()`.
Signed-off-by: Damien George <damien@micropython.org>
It's no longer necessary since the built-in C version of this type now
implements all the functionality here.
Signed-off-by: Matt Trentini <matt.trentini@gmail.com>
axtls doesn't define all the CERT_xxx constants, nor the MBEDTLS_VERSION
constant.
This change means that `tls.SSLContext` is imported into the module, but
that's subsequently overridden by the class definition in this module.
Signed-off-by: Damien George <damien@micropython.org>
MicroPython now supplies SSL/TLS functionality in a new built-in `tls`
module. The `ssl` module is now implemented purely in Python, in this
repository. Other libraries are updated to work with this scheme.
Signed-off-by: Felix Dörre <felix@dogcraft.de>
This is a replacement for the `zlib` module that used to be built-in and
has been replaced by the MicroPython-specific `deflate` module.
Also updates the `gzip` module in a similar fashion and provide the
`gzip.GzipFile` class and `gzip.open` function.
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
- For packages that were just x.y, update to x.y.0.
- For that were x.y.z-n, update to x.y.(z+1)
From now on we'll apply semver rules:
- MAJOR version when you make incompatible API changes
- MINOR version when you add functionality in a backward compatible manner
- PATCH version when you make backward compatible bug fixes
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
With the recent MicroPython change to remove the u prefix by default on
builtins (micropython/micropython#11740) the format checker in fnmatch
which was detecting ure no longer works.
This commit updates the module to filter the regex automatically as needed.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
It is inserted automatically during publish/freezing and having them in the
code prevents the automatic process from happening.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit allows you to pass an exception object in as the exc_info kwarg
(CPython allows this), so logging exceptions can work even if the
MICROPY_PY_SYS_EXC_INFO option is disabled in the firmware.
Separately to that, currently even when sys.exc_info() is enabled, it's
only printing the traceback to _stream = sys.stderr - not to the configured
logging handlers. This means for instance if you've got a file log
handler it misses out on the tracebacks. That's also fixed in this commit.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>