Wykres commitów

2320 Commity (master)

Autor SHA1 Wiadomość Data
Damien George f0392b8d3d tests/run-multitests.py: Change to dir of test script when running it.
This matches the behaviour of run-tests.py, which sets cwd to the directory
containing the test script, which helps to isolate the filesystem.

It means that the SSL tests no longer need to know the name of their
containing directory to find the certificate files, and helps to run these
tests on bare-metal.

Signed-off-by: Damien George <damien@micropython.org>
2024-01-05 11:05:34 +11:00
Damien George a003ac2f73 tests/thread: Add a test for accuracy of sleep within a thread.
The existing thread_sleep1.py test only tests execution, not accuracy, of
time.sleep.  Also the existing test only tests sleep(0) on targets like rp2
that can only create a single thread.

The new test in this commit checks for timing accuracy on the main thread
and one other thread when they run at the same time.

Signed-off-by: Damien George <damien@micropython.org>
2024-01-05 10:35:34 +11:00
Damien George 2265d70add tests/thread: Adjust thread tests so most are able to run on rp2 port.
The aim of this commit is to make it so that the existing thread tests can
be used to test the _thread module on the rp2 port.  The rp2 port only
allows up to one thread to be created at a time, and does not have the GIL
enabled.

The following changes have been made:
- run-tests.py skips mutation tests on rp2, because there's no GIL.
- run-tests.py skips other tests on rp2 that require more than one thread.
- The tests stop trying to start a new thread after there is an OSError,
  which indicates that the system cannot create more threads.
- Some of these tests also now run the test function on the main thread,
  not just the spawned threads.
- In some tests the output printing is adjusted so it's the same regardless
  of how many threads were spawned.
- Some time.sleep(1) are replaced with time.sleep(0) to make the tests run
  a little faster (finish sooner when the work is done).

For the most part the tests are unchanged for existing platforms like esp32
and unix.

Signed-off-by: Damien George <damien@micropython.org>
2024-01-05 10:02:27 +11:00
Damien George 231fc20ce0 tests/run-tests.py: Remove machine_mem.py test from skip list.
This test was removed long ago in eb0e3bab1e.

Signed-off-by: Damien George <damien@micropython.org>
2024-01-04 14:53:11 +11:00
Jim Mussared 74fd7b3d32 tools/ci.sh: Set `ulimit -n` for unix CI.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-12-21 11:05:31 +11:00
Jim Mussared 8b24aa36ba extmod/modselect: Handle growing the pollfds allocation correctly.
The poll_obj_t instances have their pollfd field point into this
allocation.  So if re-allocating results in a move, we need to update the
existing poll_obj_t's.

Update the test to cover this case.

Fixes issue #12887.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-12-21 11:04:59 +11:00
stijn 85c02166ca py/modsys: Implement optional sys.intern.
Signed-off-by: stijn <stijn@ignitron.net>
2023-12-15 11:43:39 +11:00
Carlosgg bfd6ad94ff extmod/asyncio: Add ssl support with SSLContext.
This adds asyncio ssl support with SSLContext and the corresponding
tests in `tests/net_inet` and `tests/multi_net`.

Note that not doing the handshake on connect will delegate the handshake to
the following `mbedtls_ssl_read/write` calls.  However if the handshake
fails when a client certificate is required and not presented by the peer,
it needs to be notified of this handshake error (otherwise it will hang
until timeout if any).  Finally at MicroPython side raise the proper
mbedtls error code and message.

Signed-off-by: Carlos Gil <carlosgilglez@gmail.com>
2023-12-14 12:20:19 +11:00
Damien George bba8a673d5 tests: Update SSL network tests to use SSLContext, and work on CPython.
Changes are:
- use ssl.SSLContext.wrap_socket instead of ssl.wrap_socket
- disable check_hostname and call load_default_certs() where appropriate,
  to get CPython to run the tests correctly
- pass socket.AF_INET to getaddrinfo and socket.socket(), to force IPv4
- change tests to use github.com instead of google.com, because certificate
  validation was failing with google.com

Signed-off-by: Damien George <damien@micropython.org>
2023-12-12 21:22:10 +11:00
Damien George ef996d15b9 extmod/modssl_mbedtls: Make SSLSocket.getpeercert() optional.
And only enable this method when the relevant feature is available in
mbedtls.  Otherwise, if mbedtls doesn't support getting the peer
certificate, this method always returns None and it's confusing why it does
that.  It's better to remove the method altogether, so the error trying to
use it is more obvious.

Signed-off-by: Damien George <damien@micropython.org>
2023-12-12 21:21:54 +11:00
Carlosgg f3f215e9bd extmod/modssl_mbedtls: Add SSLContext certificate methods.
This commit adds:

