Commit Graph

211 Commits

Author SHA1 Message Date
Paul Sokolovsky cb9dc086a3 modio: Implement io.StringIO class. 2014-04-26 20:26:14 +03:00
Paul Sokolovsky dec31bb872 objexcept: Add mp_obj_new_exception_arg1() convenience function. 2014-04-22 00:02:51 +03:00
Damien George 3558f62fb5 py: Making closures now passes pointer to stack, not a tuple for vars.
Closed over variables are now passed on the stack, instead of creating a
tuple and passing that.  This way memory for the closed over variables
can be allocated within the closure object itself.  See issue #510 for
background.
2014-04-20 17:50:40 +01:00
Paul Sokolovsky 4602b9a79f obj.h: Typo fix in comment. 2014-04-19 03:21:06 +03:00
Damien George b11b85adaa py: Allow to pass buffer protocol flags to get_buffer helper funcs. 2014-04-18 22:59:24 +01:00
Damien George 57a4b4f178 py: Add typecode to buffer protocol.
When querying an object that supports the buffer protocol, that object
must now return a typecode (as per binary.[ch]).  This does not have to
be honoured by the caller, but can be useful for determining element
size.
2014-04-18 22:29:21 +01:00
Damien George ea8d06c39d py: Add MP_OBJ_STOP_ITERATION and make good use of it.
Also make consistent use of MP_OBJ_NOT_SUPPORTED and MP_OBJ_NULL.
This helps a lot in debugging and understanding of function API.
2014-04-17 23:19:36 +01:00
Damien George 729f7b42d6 py: Merge BINARY_OP_SUBSCR and store_subscr (w/ delete) into subscr.
mp_obj_t->subscr now does load/store/delete.
2014-04-17 22:10:53 +01:00
Damien George de7c425139 py: Simplify objfun/objgenerator connection, no need to call bc_get. 2014-04-17 19:16:11 +01:00
Damien George 71d3112f7e py: Make built-in 'range' a class.
Addresses issue #487.
2014-04-17 18:18:55 +01:00
Damien George 777b0f32f4 py: Add property object, with basic functionality.
Enabled by MICROPY_ENABLE_PROPERTY.
2014-04-13 18:59:45 +01:00
Paul Sokolovsky 18bef25a0c objlist: Add support for statically allocated lists.
Similar to similar support for lists.
2014-04-13 07:02:56 +03:00
Damien George 2813cb6043 py: Add 'static' to inline function MP_BOOL; remove category_t.
Small fixes to get it compiling with ARMCC.  I have no idea why
category_t was in the enum definition for qstrs...
2014-04-12 17:53:05 +01:00
Damien George db049c2e00 py: Change inline to static inline for 2 functions. 2014-04-12 00:08:40 +01:00
Damien George 1184094839 py: Revert some inline functions back to macros, since they bloat stmhal. 2014-04-11 22:30:09 +01:00
Damien George a5c82a8187 py: Convert some macros to inline functions (in obj.h).
Also convert mp_obj_is_integer to an inline function.

Overall this decreased code size (at least on 32-bit x86 machine).
2014-04-11 11:16:53 +00:00
Damien George ae491055fa py: Fix float/complex binop returning NULL; implement complex power. 2014-04-10 20:08:11 +01:00
Damien George b5fbd0ba87 py: Add mp_obj_is_integer; make mp_get_index check for long int.
mp_obj_is_integer should be used to check if an object is of integral
type.  It returns true for bool, small int and long int.
2014-04-09 19:55:33 +01:00
Paul Sokolovsky 3aa8ee7c9e py: Add mp_get_buffer(), mp_get_buffer_raise() convenience functions to API. 2014-04-09 01:07:37 +03:00
Damien George f4c9b33abf py: Remove DELETE_SUBSCR opcode, combine with STORE_SUBSCR.
This makes the runtime and object APIs more consistent.  mp_store_subscr
functionality now moved into objects (ie list and dict store_item).
2014-04-08 21:32:29 +01:00
Damien George 4671392d90 Merge branch 'master' of github.com:micropython/micropython 2014-04-08 21:13:04 +01:00
Damien George 1d24ea5207 py: Finish implementation of all del opcodes.
At this point, all opcodes are now implemented!

