summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics
Commit message (Collapse)AuthorAge
* py/objarray: Allow extending array with any iterable.Jeff Epler6 days
| | | | | | | | | | | | As suggested by @dpgeorge, factor out part of array_construct to allow it to be used for construction & extension. Note that extending with a known-length list (or tuple) goes through the slow path of calling array_extend once per element. Fixes issue #7408. Signed-off-by: Jeff Epler <jepler@gmail.com>
* py/objstr: Add support for the :_b/o/x specifier in str.format.Jeff Epler2025-05-13
| | | | | | | | | | | This groups non-decimal values by fours, such as bbb_bbbb_bbbb. It also supports `{:_d}` to use underscore for decimal numbers (grouped in threes). Use of incorrect ":,b" is not diagnosed. Thanks to @dpgeorge for the suggestion to reduce code size. Signed-off-by: Jeff Epler <jepler@gmail.com>
* py/objstr: Fix handling of OP_MODULO with namedtuple.Yoctopuce dev2025-04-21
| | | | | | | | | This fix handles attrtuple as well, eg. os.uname(). A test case has been added in basics/attrtuple2.py. Fixes issue #16969. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
* tests/basics/builtin_range.py: Add more tests for range slicing.Jeff Epler2025-04-21
| | | | Signed-off-by: Jeff Epler <jepler@gmail.com>
* py/modsys: Add sys.implementation._build entry.Damien George2025-03-05
| | | | | | | | | | | | | | | | | | | For a given MicroPython firmware/executable it can be sometimes important to know how it was built, which variant/board configuration it came from. This commit adds a new field `sys.implementation._build` that can help identify the configuration that MicroPython was built with. For now it's either: * <VARIANT> for unix, webassembly and windows ports * <BOARD>-<VARIANT> for microcontroller ports (the variant is optional) In the future additional elements may be added to this string, separated by a hyphen. Resolves issue #16498. Signed-off-by: Damien George <damien@micropython.org>
* py/objstr: Support tuples and start/end args in startswith and endswith.Glenn Moloney2025-03-02
| | | | | | | | | | | | | | This change allows tuples to be passed as the prefix/suffix argument to the `str.startswith()` and `str.endswith()` methods. The methods will return `True` if the string starts/ends with any of the prefixes/suffixes in the tuple. Also adds full support for the `start` and `end` arguments to both methods for compatibility with CPython. Tests have been updated for the new behaviour. Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
* py/objfun: Implement function.__code__ and function constructor.Damien George2025-02-11
| | | | | | | | | | | This allows retrieving the code object of a function using `function.__code__`, and then reconstructing a function from a code object using `FunctionType(code_object)`. This feature is controlled by `MICROPY_PY_FUNCTION_ATTRS_CODE` and is enabled at the full-features level. Signed-off-by: Damien George <damien@micropython.org>
* py/parsenum: Throw an exception for invalid int literals like "01".Jeff Epler2025-01-26
| | | | | | | | | | | | | | | This includes making int("01") parse in base 10 like standard Python. When a base of 0 is specified it means auto-detect based on the prefix, and literals begining with 0 (except when the literal is all 0's) like "01" are then invalid and now throw an exception. The new error message is different from CPython. It says e.g., `SyntaxError: invalid syntax for integer with base 0: '09'` Additional test cases were added to cover the changed & added code. Co-authored-by: Damien George <damien@micropython.org> Signed-off-by: Jeff Epler <jepler@gmail.com>
* tests/basics/nanbox_smallint.py: Fix incorrect use of int() in test.Jeff Epler2025-01-26
| | | | | | | The literal is in base 16 but int()'s default radix in CPython is 10, not 0. Signed-off-by: Jeff Epler <jepler@gmail.com>
* tests/basics/deque2.py: Add tests for deque subscript-from-end.Damien George2024-11-04
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/objtype: Don't delegate lookup of descriptor methods to __getattr__.Damien George2024-10-16
| | | | | | | | When descriptors are enabled, lookup of the `__get__`, `__set__` and `__delete__` descriptor methods should not be delegated to `__getattr__`. That follows CPython behaviour. Signed-off-by: Damien George <damien@micropython.org>
* py/objtype: Allow passing keyword arguments to native base __init__.stijn2024-10-07
| | | | | | | | | | | | | | | Allowing passing keyword arguments to a native base's __init__, i.e. `make_new` in the C code. Previously only positional arguments were allowed. The main trade-off in this commit is that every call to the native base's `make_new` is now going to be preceded by a call to `mp_map_init_fixed_table` even though most of what that does is unused and instead it merely serves as a way to pass the number of keyword arguments. Fixes issue #15465. Signed-off-by: stijn <stijn@ignitron.net>
* py/mpz: Skip separators when running out of digits to print.Alessandro Gatti2024-09-26
| | | | | | | | | | This commit fixes the addition of a stray separator before the number when printing an MPZ-backed integer and the first group is three digits long. This fixes #8984. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* shared/runtime/sys_stdio_mphal: Fix printed type for stdio streams.timdechant2024-09-06
| | | | | | | | | The printed type for stdio streams indicates "FileIO", which is a binary IO stream. Stdio is not binary by design, and its printed type should indicate a text stream. "TextIOWrapper" suits that purpose, and is used by VfsPosix files. Signed-off-by: timdechant <timdechant.git@gmail.com>
* tests/basics: Add tests for optional args to int.to_bytes/from_bytes.Amirreza Hamzavi2024-09-02
| | | | Signed-off-by: Amirreza Hamzavi <amirrezahamzavi2000@gmail.com>
* py/objstr: Skip whitespace in bytes.fromhex().Glenn Moloney2024-08-19
| | | | | | | | | Skip whitespace characters between pairs of hex numbers. This makes `bytes.fromhex()` compatible with cpython. Includes simple test in `tests/basic/builtin_str_hex.py`. Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
* py/runtime: Fix self arg passed to classmethod when accessed via super.Damien George2024-07-25
| | | | | | Thanks to @AJMansfield for the original test case. Signed-off-by: Damien George <damien@micropython.org>
* py/objtype: Validate super() arguments.stijn2024-07-25
| | | | | | | | | | This fixes various null dereferencing and out-of-bounds access because super_attr assumes the held obj is effectively an object of the held type, which is now verified. Fixes issue #12830. Signed-off-by: stijn <stijn@ignitron.net>
* py/objint: Try to convert big-int back to small-int after binary op.Jim Mussared2024-07-01
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before this change, long/mpz ints propagated into all future calculations, even if their value could fit in a small-int object. With this change, the result of a big-int binary op will now be converted to a small-int object if the value fits in a small-int. For example, a relatively common operation like `x = a * b // c` where a,b,c all small ints would always result in a long/mpz int, even if it didn't need to, and then this would impact all future calculations with x. This adds +24 bytes on PYBV11 but avoids heap allocations and potential surprises (e.g. `big-big` is now a small `0`, and can safely be accessed with MP_OBJ_SMALL_INT_VALUE). Performance tests are unchanged on PYBV10, except for `bm_pidigits.py` which makes heavy use of big-ints and gains about 8% in speed. Unix coverage tests have been updated to cover mpz code that is now unreachable by normal Python code (removing the unreachable code would lead to some surprising gaps in the internal C functions and the functionality may be needed in the future, so it is kept because it has minimal overhead). This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/objint: Fix int.to_bytes() buffer size checks.Angus Gratton2024-06-24
| | | | | | | | | | | | | | | | | | | | | | | | Fixes and improvements to `int.to_bytes()` are: - No longer overflows if byte size is 0 (closes #13041). - Raises OverflowError in any case where number won't fit into byte length (now matches CPython, previously MicroPython would return a truncated bytes object). - Document that `micropython int.to_bytes()` doesn't implement the optional signed kwarg, but will behave as if `signed=True` when the integer is negative (this is the current behaviour). Add tests for this also. Requires changes for small ints, MPZ large ints, and "long long" large ints. Adds a new set of unit tests for ints between 32 and 64 bits to increase coverage of "long long" large ints, which are otherwise untested. Tested on unix port (64 bit small ints, MPZ long ints) and Zephyr STM32WB board (32 bit small ints, long long large ints). This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* tests/basics: Add tests to test repeated throw into the same generator.Damien George2024-06-21
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/lexer: Support raw f-strings.Damien George2024-06-06
| | | | | | | | Support for raw str/bytes already exists, and extending that to raw f-strings is easy. It also reduces code size because it eliminates an error message. Signed-off-by: Damien George <damien@micropython.org>
* py/lexer: Support concatenation of adjacent f-strings.Damien George2024-06-06
| | | | | | | This is quite a simple and small change to support concatenation of adjacent f-strings, and improve compatibility with CPython. Signed-off-by: Damien George <damien@micropython.org>
* tests/basics: Move str/bytes tests that give SyntaxWarning to sep file.Damien George2024-05-28
| | | | | | | In CPython 3.12 these invalid str/bytes/fstring escapes will issue a SyntaxWarning, and so differ to MicroPython. Signed-off-by: Damien George <damien@micropython.org>
* tests/basics: Add .exp file for slice_op test.Damien George2024-05-28
| | | | | | CPython 3.12 implemented hashing for slices, so now differs to MicroPython. Signed-off-by: Damien George <damien@micropython.org>
* tests/basics: Split out generator.throw tests that pass multiple args.Damien George2024-05-27
| | | | | | | | The three-argument form of `.throw()` is deprecated since CPython 3.12. So split out into separate tests (with .exp files) the parts of the generator tests that test more than one argument. Signed-off-by: Damien George <damien@micropython.org>
* py/objarray: Fix use-after-free if extending a bytearray from itself.Angus Gratton2024-04-22
| | | | | | | | | | | | | | | | | | | | | | | Two cases, one assigning to a slice. Closes https://github.com/micropython/micropython/issues/13283 Second is extending a slice from itself, similar logic. In both cases the problem occurs when m_renew causes realloc to move the buffer, leaving a dangling pointer behind. There are more complex and hard to fix cases when either argument is a memoryview into the buffer, currently resizing to a new address breaks memoryviews into that object. Reproducing this bug and confirming the fix was done by running the unix port under valgrind with GC-aware extensions. Note in default configurations with GIL this bug exists but has no impact (the free buffer won't be reused while the function is still executing, and is no longer referenced after it returns). Signed-off-by: Angus Gratton <angus@redyak.com.au>
* tests/basics: Split MicroPython-specific deque tests to separate file.Damien George2024-03-19
| | | | | | | So that the MicroPython-specific behaviour can be isolated, and the CPython compatible test don't need a .exp file. Signed-off-by: Damien George <damien@micropython.org>
* py/objdeque: Expand implementation to be doubly-ended and support iter.Dash Peters2024-03-18
| | | | | | | | | | | | | Add `pop()`, `appendleft()`, and `extend()` methods, support iteration and indexing, and initializing from an existing sequence. Iteration and indexing (subscription) have independent configuration flags to enable them. They are enabled by default at the same level that collections.deque is enabled (the extra features level). Also add tests for checking new behavior. Signed-off-by: Damien George <damien@micropython.org>
* all: Prune trailing whitespace.Phil Howard2024-03-07
| | | | | | | | | | | | | | | | Prune trailing whitespace across the whole project (almost), done automatically with: grep -IUrl --color "[[:blank:]]$" --exclude-dir=.git --exclude=*.exp |\ xargs sed -i 's/[[:space:]]*$//' Exceptions: - Skip third-party code in lib/ and drivers/cc3100/ - Skip generated code in bluetooth_init_cc2564C_1.5.c - Preserve command output whitespace in docs, eg: docs/esp8266/tutorial/repl.rst Signed-off-by: Phil Howard <phil@gadgetoid.com>
* py/builtinevex: Fix setting globals for native functions in compile().Damien George2024-02-20
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/compile: Fix potential Py-stack overflow in try-finally with return.Damien George2024-01-31
| | | | | | | | | | | | | | | | | | | | If a return is executed within the try block of a try-finally then the return value is stored on the top of the Python stack during the execution of the finally block. In this case the Python stack is one larger than it normally would be in the finally block. Prior to this commit, the compiler was not taking this case into account and could have a Python stack overflow if the Python stack used by the finally block was more than that used elsewhere in the function. In such a scenario the last argument of the function would be clobbered by the top-most temporary value used in the deepest Python expression/statement. This commit fixes that case by making sure enough Python stack is allocated to the function. Fixes issue #13562. Signed-off-by: Damien George <damien@micropython.org>
* all: Fix "reuse" and "overridden" spelling mistakes.Damien George2024-01-05
| | | | | | | Codespell doesn't pick up "re-used" or "re-uses", and ignores the tests/ directory, so fix these manually. Signed-off-by: Damien George <damien@micropython.org>
* py/modsys: Implement optional sys.intern.stijn2023-12-15
| | | | Signed-off-by: stijn <stijn@ignitron.net>
* py/objslice: Validate that the argument to indices() is an integer.Damien George2023-11-21
| | | | | | | | | | 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>
* py/objboundmeth: Optimise check for types in binary_op.Damien George2023-10-13
| | | | Signed-off-by: Damien George <damien@micropython.org>
* tests/basics/boundmeth1.py: Add tests for bound method equality/hash.Ned Konz2023-10-13
| | | | | | | 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>
* py/vm: Don't emit warning when using "raise ... from None".Matthias Urlichs2023-10-09
| | | | | | "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.
* py/modstruct: Support pad bytes in struct format.Daniël van de Giessen2023-09-01
| | | | | | | | | | | 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>
* py/objstr: Fix `str % {}` edge case.mcskatkat2023-09-01
| | | | | | | Eliminate `TypeError` when format string contains no named conversions. This matches CPython behavior. Signed-off-by: mcskatkat <mc_skatkat@hotmail.com>
* py/compile: Fix async for's stack handling of iterator expression.Damien George2023-07-13
| | | | | | | | | | | | | 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>
* py/lexer: Allow conversion specifiers in f-strings (e.g. !r).Jared Hancock2023-06-14
| | | | | | | | | | | | | | | | | | | | | | 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>
* tests: Replace umodule with module everywhere.Jim Mussared2023-06-08
| | | | | | This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/objint: Allow int() to parse anything with the buffer protocol.Damien George2023-06-01
| | | | | | | | This generalises and simplifies the code and follows CPython behaviour. See similar change for floats in a07fc5b6403b9a8bf7e7cb64f857272e5346d7e2. Signed-off-by: Damien George <damien@micropython.org>
* py/obj: Accept user types in mp_obj_get_int_maybe.Damien George2023-06-01
| | | | | | | | | 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>
* py: Change MP_UNARY_OP_INT to MP_UNARY_OP_INT_MAYBE.Damien George2023-06-01
| | | | | | | | 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>
* tests/basics: Remove __index__ and __inv__ from special methods tests.Damien George2023-06-01
| | | | | | | 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>
* py/runtime: If inplace binop fails then try corresponding normal binop.Damien George2023-05-19
| | | | | | | | 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>
* py/objstr: Return unsupported binop instead of raising TypeError.Damien George2023-05-19
| | | | | | | 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>
* py/objarray: Disallow memoryview addition.Damien George2023-05-19
| | | | | | | Following CPython. This is important for subsequent commits to work correctly. Signed-off-by: Damien George <damien@micropython.org>