Commit Graph

2938 Commits

Author SHA1 Message Date
Bas van Sisseren
a14ce77b28 py/binary.c: Fix bug when packing big-endian 'Q' values.
Without bugfix:

    struct.pack('>Q', 16)
    b'\x00\x00\x00\x10\x00\x00\x00\x00'

With bugfix:

    struct.pack('>Q', 16)
    b'\x00\x00\x00\x00\x00\x00\x00\x10'
2017-08-15 11:33:43 +10:00
Javier Candeira
35a1fea90b all: Raise exceptions via mp_raise_XXX
- Changed: ValueError, TypeError, NotImplementedError
  - OSError invocations unchanged, because the corresponding utility
    function takes ints, not strings like the long form invocation.
  - OverflowError, IndexError and RuntimeError etc. not changed for now
    until we decide whether to add new utility functions.
2017-08-13 22:52:33 +10:00
Paul Sokolovsky
bfc2092dc5 py/modsys: Initial implementation of sys.getsizeof().
Implemented as a new MP_UNARY_OP. This patch adds support lists, dicts and
instances.
2017-08-11 09:43:07 +03:00
Damien George
7d4a2f773c all: Make use of $(TOP) variable in Makefiles, instead of "..".
$(TOP) is defined in py/mkenv.mk and should be used to refer to the top
level of this repository.
2017-08-11 12:22:19 +10:00
Damien George
3d25d9c7d9 py/objstr: Raise an exception for wrong type on RHS of str binary op.
The main case to catch is invalid types for the containment operator, of
the form str.__contains__(non-str).
2017-08-09 21:25:48 +10:00
Damien George
eb2784e8a2 py/objtuple: Allow to use inplace-multiplication operator on tuples. 2017-08-09 21:20:42 +10:00
Paul Sokolovsky
4dc7c5649b py/mkrules.mk: Show frozen modules sizes together with executable size.
This works for Unix and similar ports so far.
2017-08-06 15:43:21 +03:00
Damien George
0f12082f5b py,extmod,stmhal: Use "static inline" for funcs that should be inline.
"STATIC inline" can expand to "inline" if STATIC is defined to nothing, and
this case can lead to link errors.
2017-08-02 13:42:34 +10:00
Alexander Steffen
55f33240f3 all: Use the name MicroPython consistently in comments
There were several different spellings of MicroPython present in comments,
when there should be only one.
2017-07-31 18:35:40 +10:00
Damien George
b62bb53d0e py/modsys: Use MP_ROM_INT for int values in an mp_rom_map_elem_t. 2017-07-31 12:59:39 +10:00
Paul Sokolovsky
456450437f py/modio: BufferedWriter: Convert to mp_rom_map_elem_t. 2017-07-28 21:54:59 +03:00
Damien George
04552ff71b py: Implement raising a big-int to a negative power.
Before this patch raising a big-int to a negative power would just return
0.  Now it returns a floating-point number with the correct value.
2017-07-25 11:49:22 +10:00
Damien George
4d1fb6107f py/mpz: Make mpz_is_zero() an inline function.
It's more efficient as an inline function, and saves code size.
2017-07-25 11:32:04 +10:00
Damien George
aa7be82a4d all: Don't include system errno.h when it's not needed. 2017-07-24 18:43:14 +10:00
Damien George
a559098fec py/mperrno: Allow mperrno.h to be correctly included before other hdrs.
Before this patch the mperrno.h file could be included and would silently
succeed with incorrect config settings, because mpconfig.h was not yet
included.
2017-07-24 18:41:24 +10:00
Damien George
1ed3356540 py/py.mk: Make berkeley-db C-defs apply only to relevant source files.
Otherwise they can interfere (eg redefinition of "abort") with other source
files in a given uPy port.
2017-07-24 15:50:47 +10:00
Tom Collins
bb3bddabb5 py/builtinevex: Add typechecking of globals/locals args to eval/exec. 2017-07-21 15:16:51 +10:00
Damien George
761e4c7ff6 all: Remove trailing spaces, per coding conventions. 2017-07-19 13:12:10 +10:00
Damien George
cadbd7f3e6 py/modmicropython: Cast stack_limit value so it prints correctly.
Without this cast the print will give a wrong result on nan-boxing builds.
2017-07-18 22:30:22 +10:00
Damien George
3235b95f08 py/asmx64: Support moving a 64-bit immediate to one of top 8 registers.
If constants (eg mp_const_none_obj) are placed in very high memory
locations that require 64-bits for the pointer then the assembler must be
able to emit instructions to move such pointers to one of the top 8
registers (ie r8-r15).
2017-07-18 17:30:23 +10:00
Damien George
016325dd0a py/vm: Make n_state variable local to just set-up part of VM.
It's not used anywhere else in the VM loop, and clashes with (is shadowed
by) the n_state variable that's redeclared towards the end of the
mp_execute_bytecode function.  Code size is unchanged.
2017-07-18 16:17:23 +10:00
Alexander Steffen
299bc62586 all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.

This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.

The rules are as follows.

Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _

In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.