Some del opcodes have been combined with store opcodes, with the value
to store being MP_OBJ_NULL.
2014-04-08 21:11:49 +01:00
Paul Sokolovsky 4dcb605ac4 py: Make bytearray a proper type. 2014-04-08 22:11:40 +03:00
Damien George 495d781a36 py: implement UNPACK_EX byte code (for: a, *b, c = d) 2014-04-08 17:51:47 +01:00
Damien George 27e735fd18 py: Replace stream_p with *stream_p in mp_obj_type_t.
This is to reduce ROM usage.  stream_p is used in file and socket types
only (at the moment), so seems a good idea to make the protocol
functions a pointer instead of the actual structure.

It saves 308 bytes of ROM in the stmhal/ port, 928 in unix/.
2014-04-05 23:02:23 +01:00
Damien George 8b0535e23f py: Change module globals from mp_map_t* to mp_obj_dict_t*.
Towards addressing issue #424.

Had a small increase to ROM usage (order 60 bytes).
2014-04-05 21:53:54 +01:00
Damien George 95004e5114 py: Fix delete operation on map/dict and set objects.
Hash table can now be completely full (ie now NULL entry) before a
resize is triggered.  Use sentinel value to indicate delete entry in the
table.
2014-04-05 17:17:19 +01:00
Damien George 66edc5d899 py: Implement DELETE_SUBSCR bytecode; implement mp_obj_dict_delete. 2014-04-05 13:25:13 +01:00
Paul Sokolovsky e3f58c8380 map: Add mp_map_dump() (#ifdef'ed) to be handy when debugging maps. 2014-04-05 05:10:02 +03:00
Damien George 36f0ee1a54 py: Remove mp_obj_less (use mp_binary_op(MP_BINARY_OP_LESS..) instead). 2014-04-04 15:07:17 +01:00
Damien George 6902eeda25 py: Add m_malloc_fail function to handle memory allocation error.
A malloc/realloc fail now throws MemoryError.
2014-04-04 10:52:59 +00:00
Damien George 8270e3853d py: More robust int conversion and overflow checking. 2014-04-03 11:00:54 +00:00
Paul Sokolovsky a96d3d0840 objexcept: No more magic messages in exceptions, only exception arguments.
One of the reason for separate "message" (besides still unfulfilled desire to
optimize memory usage) was apparent special handling of exception with
messages by CPython. Well, the message is still just an exception argument,
it just printed specially. Implement that with PRINT_EXC printing format.
2014-03-31 02:23:56 +03:00
Damien George 0aa4379543 Merge pull request #399 from pfalcon/gen-defargs
objgenerator: Handle default args to generator functions.
2014-03-30 21:51:49 +01:00
Paul Sokolovsky 7fafb28f6d objgenerator: Handle default args to generator functions.
Addresses #397.
2014-03-30 20:25:19 +03:00
Paul Sokolovsky 55ca075cab vm: Implement CALL_FUNCTION_VAR opcode (foo(*(1, 2, 3))). 2014-03-30 17:47:16 +03:00
Damien George df6567e634 Merge map.h into obj.h.
Pretty much everyone needs to include map.h, since it's such an integral
part of the Micro Python object implementation.  Thus, the definitions
are now in obj.h instead.  map.h is removed.
2014-03-30 13:54:02 +01:00
Damien George d17926db71 Rename rt_* to mp_*.
Mostly just a global search and replace.  Except rt_is_true which
becomes mp_obj_is_true.

Still would like to tidy up some of the names, but this will do for now.
2014-03-30 13:35:08 +01:00
Damien George 3e1a5c10c5 py: Rename old const type objects to mp_type_* for consistency. 2014-03-29 13:43:38 +00:00
Damien George 07ddab529c py: Change mp_const_* objects to macros.
Addresses issue #388.
2014-03-29 13:15:08 +00:00
Paul Sokolovsky c4d589e2bb objgenerator: close(): Throw instance of GeneratorExit (not type).
To comply with Python semantics and allow use of mp_obj_is_subclass_fast()
for exception matching.
2014-03-28 02:40:26 +02:00
Damien George c63f984647 py: Thin out predefined exceptions.
Only exceptions that are actually used are left prefedined.  Hierarchy
is still there, and removed exceptions are just commented out.
2014-03-27 23:49:06 +00:00
Damien George bee17b00e3 py: Put n_state for bytecode in the bytecode prelude.
Rationale: setting up the stack (state for locals and exceptions) is
really part of the "code", it's the prelude of the function.  For
example, native code adjusts the stack pointer on entry to the function.
Native code doesn't need to know n_state for any other reason.  So
putting the state size in the bytecode prelude is sensible.

It reduced ROM usage on STM by about 30 bytes :)  And makes it easier to
pass information about the bytecode between functions.
2014-03-27 11:07:04 +00:00
Damien George 9b196cddab Remove mp_obj_type_t.methods entry and use .locals_dict instead.
Originally, .methods was used for methods in a ROM class, and
locals_dict for methods in a user-created class.  That distinction is
unnecessary, and we can use locals_dict for ROM classes now that we have
ROMable maps.