1) Methods to SSLContext class that match CPython signature:

	- `SSLContext.load_cert_chain(certfile, keyfile)`
	- `SSLContext.load_verify_locations(cafile=, cadata=)`
	- `SSLContext.get_ciphers()` --> ["CIPHERSUITE"]
	- `SSLContext.set_ciphers(["CIPHERSUITE"])`

2) `sslsocket.cipher()` to get current ciphersuite and protocol
   version.

3) `ssl.MBEDTLS_VERSION` string constant.

4) Certificate verification errors info instead of
   `MBEDTLS_ERR_X509_CERT_VERIFY_FAILED`.

5) Tests in `net_inet` and `multi_net` to test these new methods.

`SSLContext.load_cert_chain` method allows loading key and cert from disk
passing a filepath in `certfile` or `keyfile` options.

`SSLContext.load_verify_locations`'s `cafile` option enables the same
functionality for ca files.

Signed-off-by: Carlos Gil <carlosgilglez@gmail.com>
2023-12-12 16:25:07 +11:00
Damien George d30d5c99af tests/run-tests.py: Skip Thumb2 tests if target doesn't support them.
Signed-off-by: Damien George <damien@micropython.org>
2023-12-06 16:05:37 +11:00
Damien George b796f1e3f1 tests/float/inf_nan_arith.py: Include -inf in argument combos.
This adds tests for, eg, -inf + inf which should be nan.

Signed-off-by: Damien George <damien@micropython.org>
2023-12-06 14:06:01 +11:00
Damien George c554df57f6 tests/extmod/deflate_compress.py: Add a test for optimal compression.
Signed-off-by: Damien George <damien@micropython.org>
2023-11-30 12:13:29 +11:00
Damien George f397a3ec31 py/objslice: Validate that the argument to indices() is an integer.
Otherwise passing in a non-integer can lead to an invalid memory access.

Thanks to Junwha Hong and Wonil Jang @S2Lab, UNIST for finding the issue.

Fixes issue #13007.

Signed-off-by: Damien George <damien@micropython.org>
2023-11-21 22:28:57 +11:00
Peter Züger 36e162f116 tests/net_hosted/asyncio_loopback.py: Add loopback test.
Signed-off-by: Peter Züger <zueger.peter@icloud.com>
2023-11-17 13:37:43 +11:00
Andrew Leech 4cf741062b extmod/vfs_reader: Add file ioctl to set read buffer size.
Can be used to speed up importing a file from a vfs based filesystem.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
2023-11-09 11:20:31 +11:00
Mathieu Serandour c85db05244 py/lexer: Change token position for new lines.
Set the position of new line tokens as the end of the preceding line
instead of the beginning of the next line.  This is done by first moving
the pointer to the end of the current line to skip any whitespace, record
the position for the token, then finaly skip any other line and whitespace.

The previous behavior was to skip every new line and whitespace, including
the indent of the next line, before recording the token position.

(Note that both lex->emit_dent and lex->nested_bracket_level equal 0 if
had_physical_newline == true, which allows simplifying the if-logic for
MP_TOKEN_NEWLINE.)

And update the cmd_parsetree.py test expected output, because the position
of the new-line token has changed.

Fixes issue #12792.

Signed-off-by: Mathieu Serandour <mathieu.serandour@numworks.fr>
2023-11-03 15:56:10 +11:00
Jim Mussared bbd8760bd9 all: Update Python formatting to ruff-format.
This updates a small number of files that change with ruff-format's (vs
black's) rules.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-03 13:30:41 +11:00
Jim Mussared 78f4f30cb1 tests/extmod/asyncio_as_uasyncio.py: Fix qstr order dependency.
This test depends on the order in which qstrs are stored in ROM, which
affects the order in which `dir()` will probe the object to see what it
supports.  Because of the lazy-loading in asyncio/__init__.py, if it
tries to do e.g. `wait_for_ms` before `funcs` then it will import funcs,
making `funcs` later succeed. But in the other way around, `funcs` will
initially not be found.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-30 11:10:02 +11:00
Jim Mussared 1a017511d0 tests/perf_bench: Add string/qstr/map tests.
These tests are designed to measure changes in performance relating to:
 - string interning / searching for existing strings
 - map lookup
 - string operations
 - string hashing

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-27 16:16:08 +11:00
Christian Walther 7be16e0540 extmod/vfs_posix: Additional tests for coverage of error cases.
Signed-off-by: Christian Walther <cwalther@gmx.ch>
2023-10-19 16:21:09 +02:00
Christian Walther be28829ae8 extmod/vfs_posix: Fix getcwd() on non-root VFS.
The unwritten API contract expected of a VFS.getcwd() by mp_vfs_getcwd()
is that its return value should be either "" or "/" when the CWD is at
the root of the VFS and otherwise start with a slash and not end with a
slash. This was not correctly implemented in VfsPosix for instances with
a non-empty root - the required leading slash, if any, was cut off
because the root length includes a trailing slash. This would result in
missing slashes in the middle of the return value of os.getcwd() or in
uninitialized garbage from beyond a string's null terminator when the
CWD was at the VFS root.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
2023-10-19 16:21:09 +02:00
Christian Walther 0c4fb16871 extmod/vfs_posix: Fix relative paths on non-root VFS.
The unwritten API contract expected of a VFS by mp_vfs_lookup_path() is
that paths passed in are relative to the root of the VFS if they start
with '/' and relative to the current directory of the VFS otherwise.
This was not correctly implemented in VfsPosix for instances with a
non-empty root - all paths were interpreted relative to the root. Fix
that. Since VfsPosix tracks its CWD using the "external" CWD of the Unix
process, the correct handling for relative paths is to pass them through
unmodified.

