Commit Graph

144 Commits

Author SHA1 Message Date
Damien George
4c03b3a899 py: Implement builtin reversed() function.
reversed function now implemented, and works for tuple, list, str, bytes
and user objects with __len__ and __getitem__.

Renamed mp_builtin_len to mp_obj_len to make it publically available (eg
for reversed).
2014-08-12 18:33:40 +01:00
Paul Sokolovsky
7133d91773 py: mp_buffer_info_t::buf may be valid, but NULL for empty objects.
This happens for example for zero-size arrays. As .get_buffer() method now
has explicit return value, it's enough to distinguish success vs failure
of getting buffer.
2014-08-10 16:22:48 +03:00
Damien George
bb4c6f35c6 py: Make MP_OBJ_NEW_SMALL_INT cast arg to mp_int_t itself.
Addresses issue #724.
2014-07-31 10:49:14 +01:00
Damien George
ffe911d228 py: Make long ints hashable.
Addresses issue #765.
2014-07-24 14:21:37 +01:00
Damien George
40f3c02682 Rename machine_(u)int_t to mp_(u)int_t.
See discussion in issue #50.
2014-07-03 13:25:24 +01:00
Paul Sokolovsky
caa7334141 stackctrl: Add "mp_" prefix. 2014-07-01 02:14:08 +03:00
Damien George
b3a50f0f3e Merge branch 'master' into unicode
Conflicts:
	py/mpconfig.h
2014-06-28 10:27:15 +01:00
Paul Sokolovsky
8993fb6cf0 py: Add protection against printing too nested or recursive data structures.
With a test which cannot be automatically validated so far.
2014-06-28 02:25:04 +03:00
Paul Sokolovsky
e7f2b4c875 objstrunicode: Revamp len() handling for unicode, and optimize bool(). 2014-06-27 00:04:18 +03:00
Paul Sokolovsky
3b6f7b95eb py: Separate MICROPY_PY_BUILTINS_COMPLEX from MICROPY_PY_BUILTINS_FLOAT.
One thing is wanting to do 1 / 2 and get something else but 0, and quite
another - doing rocket science ;-).
2014-06-20 18:00:23 +03:00
Damien George
fb510b3bf9 Rename bultins config variables to MICROPY_PY_BUILTINS_*.
This renames:
MICROPY_PY_FROZENSET -> MICROPY_PY_BUILTINS_FROZENSET
MICROPY_PY_PROPERTY -> MICROPY_PY_BUILTINS_PROPERTY
MICROPY_PY_SLICE -> MICROPY_PY_BUILTINS_SLICE
MICROPY_ENABLE_FLOAT -> MICROPY_PY_BUILTINS_FLOAT

See issue #35 for discussion.
2014-06-01 13:32:54 +01:00
Damien George
6ac5dced24 py: Rename MP_OBJ_NOT_SUPPORTED to MP_OBJ_NULL.
See issue #608 for justification.
2014-05-21 19:42:43 +01:00
Paul Sokolovsky
ab7bf28489 py: More const usage. 2014-05-17 11:20:10 +03:00
Paul Sokolovsky
7e7940c39d py: Fix __len__ special method result handling.
Having both MP_OBJ_NOT_SUPPORTED and MP_OBJ_NULL is arguably confusing.
2014-05-11 20:51:31 +03:00
Paul Sokolovsky
7aca1cae34 py: Start making good use of mp_const_obj_t. 2014-05-11 02:26:42 +03:00
Paul Sokolovsky
0bc15941c2 py: Make mp_obj_print() handle null object w/o segfault if debug build.
Happens regularly when used for debugging.
2014-05-10 21:26:07 +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
Paul Sokolovsky
f54bcbf099 py, unix: Make "mpconfig.h" be first included, as other headers depend on it.
Specifically, nlr.h does.
2014-05-02 17:48:40 +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
ee01411036 py: Add len(bytes). 2014-04-15 23:10:00 +01:00
Damien George
0e4ba25834 py: Fix SyntaxError exception: don't have a block name, so pass NULL. 2014-04-13 15:01:28 +01:00
Damien George
8a1cab952f py: Fix mp_get_buffer, and use it in more places.
Must use mp_obj_get_type to get the type of an object.  Can't assume
mp_obj_t is castable to mp_obj_base_t.
2014-04-13 12:08:52 +01:00
Paul Sokolovsky
f130ca1f60 py: Make bytes type hashable. 2014-04-13 06:45:12 +03: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
e22d76e73b py: Fix up object equality test.
It regressed a bit after implementing float/complex equality.  Now it
should be improved, and support more equality tests.
2014-04-11 10:52:06 +00:00
Damien George
a9ddd6d9df py: Simplify and improve mp_get_index.
It has (again) a fast path for ints, and a simplified "slow" path for
everything else.