py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-07-18 11:57:39 +10:00
Damien George
12d4fa9b37 py/gc: Refactor assertions in gc_free function.
gc_free() expects either NULL or a valid pointer into the heap, so the
checks for a valid pointer can be turned into assertions.
2017-07-12 12:17:38 +10:00
Damien George
0291a624cb py/compile: Fix enum variable declaration. 2017-07-09 13:18:14 +10:00
Damien George
f69ab79ec8 py/objgenerator: Allow to hash generators and generator instances.
Adds nothing to the code size, since it uses existing empty slots in the
type structures.
2017-07-07 11:47:38 +10:00
Tom Collins
145796f037 py,extmod: Some casts and minor refactors to quiet compiler warnings. 2017-07-07 11:32:22 +10:00
Krzysztof Blazewicz
91a385db98 py/compile: Use switch-case to match token and operator.
Reduces code size.
2017-07-05 15:50:36 +10:00
Krzysztof Blazewicz
a040fb89e7 py/compile: Combine arith and bit-shift ops into 1 compile routine.
This refactoring saves code space.
2017-07-05 15:49:00 +10:00
Damien George
e66fd56852 py/repl: Change mp_uint_t to size_t in repl helpers. 2017-07-04 23:44:54 +10:00
Damien George
7bd10c1ffe py: Change mp_uint_t to size_t in builtins code. 2017-07-04 23:44:22 +10:00
Damien George
8b84b8ab8a py/objdict: Factorise dict accessor helper to reduce code size.
Code size change in bytes for this patch is:

   bare-arm:   -72
minimal x86:   -48
   unix x64:   -32
unix nanbox:  -120
     stmhal:   -68
     cc3200:   -64
    esp8266:   -56
2017-07-04 23:24:59 +10:00
Damien George
b51919f5b7 py/makeversionhdr.py: Update to parse new release line in docs/conf.py.
The line in docs/conf.py with the release/version number was recently
changed and this patch makes the makeversionhdr.py script work again.
2017-07-04 22:37:41 +10:00
Damien George
6b8b56f859 py/modmath: Check for zero division in log with 2 args. 2017-07-04 02:15:11 +10:00
Damien George
9ed5e80eea py/vm: Make "if" control flow more obvious in YIELD_FROM opcode. 2017-07-04 02:14:25 +10:00
Damien George
9d2c72ad4f py/objstr: Remove unnecessary "sign" variable in formatting code. 2017-07-04 02:13:27 +10:00
Damien George
2138258fea py/runtime: Mark m_malloc_fail() as NORETURN. 2017-07-04 02:12:36 +10:00
Damien George
9aeba3e41b py/binary: Add missing "break" statements. 2017-07-04 02:11:46 +10:00
Damien George
65417c5ad9 py/objstr: Move uPy function wrappers to just after the C function.
This matches the coding/layout style of all the other objects.
2017-07-02 23:35:42 +10:00
Damien George
adf22c19ae py/mpprint: Remove unreachable check for neg return of mp_format_float. 2017-06-30 12:10:50 +10:00
Damien George
265500c5c8 py/objnamedtuple: Simplify and remove use of alloca building namedtuple.
Prior to this patch there were 2 paths for creating the namedtuple, one for
when no keyword args were passed, and one when there were keyword args.
And alloca was used in the keyword-arg path to temporarily create the array
of elements for the namedtuple, which would then be copied to a
heap-allocated object (the namedtuple itself).

This patch simplifies the code by combining the no-keyword and keyword
paths, and removing the need for the alloca by constructing the namedtuple
on the heap before populating it.

Heap usage in unchanged, stack usage is reduced, use of alloca is removed,
and code size is not increased and is actually reduced by between 20-30
bytes for most ports.
2017-06-29 17:40:25 +10:00
Damien George
2f7fad66a2 py/builtinimport: Remove unreachable code for relative imports.
The while-loop that calls chop_component will guarantee that level==-1 at
the end of the loop.  Hence the code following it is unnecessary.

The check for p==this_name will catch imports that are beyond the
top-level, and also covers the case of new_mod_q==MP_QSTR_ (equivalent to
new_mod_l==0) so that check is removed.

There is also a new check at the start for level>=0 to guard against
__import__ being called with bad level values.
2017-06-28 12:21:29 +10:00
Alexander Steffen
cd0987f5b7 py/frozenmod.h: Add missing header guards 2017-06-28 00:51:45 +03:00
Damien George
c408ed9fb1 py/mpconfig.h: Remove spaces in "Micro Python" and remove blank line. 2017-06-26 12:29:20 +10:00
Damien George
d94bc675e8 py/compile: Optimise emitter label indices to save a word of heap.
Previous to this patch, a label with value "0" was used to indicate an
invalid label, but that meant a wasted word (at slot 0) in the array of
label offsets.  This patch adjusts the label indices so the first one
starts at 0, and the maximum value indicates an invalid label.
2017-06-22 15:05:58 +10:00
Damien George
4c5f108321 py/compile: Fix bug with break/continue in else of optimised for-range.
This patch fixes a bug whereby the Python stack was not correctly reset if
there was a break/continue statement in the else black of an optimised
for-range loop.

For example, in the following code the "j" variable from the inner for loop
was not being popped off the Python stack:

    for i in range(4):
        for j in range(4):
            pass
        else:
            continue

This is now fixed with this patch.
2017-06-22 13:50:33 +10:00
Damien George
e269cabe3e py/objint: In to_bytes(), allow length arg to be any int and check sign. 2017-06-15 14:21:02 +10:00
Damien George
8c5632a869 py/objint: Support "big" byte-order in int.to_bytes(). 2017-06-15 13:56:21 +10:00
Damien George
48d867b4a6 all: Make more use of mp_raise_{msg,TypeError,ValueError} helpers. 2017-06-15 11:54:41 +10:00
Damien George
1e70fda69f py/compile: Raise SyntaxError if positional args are given after */**.
In CPython 3.4 this raises a SyntaxError.  In CPython 3.5+ having a
positional after * is allowed but uPy has the wrong semantics and passes
the arguments in the incorrect order.  To prevent incorrect use of a
function going unnoticed it is important to raise the SyntaxError in uPy,
until the behaviour is fixed to follow CPython 3.5+.
2017-06-14 18:18:01 +10:00
Damien George
e374cfff80 py/modthread: Raise RuntimeError in release() if lock is not acquired. 2017-06-14 14:43:50 +10:00