Also, when concatenating absolute paths, fix an off-by-one resulting in
a harmless double slash (the root path already has a trailing slash).

Signed-off-by: Christian Walther <cwalther@gmx.ch>
2023-10-19 16:21:09 +02:00
Christian Walther 5f7065f57a extmod/vfs_posix: Fix accidentally passing tests.
These tests test an unrealistic situation and only pass by accident due
to a bug. The upcoming fix for the bug would make them fail.

The unrealistic situation is that VfsPosix methods are called with
relative paths while the current working directory is somewhere outside
of the root of the VFS. In the intended use of VFS objects via
os.mount() (as opposed to calling methods directly as the tests do),
this never happens, as mp_vfs_lookup_path() directs incoming calls to
the VFS that contains the CWD.

Make the testing situation realistic by changing the working directory
to the root of the VFS before calling methods on it, as the subsequent
relative path accesses expect.

Thanks to the preceding commit, the tests still pass, but still for the
wrong reason. The following commit "Fix relative paths on non-root VFS"
will make them pass for the correct reason.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
2023-10-19 16:21:08 +02:00
Christian Walther e3ba6f952b extmod/vfs_posix: Fix relative root path.
A VfsPosix created with a relative root path would get confused when
chdir() was called on it and become unable to properly resolve absolute
paths, because changing directories effectively shifted its root. The
simplest fix for that would be to say "don't do that", but since the
unit tests themselves do it, fix it by making a relative path absolute
before storing it.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
2023-10-19 16:21:08 +02:00
Jim Mussared 8eb7721b4a extmod/modframebuf: Remove FrameBuffer1 from natmod build.
This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-16 12:18:43 +11:00
Jim Mussared 03a9fa227d extmod/modframebuf: Fix FrameBuffer get-buffer implementation.
This wasn't correctly accounting for the bits-per-pixel and was returning a
bufinfo struct with the incorrect length.  Instead, just forward directly
to the underlying buffer object.

Fixes issue #12563.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-16 12:18:08 +11:00
Jim Mussared d040478d8a extmod/modframebuf: Validate FrameBuffer bounds against input buffer.
This ensures that the buffer is large enough for the specified width,
height, bits-per-pixel, and stride.

Also makes the legacy FrameBuffer1 constructor re-use the FrameBuffer
make_new to save some code size.

Fixes issue #12562.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-16 12:17:51 +11:00
Damien George 6967ff3c58 py/persistentcode: Bump .mpy sub-version.
This is required because the previous commit changed the .mpy native ABI.

Signed-off-by: Damien George <damien@micropython.org>
2023-10-16 11:25:31 +11:00
Damien George 516385c4cc py/objboundmeth: Optimise check for types in binary_op.
Signed-off-by: Damien George <damien@micropython.org>
2023-10-13 15:29:09 +11:00
Ned Konz 66c62353ce tests/basics/boundmeth1.py: Add tests for bound method equality/hash.
This commit adds tests for bound method comparison and hashing to support
the changes in the previous commit.

Signed-off-by: Ned Konz <ned@productcreationstudio.com>
2023-10-13 15:15:49 +11:00
Damien George 05cb1406ad extmod/moductypes: Validate that uctypes.struct addr argument is an int.
Fixes issue #12660.

Signed-off-by: Damien George <damien@micropython.org>
2023-10-12 15:47:15 +11:00
Jim Mussared 5015779a6f py/builtinevex: Handle invalid filenames for execfile.
If a non-string buffer was passed to execfile, then it would be passed
as a non-null-terminated char* to mp_lexer_new_from_file.

This changes mp_lexer_new_from_file to take a qstr instead (as in almost
all cases a qstr will be created from this input anyway to set the
`__file__` attribute on the module).

This now makes execfile require a string (not generic buffer) argument,
which is probably a good fix to make anyway.

Fixes issue #12522.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-12 15:17:59 +11:00
Matthias Urlichs 3fb1bb131f py/vm: Don't emit warning when using "raise ... from None".
"Raise SomeException() from None" is a common Python idiom to suppress
chained exceptions and thus shouldn't trigger a warning on a version of
Python that doesn't support them in the first place.
2023-10-09 09:46:02 +11:00
Jim Mussared 977dc9a369 extmod/asyncio/stream.py: Fix cancellation handling of start_server.
The following code:

  server = await asyncio.start_server(...)
  async with server:
    ... code that raises ...