Also simplify the way str indexing is done (now matches tuple and list).
2014-04-11 10:40:38 +00:00
Damien George
b8a053aeb1 py: Implement float and complex == and !=.
Addresses issue #462.
2014-04-11 10:10:37 +01:00
Damien George
686afc5c0a py: Check that sequence has 2 elements for dict iterable constructor. 2014-04-11 09:13:30 +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
ea13f407a3 py: Change nlr_jump to nlr_raise, to aid in debugging.
This does not affect code size or performance when debugging turned off.

To address issue #420.
2014-04-05 18:32:08 +01:00
Paul Sokolovsky
e99841be69 mp_obj_get_int(): Add warning against adding implicit float->int conversion. 2014-04-05 17:46:47 +03:00
Paul Sokolovsky
91cbe6033a py: Allow types to be hashable.
Quite natural to have d[int] = handle_int .
2014-04-05 12:51:58 +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
8270e3853d py: More robust int conversion and overflow checking. 2014-04-03 11:00:54 +00:00
Damien George
e09320adb5 py: Remove implicit conversion from int to float. 2014-04-01 10:56:07 +01:00
Dave Hylands
baf6f14deb Enhance str.format support
This adds support for almost everything (the comma isn't currently
supported).

The "unspecified" type with floats also doesn't behave exactly like
python.

Tested under unix with float and double
Spot tested on stmhal
2014-04-01 01:17:33 -07: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
e0f2979aed py: Add equality test for None object. 2014-03-30 23:16:42 +01: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
Paul Sokolovsky
0ae518fb9e mp_obj_print_exception(): Assert that traceback has sane number of entries. 2014-03-30 04:16:24 +03:00
Damien George
0aa5d51cf1 py: Support mpz -op- float, mpz -op- complex, and complex -op- mpz. 2014-03-29 17:28:20 +00: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
Damien George
24ff063e80 py: Remove obsolete declarations; make mp_obj_get_array consistent. 2014-03-24 10:47:13 +00:00
Damien George
eabdf6718a py: Add function to convert long int to float. 2014-03-22 20:54:01 +00:00
Damien George
7f8be59111 py: Allow hashing of functions and tuples. 2014-03-20 19:20:59 +00:00
xbe
efe3422394 py: Clean up includes.
Remove unnecessary includes. Add includes that improve portability.
2014-03-17 02:43:40 -07:00
xbe
c5d70ba48b Fix issues in str.count implementation.
See pull request #343.
2014-03-13 00:33:07 -07: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
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
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
Paul Sokolovsky
4e836fb4ea mp_obj_equal(): Instead of assert(), throw NotImplementedError.
With a nice traceback, helps debugging much better.
2014-02-11 02:28: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
9aa2a527b5 py: Tidy up BINARY_OPs; negation done by special NOT bytecode.
IS_NOT and NOT_IN are now compiled to IS + NOT and IN + NOT, with a new
special NOT bytecode.
2014-02-01 23:04:09 +00:00
Damien George
09a0c64bce py: Improve __bool__ and __len__ dispatch; add slots for them. 2014-01-30 10:05:33 +00:00
Paul Sokolovsky
c1d9bbc345 Implement __bool__ and __len__ via unary_op virtual method for all types.
__bool__() and __len__() are just the same as __neg__() or __invert__(),
and require efficient dispatching implementation (not requiring search/lookup).
type->unary_op() is just the right choice for this short of adding
standalone virtual method(s) to already big mp_obj_type_t structure.
2014-01-30 04:37:19 +02:00
Damien George
62ad189a65 py: Add compile option to enable/disable source line numbers. 2014-01-29 21:51:51 +00:00
Damien George
b051e7d167 py: Simpler implementation of mp_obj_callable. 2014-01-23 18:13:53 +00:00
Paul Sokolovsky
c3e72a8cc8 mp_obj_is_callable(): Only object types can be callable.
Fixes segfault on callable("string").
2014-01-23 03:37:21 +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
Damien George
e6a4ab4be1 py: Remove implicit conversion of float to int in mp_obj_get_int().
Addresses Issue #199.
2014-01-22 00:21:47 +00:00
Paul Sokolovsky
33996685df Add len() support for arrays. 2014-01-21 23:59:50 +02:00
Damien George
12eaccacda Merge branch 'master' of github.com:micropython/micropython
Conflicts:
	py/objstr.c
	py/py.mk
	py/stream.c
	unix/main.c
	unix/socket.c