This removes an entry in the bloated mp_obj_type_t struct, saving a word
for each ROM object and each RAM object.  ROM objects that have a
methods table (now a locals_dict) need an extra word in total (removed
the methods pointer (1 word), no longer need the sentinel (2 words), but
now need an mp_obj_dict_t wrapper (4 words)).  But RAM objects save a
word because they never used the methods entry.

Overall the ROM usage is down by a few hundred bytes, and RAM usage is
down 1 word per user-defined type/class.

There is less code (no need to check 2 tables), and now consistent with
the way ROM modules have their tables initialised.

Efficiency is very close to equivaluent.
2014-03-26 21:47:19 +00:00
Damien George c12b2213c1 Change mp_method_t.name from const char * to qstr.
Addresses issue #377.
2014-03-26 20:15:40 +00:00
Damien George 66eaf84b8c py: Replace mp_const_stop_iteration object with MP_OBJ_NULL. 2014-03-26 19:27:58 +00:00
Damien George 9c817b9465 Merge branch 'master' of github.com:micropython/micropython 2014-03-26 18:56:02 +00:00
Damien George 752ba554cc Merge branch 'gen-close-ret-val' of github.com:pfalcon/micropython into pfalcon-gen-close-ret-val 2014-03-26 18:46:06 +00:00
Damien George 9e6e935df0 py: Add support for user-defined iterators via __iter__, __next__. 2014-03-26 18:37:06 +00:00
Paul Sokolovsky af1ae30399 objexcept: Add mp_obj_exception_get_value() convenience function.
This gets "value" of exceptions in the sense as it's defined for
StopIteration.value (i.e. args[0] or None).

TODO: This really should be inline function.
2014-03-26 19:29:36 +02:00
Paul Sokolovsky 962b1cd1b1 objgenerator: Implement return with value and .close() method.
Return with value gets converted to StopIteration(value). Implementation
keeps optimizing against creating of possibly unneeded exception objects,
so there're considerable refactoring to implement these features.
2014-03-26 15:40:58 +02:00
Damien George ffb5cfc8d8 py: Removed some unnecessary exception objects.
They still exist in commented-out form in objexcept.c if they are ever
needed.
2014-03-25 14:29:40 +00:00
Damien George caac542b23 Proper support for registering builtin modules in ROM.
Comes with some refactoring of code and renaming of files.  All modules
are now named mod*.[ch].
2014-03-25 14:18:18 +00:00
Damien George 1dfde891e3 Merge pull request #373 from iabdalkader/module_register
Add mp_obj_module_register
2014-03-25 13:21:51 +00:00
mux 89d45248ee Add mp_obj_module_register
* Add function to load static modules.
* Use module_register to pyb module.
2014-03-25 00:25:27 +02:00
Damien George 24ff063e80 py: Remove obsolete declarations; make mp_obj_get_array consistent. 2014-03-24 10:47:13 +00:00
Damien George 3ec0a1a32d py: Add 'object' object. 2014-03-22 21:31:28 +00:00
Damien George eabdf6718a py: Add function to convert long int to float. 2014-03-22 20:54:01 +00:00
Rachel Dowdall 721c55dced Added exception hierarchy except for OSError and UnicodeError (requires arguments). Comment out the errors that aren't needed if memory becomes an issue. 2014-03-22 15:28:16 +00:00
Rachel Dowdall 300c8bd4c2 Added ZeroDivisionError to float division. 2014-03-20 22:40:38 +00:00
Damien George 7f8be59111 py: Allow hashing of functions and tuples. 2014-03-20 19:20:59 +00:00
xbe 9e1e8cd642 Implement str.count and add tests for it.
Also modify mp_get_index to accept:
1. Indices that are or evaluate to a boolean.
2. Slice indices.
Add tests for these two cases.
2014-03-12 22:57:16 -07:00
Damien George 9d68e9ccdd py: Implement integer overflow checking for * and << ops.
If operation will overflow, a multi-precision integer is created.
2014-03-12 15:38:15 +00:00
Damien George 0c36da0b59 Implement ROMable modules. Add math module.
mp_module_obj_t can now be put in ROM.