would lose the original exception because the server's task would not have
had a chance to be scheduled yet, and so awaiting the task in wait_closed
would raise the cancellation instead of the original exception.

Additionally, ensures that explicitly cancelling the parent task delivers
the cancellation correctly (previously was masked by the server loop), now
this only happens if the server was closed, not when the task was
cancelled.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-02 14:11:52 +11:00
Jim Mussared fae83a6b4d tests/extmod/asyncio_threadsafeflag.py: Update for unix select.
1. Remove the skip for detecting support for polling user-defined objects
   as this is always possible now on all ports.
2. Don't print when the scheduled task runs as the ordering of this
   relative to the other prints is dependent on other factors (e.g. if
   using the native emitter).

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-09-29 17:58:40 +10:00
Carlosgg 10f34b97d1 tests/multi_net/ssl_cert_rsa.py: Update test certificate.
Update expired certificate, increase time validity period to five years and
fix command arguments typos in commentaries.

Signed-off-by: Carlos Gil <carlosgilglez@gmail.com>
2023-09-29 17:00:16 +10:00
Angus Gratton b461d218d1 tests/run-internalbench.py: Remove old CPython reference.
At one point it was possible to internal_bench CPython vs MicroPython, but
seemingly not any more.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2023-09-29 15:41:41 +10:00
Angus Gratton dd8a69b5f2 tests/README: Document ./run-internalbench.py.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
2023-09-29 15:41:28 +10:00
Damien George 62c3033ba6 tests/extmod/vfs_fat_finaliser.py: Tweak test so files are collected.
Signed-off-by: Damien George <damien@micropython.org>
2023-09-29 12:02:36 +10:00
Damien George 7c88cdda49 tests/float/math_domain.py: Tweak test to also pass with obj-repr-C.
Signed-off-by: Damien George <damien@micropython.org>
2023-09-29 12:02:21 +10:00
Damien George a7e2a6d9f2 tests/extmod/ssl_cadata.py: Skip test on axtls.
The axtls bindings don't support this.

Signed-off-by: Damien George <damien@micropython.org>
2023-09-29 12:01:50 +10:00
Damien George a33766880e tests/extmod/deflate_decompress.py: Skip test when not enough memory.
Signed-off-by: Damien George <damien@micropython.org>
2023-09-29 12:01:04 +10:00
Damien George 3695211576 tests/float/float_format_ints.py: Put power-of-10 test in separate file.
This test doesn't pass on builds with 30-bit floats (object repr C).

Signed-off-by: Damien George <damien@micropython.org>
2023-09-29 12:00:15 +10:00
Jim Mussared 52f76cf4fc tests/stress/bytecode_limit.py: Reverse order of cases.
The PYBD_SF2 is right on the limit of being able to run this test and so
it succeeds the first two cases and fails the next two with MemoryError.

This causes it to SKIP, but that only works if it's the first thing
printed. So reverse the order of the tests so it fails on the biggest
one first.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-09-29 11:44:20 +10:00
Damien George 397697aa83 py/persistentcode: Always close reader even if an exception is raised.
Fixes issue #3874.

Signed-off-by: Damien George <damien@micropython.org>
2023-09-14 13:03:31 +10:00
Damien George 5e122b11ea py/parse: Always free lexer even if an exception is raised.
Fixes issue #3843.

Signed-off-by: Damien George <damien@micropython.org>
2023-09-14 13:03:31 +10:00
iabdalkader 3637252b7b tests/multi_net: Increase asyncio tests timeouts.
Increase asyncio tests timeouts to account for different WiFi modules and
CPU clocks on different boards.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2023-09-06 12:09:19 +10:00
David Lechner ffb43b2dd3 py/modthread: Return thread id from start_new_thread().
In CPython, `_thread.start_new_thread()` returns an ID that is the same ID
that is returned by `_thread.get_ident()`.  The current MicroPython
implementation of `_thread.start_new_thread()` always returns `None`.

This modifies the required functions to return a value. The native thread
id is returned since this can be used for interop with other functions, for
example, `pthread_kill()` on *nix. `_thread.get_ident()` is also modified
to return the native thread id so that the values match and avoids the need
for a separate `native_id` attribute.

Fixes issue #12153.

Signed-off-by: David Lechner <david@pybricks.com>
2023-09-03 18:49:18 +10:00
stephanelsmith db06041d59 extmod/vfs_posix_file: Implement sys.std*.buffer objects.
Add the buffer attribute to sys.stdin, sys.stdout and sys.stderr.  This
provides raw access to underlying stdio streams for the unix port (and
others that use VfsPosix).

