Commit Graph

55 Commits

Author SHA1 Message Date
David Lechner 093fd80760 py/modsys: Use consistent naming pattern for module-level const objects.
This renames a few identifiers to follow the usual naming convention of
mp_<module>_<name>.  This makes them easier to find, e.g. when grep'ing.
2020-05-28 10:02:14 +10:00
Damien George 69661f3343 all: Reformat C and Python source code with tools/codeformat.py.
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-28 10:33:03 +11:00
Damien George 80df377e95 py/modsys: Report .mpy version in sys.implementation.
This commit adds a sys.implementation.mpy entry when the system supports
importing .mpy files.  This entry is a 16-bit integer which encodes two
bytes of information from the header of .mpy files that are supported by
the system being run: the second and third bytes, .mpy version, and flags
and native architecture.  This allows determining the supported .mpy file
dynamically by code, and also for the user to find it out by inspecting
this value.  It's further possible to dynamically detect if the system
supports importing .mpy files by `hasattr(sys.implementation, 'mpy')`.
2019-11-04 16:00:41 +11:00
Milan Rossa 310b3d1b81 py: Integrate sys.settrace feature into the VM and runtime.
This commit adds support for sys.settrace, allowing to install Python
handlers to trace execution of Python code.  The interface follows CPython
as closely as possible.  The feature is disabled by default and can be
enabled via MICROPY_PY_SYS_SETTRACE.
2019-08-30 16:44:12 +10:00
Milan Rossa cb3647004f py: Implement new sys.atexit feature.
This patch implements a new sys.atexit function which registers a function
that is later executed when the main script ends.  It is configurable via
MICROPY_PY_SYS_ATEXIT, disabled by default.