Configuration of float type is now similar to longint: can now choose
none, float or double as the implementation.

math module has basic math functions.  For STM port, these are not yet
implemented (they are just stub functions).
2014-03-08 15:24:39 +00:00
Damien George 715101580b py: Factor and improve issubclass. 2014-03-03 22:38:13 +00:00
Paul Sokolovsky d86d22e1e7 Add mp_obj_is_subclass_fast() - intended for fast argument checking.
I.e. as replacement of MP_OBJ_IS_TYPE(), which takes into account subclassing.
2014-03-03 11:42:53 +08:00
Damien George d5e81826ec py: Reduce size of mp_obj_fun_native_t struct by packing ints. 2014-02-26 17:47:05 +00:00
Damien George 510477557d py: Take out bitfield entries from their own structure.
Don't need to wrap bitfields in their own struct.  Compiler does the
correct thing without it.
2014-02-26 17:40:52 +00:00
Damien George 1dc76af7bf py: Remove name of var arg from macros with var args. 2014-02-26 16:57:08 +00:00
Damien George 438c88dd2f Add arbitrary precision integer support.
Some functionality is still missing (eg and, or, bit shift), and some
things are buggy (eg subtract).
2014-02-22 19:25:23 +00:00
Paul Sokolovsky ac2e28c654 Support passing positional args as keywords to bytecode functions.
For this, record argument names along with each bytecode function. The code
still includes extensive debug logging support so far.
2014-02-16 18:36:33 +02:00
Damien George 2e482cdb7b py: Implement *vargs support.
Addresses issue #295.
2014-02-16 00:01:29 +00:00
Damien George c5966128c7 Implement proper exception type hierarchy.
Each built-in exception is now a type, with base type BaseException.
C exceptions are created by passing a pointer to the exception type to
make an instance of.  When raising an exception from the VM, an
instance is created automatically if an exception type is raised (as
opposed to an exception instance).

Exception matching (RT_BINARY_OP_EXCEPTION_MATCH) is now proper.

Handling of parse error changed to match new exceptions.