Signed-off-by: stephanelsmith <stephane.smith@titansensor.com>
2023-09-01 17:39:38 +10:00
stephanelsmith 633599cdd5 tests/run-tests.py: Capture output of stderr when running on CPython.
Signed-off-by: stephanelsmith <stephane.smith@titansensor.com>
2023-09-01 17:39:19 +10:00
Jim Mussared a64f2fdca0 py/dynruntime.h: Implement MP_OBJ_NEW_QSTR.
Because mpy_ld.py doesn't know the target object representation, it emits
instances of `MP_OBJ_NEW_QSTR(MP_QSTR_Foo)` as const string objects, rather
than qstrs. However this doesn't work for map keys (e.g. for a locals dict)
because the map has all_keys_are_qstrs flag is set (and also auto-complete
requires the map keys to be qstrs).

Instead, emit them as regular qstrs, and make a functioning MP_OBJ_NEW_QSTR
function available (via `native_to_obj`, also used for e.g. making
integers).

Remove the code from mpy_ld.py to emit qstrs as constant strings, but leave
behind the scaffold to emit constant objects in case we want to do use this
in the future.

Strictly this should be a .mpy sub-version bump, even though the function
table isn't changing, it does lead to a change in behavior for a new .mpy
running against old MicroPython. `mp_native_to_obj` will incorrectly return
the qstr value directly as an `mp_obj_t`, leading to unexpected results.
But given that it's broken at the moment, it seems unlikely that anyone is
relying on this, so it's not work the other downsides of a sub-version bump
(i.e. breaking pure-Python modules that use @native). The opposite case of
running an old .mpy on new MicroPython is unchanged, and remains broken in
exactly the same way.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-09-01 16:14:22 +10:00
Daniël van de Giessen d1f288c041 py/modstruct: Support pad bytes in struct format.
This adds support for the x format code in struct.pack and struct.unpack.

The primary use case for this is ignoring bytes while unpacking.  When
interfacing with existing systems, it may often happen that you either have
fields in a struct that aren't properly specified or you simply don't care
about them.  Being able to easily skip them is useful.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2023-09-01 14:50:49 +10:00
mcskatkat e0a1480600 py/objstr: Fix `str % {}` edge case.
Eliminate `TypeError` when format string contains no named conversions.
This matches CPython behavior.

Signed-off-by: mcskatkat <mc_skatkat@hotmail.com>
2023-09-01 14:31:57 +10:00
stijn 83f2f36b9e tests/unix/mod_os: Make os.system() test work on windows.
The "true" command by default is unavailable on windows so use
an equivalent which works on both unix and windows.

Signed-off-by: stijn <stijn@ignitron.net>
2023-09-01 14:08:58 +10:00
Jim Mussared b2b5bcce28 py/profile: Remove the requirement to disable MICROPY_COMP_CONST.
The only reason that const had to be disabled was to make the test output
match CPython when const was involved.  Instead, this commit fixes the test
to handle the lines where const is used.

Also:
- remove the special handling for MICROPY_PERSISTENT_CODE_SAVE in
  unix/mpconfigport.h, and make this automatic.
- move the check for MICROPY_PERSISTENT_CODE_SAVE to where it's used (like
  we do for other similar checks) and add a comment explaining it.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-08-30 10:58:04 +10:00
Jim Mussared 4a3fdc0e76 tests/misc/sys_settrace_features.py: Fix to run on newer CPython.
This test was failing on CPython 3.11 as it now emits `0` as the line
number for the "call" event corresponding to import, where as in 3.6 it had
`1` as the line number.

We maintain the old behavior, but in order to make this test pass on both
CPython versions, the trace handler now converts the `0` to a `1`.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-08-30 10:57:37 +10:00
Damien George 88518009ce tests/extmod/select_poll_eintr.py: Improve robustness of test.
Increase allowed range of dt_ms, and print it in case of failure.

Signed-off-by: Damien George <damien@micropython.org>
2023-08-15 00:08:51 +10:00
Damien George f6af48416d extmod/modselect: Properly track number of poll objects that are fd's.
Signed-off-by: Damien George <damien@micropython.org>
2023-08-14 18:48:47 +10:00
Damien George d529c20674 extmod/modssl_mbedtls: Fix ioctl of a socket in closed/error state.
Signed-off-by: Damien George <damien@micropython.org>
2023-08-09 13:22:57 +10:00
Damien George 20d3a6b196 extmod/modssl_mbedtls: Reject ioctls that are not supported.
An SSL stream can only handle CLOSE and POLL ioctls.  Other ones do not
make sense, or at least it doesn't make sense to pass the ioctl request
directly down to the underlying stream.

In particular MP_STREAM_GET_FILENO should not be passed to the underlying
stream because the SSL stream is not directly related to a file descriptor,
and the SSL stream must handle the polling itself.

Signed-off-by: Damien George <damien@micropython.org>
2023-08-09 13:22:32 +10:00
Damien George 218242d1de tests/extmod: Skip select/socket tests if they can't create UDP socket.
Some targets (eg PYBV10) have the socket module but are unable to create
UDP sockets without a registered NIC.  So skip UDP tests on these targets.