This is not compliant with CPython, rather it can be used to implement a
CPython compatible "atexit" module if desired (similar to how
sys.print_exception can be used to implement functionality of the
"traceback" module).
2019-08-15 17:30:50 +10:00
Damien George 7cd59c5bc3 py/mpconfig: Move MICROPY_VERSION macros to static ones in mpconfig.h.
It's more robust to have the version defined statically in a header file,
rather than dynamically generating it via git using a git tag.  In case
git doesn't exist, or a different source control tool is used, it's
important to still have the uPy version number available.
2018-12-22 01:40:38 +11:00
Damien George 582b190764 py: Add checks for stream objects in print() and sys.print_exception(). 2018-06-20 15:57:10 +10:00
Damien George 323b5f7270 py/modsys: Don't compile getsizeof function if feature is disabled. 2018-04-04 14:23:25 +10:00
Damien George a3dc1b1957 all: Remove inclusion of internal py header files.
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-10-04 12:37:50 +11:00
Damien George 0102ee092b py: Change obsolete "///" comment formatting to normal comments.
This comment style is no longer used because the docs are written by hand,
not generated.
2017-08-30 21:02:00 +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
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
Damien George e1cda00387 py/modsys: Allow to compile with obj-repr D and PY_ATTRTUPLE disabled. 2017-06-08 00:41:27 +10:00
Tom Collins f06d0839bd py/modsys: update conditionals for code referencing sys.stdout
Working on a build with PY_IO enabled (for PY_UJSON support) but PY_SYS_STDFILES disabled (no filesystem).  There are multiple references to mp_sys_stdout_obj that should only be enabled if both PY_IO and PY_SYS_STDFILES are enabled.
2017-05-14 18:24:50 +03:00
Paul Sokolovsky 96aa3a3102 py/modsys: Use MP_SMALL_INT_MAX for sys.maxsize in case of LONGINT_IMPL_NONE.
INT_MAX used previosly is indeed max value for int, whereas on LP64
platforms, long is used for mp_int_t. Using MP_SMALL_INT_MAX is the
correct way to do it anyway.
2017-03-06 12:15:25 +01:00
Damien George 93c4a6a3f7 all: Remove 'name' member from mp_obj_module_t struct.
One can instead lookup __name__ in the modules dict to get the value.
2016-09-22 00:23:16 +10:00
Damien George 4b72b3a133 py: Change type signature of builtin funs that take variable or kw args.
With this patch the n_args parameter is changed type from mp_uint_t to
size_t.
2016-01-11 00:49:27 +00:00
Damien George 3911d5af32 py/modsys: Fix module globals table to use MP_ROM_QSTR. 2015-12-16 19:40:14 -05:00
Paul Sokolovsky f2d532c404 py/modsys: Use MP_ROM_PTR() initializer for sys.modules.
Based on similar usage for sys.argv/sys.path.
2015-12-05 00:27:04 +02:00
Paul Sokolovsky 1a1d11fa32 py/modsys: Implement sys.modules.
This for example will allow people to reload modules which didn't load
successfully (e.g. due to syntax error).
2015-12-05 00:13:29 +02:00
Damien George 999cedb90f py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.
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-29 14:25:35 +00:00
Damien George cbf7674025 py: Add MP_ROM_* macros and mp_rom_* types and use them. 2015-11-29 14:25:04 +00:00
Paul Sokolovsky 72bd172b30 py/modsys: Consistently use indented #if's. 2015-11-21 16:03:37 +02:00
Damien George 3a2171e406 py: Eliminate some cases which trigger unused parameter warnings. 2015-09-04 16:53:46 +01:00
Damien George 95f53461c2 py: Replace py-version.sh with makeversionhdr.py, written in Python.
Also rename py-version.h to mpversion.h for consistency with mpconfig.h.
2015-04-28 23:52:36 +01:00
Paul Sokolovsky 8b85d14b92 modsys: Add basic sys.exc_info() implementation.
The implementation is very basic and non-compliant and provided solely for
CPython compatibility. The function itself is bad Python2 heritage, its
usage is discouraged.
2015-04-25 03:49:23 +03:00
Damien George c3184aea63 py: Add sys.implementation, containing uPy name and version number.
Uses attrtuple if it's enabled, otherwise just a normal tuple.
2015-04-21 14:45:04 +00:00
Damien George 5ae5ec986e py: Make mp_sys_stdout_print object, wrapping sys.stdout for mp_print*.
So now all printing should go via either mp_plat_print or
mp_sys_stdout_print.
2015-04-16 14:30:16 +00:00
Damien George 7f9d1d6ab9 py: Overhaul and simplify printf/pfenv mechanism.
Previous to this patch the printing mechanism was a bit of a tangled
mess.  This patch attempts to consolidate printing into one interface.

All (non-debug) printing now uses the mp_print* family of functions,
mainly mp_printf.  All these functions take an mp_print_t structure as
their first argument, and this structure defines the printing backend
through the "print_strn" function of said structure.

Printing from the uPy core can reach the platform-defined print code via
two paths: either through mp_sys_stdout_obj (defined pert port) in
conjunction with mp_stream_write; or through the mp_plat_print structure
which uses the MP_PLAT_PRINT_STRN macro to define how string are printed
on the platform.  The former is only used when MICROPY_PY_IO is defined.