mp_const_type renamed to mp_type_type for consistency.
2014-02-15 16:10:44 +00:00
Damien George a71c83a1d1 Change mp_obj_type_t.name from const char * to qstr.
Ultimately all static strings should be qstr.  This entry in the type
structure is only used for printing error messages (to tell the type of
the bad argument), and printing objects that don't supply a .print method.
2014-02-15 11:34:50 +00:00
Damien George 099a9cb575 Remove mp_obj_new_exception_msg_1_arg and _2_arg. 2014-02-12 23:02:19 +00:00
Paul Sokolovsky ac0134d427 Factor out mp_seq_count_obj() and implement tuple.count(). 2014-02-10 07:13:32 +02:00
Paul Sokolovsky 0cd1dc06e6 Factor out mp_seq_index_obj() function to implement .index() on sequences. 2014-02-10 07:13:32 +02:00
Paul Sokolovsky ee4aaf7cdd Implement tuple addition. 2014-02-08 23:17:51 +02:00
Paul Sokolovsky 1a996c48ac Refactor list comparison code to mp_seq_cmp_objs(). 2014-02-08 22:56:36 +02:00
Paul Sokolovsky 557c9d5264 unix microsocket: Add dummy makefile() method.
Unlike CPython socket, microsocket object already implements stream protocol
(read/write methods), so makefile() just returns object itself. TODO: this
doesn't take care of arguments CPython's makefile() may accept.
2014-02-08 22:00:20 +02:00
Damien George 698ec21e46 Make mp_obj_str_get_data return char* instead of byte*.
Can't decide which is better for string type, char or byte pointer.
Changing to char removes a few casts.  Really need to do proper unicode.
2014-02-08 18:17:23 +00:00
Damien George 64131f3215 Add staticmethod and classmethod to builtin namespace. 2014-02-06 20:31:44 +00:00
Damien George 35e2a4e6bb py: Add built-in super. 2014-02-05 00:51:47 +00:00
Paul Sokolovsky 0473e2790b Fix thinko with how bitfields were added to mp_obj_fun_native_t.
Structure is back to expected 16 bytes.
2014-02-02 17:58:44 +02:00
Paul Sokolovsky 87e85b7dc7 Implement str/bytes rich comparisons. 2014-02-02 08:58:16 +02:00
Paul Sokolovsky 13cfabd1b2 Implement slicing for lists. 2014-02-02 03:32:55 +02:00
Paul Sokolovsky 7364af2d8c Factor out m_seq_get_fast_slice_indexes() fucntions as sequence helper.
Takes slice object and sequence length and computes subsequence indexes
for case of slice step=1.
2014-02-02 02:38:22 +02:00
Paul Sokolovsky 90750029df Implement default function arguments (for Python functions).
TODO: Decide if we really need separate bytecode for creating functions
with default arguments - we would need same for closures, then there're
keywords arguments too. Having all combinations is a small exponential
explosion, likely we need just 2 cases - simplest (no defaults, no kw),
and full - defaults & kw.
2014-02-01 15:38:22 +02:00
Damien George d0691ccaec py: Simplify fastn in VM; reduce size of unique code struct.
We still have FAST_[0,1,2] byte codes, but they now just access the
fastn array (before they had special local variables).  It's now
simpler, a bit faster, and uses a bit less stack space (on STM at least,
which is most important).

The only reason now to keep FAST_[0,1,2] byte codes is for compressed
byte code size.
2014-01-29 20:30:52 +00:00
Paul Sokolovsky 7f11c794a5 mp_obj_new_bytearray_by_ref(): Allow to create array by reference.
This is special feature for FFI.
2014-01-29 00:21:41 +02:00
Paul Sokolovsky 0b7e29c025 Factor out quoted string print function for reuse (mp_str_print_quoted()). 2014-01-28 03:40:06 +02:00
Paul Sokolovsky 2ca84aa01e Add MP_OBJ_IS_INT(), for symmetry with MP_OBJ_IS_STR(). 2014-01-26 02:03:07 +02:00
Damien George b829b5caec Implement mp_parse_node_free; print properly repr(string). 2014-01-25 13:51:19 +00:00
Damien George 7c9c667633 py: Implement iterator support for object that has __getitem__.
Addresses Issue #203.
2014-01-25 00:17:36 +00:00
Paul Sokolovsky 91fb1c9b13 Add basic implementation of bytes type, piggybacking on str.
This reuses as much str implementation as possible, from this we
can make them more separate as needed.
2014-01-24 22:56:26 +02:00
Damien George 5fa93b6755 Second stage of qstr revamp: uPy str object can be qstr or not. 2014-01-22 14:35:10 +00:00
Paul Sokolovsky 33996685df Add len() support for arrays. 2014-01-21 23:59:50 +02:00
Paul Sokolovsky 439542f70c sequence.c: Start to refactor sequence operations for reuse among types. 2014-01-21 00:39:12 +02:00
Paul Sokolovsky dff3f896d7 mp_identity(): Add generic identity function.
Useful as getiter method for objects which are their own iterators, etc.
2014-01-20 18:42:08 +02:00
Paul Sokolovsky bb33cc66fb Properly print MP_OBJ_QSTR objects. 2014-01-20 01:00:28 +02:00