Signed-off-by: Damien George <damien@micropython.org>
2023-08-07 12:39:29 +10:00
Damien George 6b78a1bf00 tests/extmod: Add coverage tests for select module.
Signed-off-by: Damien George <damien@micropython.org>
2023-08-07 12:37:48 +10:00
Jim Mussared 975a687447 py/mpconfig: Add MICROPY_PY_PLATFORM, enabled at extra features level.
Previously this was explicitly enabled on esp32/stm32/renesas/mimxrt/samd,
but didn't get a default feature level because it wasn't in py/mpconfig.h.

With this commit it's now enabled at the "extra features" level, which adds
rp2, unix-standard, windows, esp8266, webassembly, and some nrf boards.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-07-24 23:53:20 +10:00
Jim Mussared ea1a5e43d0 examples/natmod/deflate: Add deflate as a dynamic native module.
This replaces the previous zlib version.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-07-21 19:32:42 +10:00
Jim Mussared 8b315ef0d8 tests/extmod: Add deflate.DeflateIO tests.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-07-21 19:32:42 +10:00
Jim Mussared 3533924c36 extmod/moddeflate: Add deflate module providing the DeflateIO class.
This provides similar functionality to the former zlib.DecompIO and
especially CPython's gzip.GzipFile for both compression and decompression.

This class can be used directly, and also can be used from Python to
implement (via io.BytesIO) zlib.decompress and zlib.compress, as well as
gzip.GzipFile.

Enable/disable this on all ports/boards that zlib was previously configured
for.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-07-21 19:32:40 +10:00
Jim Mussared add1200343 all: Remove the zlib module.
This will be replaced with a new deflate module providing the same
functionality, with an optional frozen Python wrapper providing a
replacement zlib module.

binascii.crc32 is temporarily disabled.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-07-21 18:48:29 +10:00
Damien George 606ec9bfb1 py/compile: Fix async for's stack handling of iterator expression.
Prior to this fix, async for assumed the iterator expression was a simple
identifier, and used that identifier as a local to store the intermediate
iterator object.  This is incorrect behaviour.

This commit fixes the issue by keeping the iterator object on the stack as
an anonymous local variable.

Fixes issue #11511.

Signed-off-by: Damien George <damien@micropython.org>
2023-07-13 13:50:50 +10:00
Damien George 9900eaa269 tests/extmod: Add test for passing cadata into ssl.wrap_socket().
For coverage.

Signed-off-by: Damien George <damien@micropython.org>
2023-06-26 16:34:41 +10:00
Damien George b50da3dbd7 tests/extmod: Add tests for ssl.SSLContext.
Signed-off-by: Damien George <damien@micropython.org>
2023-06-26 16:34:41 +10:00
Damien George e8a4c1dd53 extmod/modssl: Add SSLContext class.
This commit adds the SSLContext class to the ssl module, and retains the
existing ssl.wrap_socket() function to maintain backwards compatibility.

CPython deprecated the ssl.wrap_socket() function since CPython 3.7 and
instead one should use ssl.SSLContext().wrap_socket().  This commit makes
that possible.

For the axtls implementation:
- ssl.SSLContext is added, although it doesn't hold much state because
  axtls requires calling ssl_ctx_new() for each new socket
- ssl.SSLContext.wrap_socket() is added
- ssl.PROTOCOL_TLS_CLIENT and ssl.PROTOCOL_TLS_SERVER are added

For the mbedtls implementation:
- ssl.SSLContext is added, and holds most of the mbedtls state
- ssl.verify_mode is added (getter and setter)
- ssl.SSLContext.wrap_socket() is added
- ssl.PROTOCOL_TLS_CLIENT and ssl.PROTOCOL_TLS_SERVER are added

The signatures match CPython:
- SSLContext(protocol)
- SSLContext.wrap_socket(sock, *, server_side=False,
    do_handshake_on_connect=True, server_hostname=None)

The existing ssl.wrap_socket() functions retain their existing signature.

Signed-off-by: Damien George <damien@micropython.org>
2023-06-26 16:34:41 +10:00
Duncan Lowther 41c91422f0
tests/extmod/framebuf: Fix buffer size issues.
Tests framebuf1 and framebuf2 do not take the need for byte-aligned
strides into consideration when calculating buffer lengths.
Accordingly, the buffers allocated are slightly too small. Fixed
buffer length calculations.

Signed-off-by: Duncan Lowther <Duncan.Lowther@glasgow.ac.uk>
2023-06-21 09:49:03 +01:00
Duncan Lowther bc2ed8c55a
tests/extmod/uctypes_array_assign_le: Fix buffer.
Structure descriptor in test extmod/uctypes_array_assign_le
is 6 bytes long, due to member "arr3" having length 4
(2 * UINT16) and offset 2, but only 5 bytes are allocated.
Increased buffer length to 6 bytes.