2014-01-21 21:54:15 +00:00
Damien George
55baff4c9b Revamp qstrs: they now include length and hash.
Can now have null bytes in strings.  Can define ROM qstrs per port using
qstrdefsport.h
2014-01-21 21:40:13 +00:00
Paul Sokolovsky
e6da0df6d1 mp_obj_get_type_str(): Handle MP_OBJ_QSTR. 2014-01-20 23:01:06 +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
3754c4a040 mp_obj_get_qstr(): Handle MP_OBJ_QSTR. 2014-01-20 18:23:38 +02:00
Paul Sokolovsky
bb33cc66fb Properly print MP_OBJ_QSTR objects. 2014-01-20 01:00:28 +02:00
Damien George
136b149e41 py: Add full traceback to exception printing. 2014-01-19 12:38:49 +00:00
Damien George
632cf5710c Merge branch 'master' of github.com:dpgeorge/micropython 2014-01-18 14:15:48 +00:00
Paul Sokolovsky
d26b379eec int: Add value accessors: mp_obj_int_get() & mp_obj_int_get_checked().
mp_obj_int_get() can be used when just full resolution of C machine_int_t
is required (returns truncated value of long int). mp_obj_int_get_checked()
will throw exception if Python int value not representable in machine_int_t.
2014-01-18 16:12:12 +02:00
Damien George
20006dbba9 Make VM stack grow upwards, and so no reversed args arrays.
Change state layout in VM so the stack starts at state[0] and grows
upwards.  Locals are at the top end of the state and number downwards.
This cleans up a lot of the interface connecting the VM to C: now all
functions that take an array of Micro Python objects are in order (ie no
longer in reverse).

Also clean up C API with keyword arguments (call_n and call_n_kw
replaced with single call method that takes keyword arguments).  And now
make_new takes keyword arguments.

emitnative.c has not yet been changed to comply with the new order of
stack layout.
2014-01-18 14:10:48 +00:00
Paul Sokolovsky
76d982ef34 type->print(): Distinguish str() and repr() variety by passing extra param. 2014-01-15 02:15:38 +02:00
Paul Sokolovsky
ca318bba0d mp_obj_equal(): Compare small and long ints properly.
By dispatching to long int methods.
2014-01-13 22:41:16 +02:00
Paul Sokolovsky
cc57bd2663 mp_obj_equal(): For non-trivial types, call out to type's special method. 2014-01-12 03:33:56 +02:00
Damien George
eae16445d5 py: Implement staticmethod and classmethod (internally).
Still need to make built-ins by these names, and write tests.
2014-01-11 19:22:29 +00:00
Damien George
745ce4c2ad Merge pull request #131 from chipaca/dict_fromkeys
Added dict.fromkeys.
2014-01-10 15:11:56 -08:00
Paul Sokolovsky
d6f27fe317 mp_obj_equal(): Print which types' equality not implemented before assert(). 2014-01-11 00:14:33 +02:00
John R. Lenton
4bee76ebca Added dict.fromkeys. Are we done with dict and #99 yet? I do think we are. 2014-01-10 11:25:03 +00:00
Damien George
6c73ca1e75 py: add variable argument exception constructor function.
Addresses issue #104.
2014-01-08 18:11:23 +00:00
Damien George
38a2da68c2 py: Stuff qstr in object pointer; keys for mp_map_t are now always mp_obj_t. 2014-01-08 17:33:12 +00:00
Damien George
b97669ab94 py: Improve __build_class__. 2014-01-08 11:47:55 +00:00
Damien George
71c5181a8d Convert Python types to proper Python type hierarchy.
Now much more inline with how CPython does types.
2014-01-04 20:21:15 +00:00
Damien George
eb7bfcb286 Split qstr into pools, and put initial pool in ROM.
Qstr's are now split into a linked-list of qstr pools.  This has 2
benefits: the first pool can be in ROM (huge benefit, since we no longer
use RAM for the core qstrs), and subsequent pools use m_new for the next
pool instead of m_renew (thus avoiding a huge single table for all the
qstrs).

Still would be better to use a hash table, but this scheme takes us part
of the way (eventually convert the pools to hash tables).

Also fixed bug with import.

Also improved the way the module code is referenced (not magic number 1
anymore).
2014-01-04 15:57:35 +00:00
Damien George
209d1b1835 py: add int() and float() built-ins, partially implemented. 2014-01-01 17:03:35 +00:00
Damien
d99b05282d Change object representation from 1 big union to individual structs.
A big change.  Micro Python objects are allocated as individual structs
with the first element being a pointer to the type information (which
is itself an object).  This scheme follows CPython.  Much more flexible,
not necessarily slower, uses same heap memory, and can allocate objects
statically.

Also change name prefix, from py_ to mp_ (mp for Micro Python).
2013-12-21 18:17:45 +00:00
Damien
a3dcd9e80c py: add more Python built-in functions. 2013-12-17 21:35:38 +00:00
Damien
660365e14c py: split runtime into map, obj, builtin. 2013-12-17 18:27:24 +00:00