With this new scheme printing is generally more efficient (less layers
to go through, less arguments to pass), and, given an mp_print_t*
structure, one can call mp_print_str for efficiency instead of
mp_printf("%s", ...).  Code size is also reduced by around 200 bytes on
Thumb2 archs.
2015-04-16 14:30:16 +00:00
stijn e50cff69bb py: Use a dummy type for referring to extern structs
Fixes msvc linker warnings about mismatching sizes between the mp_obj_fdfile_t
struct defined in file.c and the mp_uint_t declarations found in modsys.c and modbuiltins.c
2015-04-09 15:03:22 +02:00
Damien George e1e359ff59 py: Put mp_sys_path, mp_sys_argv and gc_collected in mp_state_ctx_t.
Without mp_sys_path and mp_sys_argv in the root pointer section of the
state, their memory was being incorrectly collected by GC.
2015-02-07 17:24:10 +00:00
Damien George cd34207409 py: Can compile with -Wmissing-declarations and -Wmissing-prototypes. 2015-01-12 22:30:49 +00:00
Damien George 51dfcb4bb7 py: Move to guarded includes, everywhere in py/ core.
Addresses issue #1022.
2015-01-01 20:32:09 +00:00
Paul Sokolovsky 46c3ab2004 modsys: Add sys.print_exception(exc, file=sys.stdout) function.
The function is modeled after traceback.print_exception(), but unbloated,
and put into existing module to save overhead on adding another module.
Compliant traceback.print_exception() is intended to be implemented in
micropython-lib in terms of sys.print_exception().

This change required refactoring mp_obj_print_exception() to take pfenv_t
interface arguments.

Addresses #751.
2014-12-08 20:25:49 +00:00
Damien George 3b603f29ec Use MP_DEFINE_CONST_DICT macro to define module dicts.
This is just a clean-up of the code.  Generated code is exactly the
same.
2014-11-29 14:39:27 +00:00
Damien George b92cbe6129 py: Move definition of mp_sys_exit to core.
sys.exit always raises SystemExit so doesn't need a special
implementation for each port.  If C exit() is really needed, use the
standard os._exit function.

Also initialise mp_sys_path and mp_sys_argv in teensy port.
2014-09-15 15:53:09 +01:00
Damien George 4ef26c14b1 doc: Fix up a few docs in sys module. 2014-08-10 17:53:43 +01:00
Damien George 30dd23aa7f doc: Document gc, sys, math, cmath. 2014-08-10 17:50:28 +01:00
Paul Sokolovsky cb66f41ebc py: Make print() accept "file" argument, and actually print to stream.
And not system printf(), like it was before. For this, move pfenv_printf()
from stmhal port to py/.
2014-07-19 21:27:22 +03:00
Paul Sokolovsky 4e0eeebdc2 py: Implement sys.maxsize, standard way to check platform "bitness".
Implementing it as a static constant is a bit peculiar and require cooperation
from long int implementation.
2014-07-03 18:09:36 +03:00
Paul Sokolovsky 59c675a64c py: Include mpconfig.h before all other includes.
It defines types used by all other headers.

Fixes #691.
2014-06-21 22:43:22 +03:00
Emmanuel Blot f6932d6506 Prefix ARRAY_SIZE with micropython prefix MP_ 2014-06-19 18:54:34 +02:00
Paul Sokolovsky b9b9354e6c modsys: Add optional support for sys.platform.
Ports which wants to have it, should define MICROPY_PY_SYS_PLATFORM to a
string value they need.
2014-06-07 23:40:04 +03:00
Damien George ee3fd46f13 Rename configuration variables controling Python features.
Now of the form MICROPY_PY_*.  See issue #35.
2014-05-24 23:03:12 +01:00
Damien George 89755ae67f py: Rename MICROPY_SYS_EXIT to MICROPY_MOD_SYS_EXIT.
For consistency with MICROPY_MOD_SYS_STDFILES, etc.
2014-05-11 17:35:43 +01:00
Paul Sokolovsky deaeaac469 modsys: Enable sys.exit() per port after all. 2014-05-10 17:26:47 +03:00
Paul Sokolovsky d99e9083cb modsys, unix: Add sys.exit(), should be implemented by a port. 2014-05-10 16:56:21 +03:00
Damien George 04b9147e15 Add license header to (almost) all files.
Blanket wide to all .c and .h files.  Some files originating from ST are
difficult to deal with (license wise) so it was left out of those.

Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.
2014-05-03 23:27:38 +01:00
Damien George 6d3c5e4301 Add ARRAY_SIZE macro, and use it where possible. 2014-04-26 10:47:29 +01:00