Signed-off-by: Duncan Lowther <Duncan.Lowther@glasgow.ac.uk>
2023-06-21 09:48:37 +01:00
Jim Mussared ca79b49619 extmod/asyncio/uasyncio.py: Add backwards-compatible uasyncio alias.
This allows existing code that does `import uasyncio` or
`import uasyncio as asyncio` to continue working.

It uses the same lazy-loading as asyncio to prevent loading of unused
features.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-19 18:37:34 +10:00
Jim Mussared 6027c41c8f tests: Rename uasyncio to asyncio.
This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-19 17:33:03 +10:00
Damien George ed962f1f23 tests/float: Test domain errors for more combos of args to math funcs.
Instead of having a special set of arguments to test for each math-module
function, just test all functions with all sets of arguments.  This gives
improved test cases to prevent regressions.

Signed-off-by: Damien George <damien@micropython.org>
2023-06-18 12:45:55 +10:00
Jared Hancock b3cd41dd4b py/lexer: Allow conversion specifiers in f-strings (e.g. !r).
PEP-498 allows for conversion specifiers like !r and !s to convert the
expression declared in braces to be passed through repr() and str()
respectively.

This updates the logic that detects the end of the expression to also stop
when it sees "![rs]" that is either at the end of the f-string or before
the ":" indicating the start of the format specifier. The "![rs]" is now
retained in the format string, whereas previously it stayed on the end
of the expression leading to a syntax error.

Previously: `f"{x!y:z}"` --> `"{:z}".format(x!y)`
Now: `f"{x!y:z}"` --> `"{!y:z}".format(x)`

Note that "!a" is not supported by `str.format` as MicroPython has no
`ascii()`, but now this will raise the correct error.

Updated cpydiff and added tests.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-14 19:11:04 +10:00
Jim Mussared a1fbb1980c extmod/modtimeq: Remove timeq module.
This is a MicroPython-specific module that existed to support the old
version of uasyncio.  It's undocumented and not enabled on all ports and
takes up code size unnecessarily.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08 17:54:28 +10:00
Jim Mussared 9d7eac0713 tests/run-natmodtests.py: Don't allow imports from the cwd.
Make tests run in an isolated environment (i.e. `import io` would
otherwise get the `tests/io` directory).

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08 17:54:24 +10:00
Jim Mussared 339f02a594 tests/run-perfbench.py: Don't allow imports from the cwd.
Make tests run in an isolated environment (i.e. `import io` would
otherwise get the `tests/io` directory).

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08 17:54:24 +10:00
Jim Mussared 109717457e tests/run-multitests.py: Don't allow imports from the cwd.
Make tests run in an isolated environment (i.e. `import io` would
otherwise get the `tests/io` directory).

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08 17:54:24 +10:00
Jim Mussared 4216bc7d13 tests: Replace umodule with module everywhere.
This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08 17:54:24 +10:00
David Grayson a79a6ab364 py/builtinimport: Remove partially-loaded modules from sys.modules.
Prior to this commit, importing a module that exists but has a syntax error
or some other problem that happens at import time would result in a
potentially-incomplete module object getting added to sys.modules.
Subsequent imports would use that object, resulting in confusing error
messages that hide the root cause of the problem.

This commit fixes that issue by removing the failed module from sys.modules
using the new NLR callback mechanism.

Note that it is still important to add the module to sys.modules while the
import is happening so that we can support circular imports just like
CPython does.

Fixes issue #967.

Signed-off-by: David Grayson <davidegrayson@gmail.com>
2023-06-05 23:21:52 +10:00
Jim Mussared 99a0c45aef tests/import/import_pkg9.py: Add test for subpackage attribute.
When foo.bar is imported, bar is added as an attribute to foo. Previously
this happened on every import, but should only happen on first import.

This verifies the behavior for relative imports and overriding.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01 16:21:37 +10:00
Jim Mussared dfa7677e2f tests/import/builtin_ext.py: Add test for built-in module override.
This verifies the behavior:
 - Exact matches of built-ins bypass filesystem.
 - u-prefix modules can be overridden from the filesystem.
 - Builtin import can be forced using either u-prefix or sys.path=[].

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01 16:21:37 +10:00
Jim Mussared 5e04521251 examples/usercmodule: Add a sub-package example.
This demonstrates how to add a sub-package in a user c module, as well
as how to define the necessary qstrs and enable the feature in the build.

This is used by the unix coverage build to test this feature.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01 16:21:37 +10:00
Damien George 69dd013919 py/objint: Allow int() to parse anything with the buffer protocol.
This generalises and simplifies the code and follows CPython behaviour.

See similar change for floats in a07fc5b640.

Signed-off-by: Damien George <damien@micropython.org>
2023-06-01 15:11:06 +10:00
Damien George 66dc1397c9 py/obj: Accept user types in mp_obj_get_int_maybe.
This is possible now that MP_UNARY_OP_INT_MAYBE exists.

As a consequence mp_obj_get_int now also supports user types, which was
previously possible with MP_UNARY_OP_INT but no tests existed for it.

Signed-off-by: Damien George <damien@micropython.org>
2023-06-01 14:18:54 +10:00
Damien George 48ffd6596e py: Change MP_UNARY_OP_INT to MP_UNARY_OP_INT_MAYBE.
To be consistent with MP_UNARY_OP_INT_FLOAT and MP_UNARY_OP_INT_COMPLEX,
and allow int() to first check if a type supports __int__ before trying
other things (as per CPython).

Signed-off-by: Damien George <damien@micropython.org>
2023-06-01 13:01:07 +10:00
Damien George 3ae78e803b tests/basics: Remove __index__ and __inv__ from special methods tests.
MicroPython does not support these special methods, and they may get in the
way of other tests (eg indexing with __int__).

Signed-off-by: Damien George <damien@micropython.org>
2023-06-01 13:01:07 +10:00
Damien George ea7031faff py/runtime: If inplace binop fails then try corresponding normal binop.
The code that handles inplace-operator to normal-binary-operator fallback
is moved in this commit from py/objtype.c to py/runtime.c, making it apply
to all types, not just user classes.

Signed-off-by: Damien George <damien@micropython.org>
2023-05-19 13:44:00 +10:00
Damien George 4b57330465 py/objstr: Return unsupported binop instead of raising TypeError.
So that user types can implement reverse operators and have them work with
str on the left-hand-side, eg `"a" + UserType()`.

Signed-off-by: Damien George <damien@micropython.org>
2023-05-19 13:42:35 +10:00
Damien George ca9068e0ef py/objarray: Disallow memoryview addition.
Following CPython.  This is important for subsequent commits to work
correctly.

Signed-off-by: Damien George <damien@micropython.org>
2023-05-19 13:33:54 +10:00
Damien George 9accb7dd44 tests/basics: Add more tests for hashing of various types.
Signed-off-by: Damien George <damien@micropython.org>
2023-05-19 12:35:12 +10:00
David Lechner 2fe6d4eb86 py/objdict: Fix __hash__ for dict_view types.
This adds a unary_op implementation for the dict_view type that makes
the implementation of `hash()` for these types compatible with CPython.

Signed-off-by: David Lechner <david@pybricks.com>
2023-05-19 12:06:17 +10:00
David Lechner 8491eb190f py/objslice: Ensure slice is not hashable.
As per https://bugs.python.org/issue408326, the slice object should not be
hashable.  Since MicroPython has an implicit fallback when the unary_op
slot is empty, we need to fill this slot.

Signed-off-by: David Lechner <david@pybricks.com>
2023-05-19 12:06:06 +10:00
Jim Mussared 17127bbee5 tests/run-tests.py: Ensure correct cwd for mpy tests.
Previously when using --via-mpy, the file was compiled to tests/<tmp>.mpy
and then run using `micropython -m <tmp>` in the current cwd
(usually tests/).  This meant that an import in the test would be resolved
relative to tests/.

This is different to regular (non-via-mpy) tests, where we run (for
example) `micropython basics/test.py` which means that an import would be
resolved relative to basics/.

Now --via-mpy matches the .py behavior.  This is important because:
a) It makes it so import tests do the right thing.
b) There are directory names in tests/ that match built-in module names.

Furthermore, it always ensures the cwd (for both micropython and cpython)
is the test directory (e.g. basics/) rather than being left unset.  This
also makes it clearer inside the test that e.g. file access is relative to
the Python file.

Updated tests with file paths to match.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-05-18 13:48:21 +10:00
Damien George 1b980c9dbe py/compile: Remove over-eager optimisation of tuples as if condition.
When a tuple is the condition of an if statement, it's only possible to
optimise that tuple away when it is a constant tuple (ie all its elements
are constants), because if it's not constant then the elements must be
evaluated in case they have side effects (even though the resulting tuple
will always be "true").

The code before this change handled the empty tuple OK (because it doesn't
need to be evaluated), but it discarded non-empty tuples without evaluating
them, which is incorrect behaviour (as show by the updated test).

This optimisation is anyway rarely applied because it's not common Python
coding practice to write things like `if (): ...` and `if (1, 2): ...`, so
removing this optimisation completely won't affect much code, if any.

Furthermore, when MICROPY_COMP_CONST_TUPLE is enabled, constant tuples are
already optimised by the parser, so expression with constant tuples like
`if (): ...` and `if (1, 2): ...` will continue to be optimised properly
(and so when this option is enabled the code that's deleted in this commit
is actually unreachable when the if condition is a constant tuple).

Signed-off-by: Damien George <damien@micropython.org>
2023-05-03 13:21:18 +10:00