diff --git a/py/emitbc.c b/py/emitbc.c index ebc2ba500..365ec458a 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -50,7 +50,6 @@ struct _emit_t { pass_kind_t pass : 8; uint last_emit_was_return_value : 8; - byte dummy_data[DUMMY_DATA_SIZE]; int stack_size; @@ -67,6 +66,8 @@ struct _emit_t { uint bytecode_offset; uint bytecode_size; byte *code_base; // stores both byte code and code info + // Accessed as uint, so must be aligned as such + byte dummy_data[DUMMY_DATA_SIZE]; }; STATIC void emit_bc_rot_two(emit_t *emit); @@ -207,6 +208,8 @@ STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) { emit_write_bytecode_byte(emit, b); emit_align_bytecode_to_machine_word(emit); mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t)); + // Verify thar c is already uint-aligned + assert(c == MP_ALIGN(c, sizeof(mp_uint_t))); *c = (mp_uint_t)ptr; } diff --git a/py/misc.h b/py/misc.h index c8ccdaa02..94a3ffaaf 100644 --- a/py/misc.h +++ b/py/misc.h @@ -84,6 +84,9 @@ int m_get_peak_bytes_allocated(void); // get the number of elements in a fixed-size array #define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +// align ptr to the nearest multiple of "alignment" +#define MP_ALIGN(ptr, alignment) (void*)(((mp_uint_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1)) + /** unichar / UTF-8 *********************************************/ typedef int unichar; // TODO diff --git a/py/stream.c b/py/stream.c index 98f62518a..2b4410728 100644 --- a/py/stream.c +++ b/py/stream.c @@ -98,18 +98,15 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) { } } -STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) { +mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) { struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) { // CPython: io.UnsupportedOperation, OSError subclass nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); } - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); - int error; - mp_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error); + mp_int_t out_sz = o->type->stream_p->write(self_in, buf, len, &error); if (out_sz == -1) { if (is_nonblocking_error(error)) { // http://docs.python.org/3/library/io.html#io.RawIOBase.write @@ -125,6 +122,12 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) { } } +STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); + return mp_stream_write(self_in, bufinfo.buf, bufinfo.len); +} + STATIC mp_obj_t stream_readall(mp_obj_t self_in) { struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { @@ -248,4 +251,4 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline); -MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write); +MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method); diff --git a/py/stream.h b/py/stream.h index e52508daa..4cdc1b4dc 100644 --- a/py/stream.h +++ b/py/stream.h @@ -32,3 +32,5 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj); // Iterator which uses mp_stream_unbuffered_readline_obj mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self); + +mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len); diff --git a/tests/run-tests b/tests/run-tests index c6bc4020d..71a94f946 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -20,7 +20,7 @@ def rm_f(fname): if os.path.exists(fname): os.remove(fname) -def run_tests(pyb, tests): +def run_tests(pyb, tests, args): test_count = 0 testcase_count = 0 passed_count = 0 @@ -54,9 +54,15 @@ def run_tests(pyb, tests): # run CPython to work out expected output try: output_expected = subprocess.check_output([CPYTHON3, '-B', test_file]) + if args.write_exp: + with open(test_file_expected, 'wb') as f: + f.write(output_expected) except subprocess.CalledProcessError: output_expected = b'CPYTHON3 CRASH' + if args.write_exp: + continue + # run Micro Python if pyb is None: # run on PC @@ -113,6 +119,7 @@ def main(): cmd_parser = argparse.ArgumentParser(description='Run tests for Micro Python.') cmd_parser.add_argument('--pyboard', action='store_true', help='run the tests on the pyboard') cmd_parser.add_argument('-d', '--test-dirs', nargs='*', help='input test directories (if no files given)') + cmd_parser.add_argument('--write-exp', action='store_true', help='save .exp files to run tests w/o CPython') cmd_parser.add_argument('files', nargs='*', help='input test files') args = cmd_parser.parse_args() @@ -139,7 +146,7 @@ def main(): # tests explicitly given tests = args.files - if not run_tests(pyb, tests): + if not run_tests(pyb, tests, args): sys.exit(1) if __name__ == "__main__": diff --git a/tests/run-tests-exp.sh b/tests/run-tests-exp.sh new file mode 100755 index 000000000..032c42dd3 --- /dev/null +++ b/tests/run-tests-exp.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# +# This is plain shell variant of run-tests script, which uses .exp files +# as generated by run-tests --write-exp. It is useful to run testsuite +# on embedded systems which doesn't have CPython3. +# + +RM="rm -f" +MP_PY=micropython + +numtests=0 +numtestcases=0 +numpassed=0 +numfailed=0 +namefailed= + +if [ $# -eq 0 ] +then + tests="basics/*.py micropython/*.py float/*.py import/*.py io/*.py misc/*.py" +else + tests="$@" +fi + +for infile in $tests +do + basename=`basename $infile .py` + outfile=${basename}.out + expfile=$infile.exp + + $MP_PY $infile > $outfile + numtestcases=$(expr $numtestcases + $(cat $expfile | wc -l)) + + diff --brief $expfile $outfile > /dev/null + + if [ $? -eq 0 ] + then + echo "pass $infile" + $RM $outfile + numpassed=$(expr $numpassed + 1) + else + echo "FAIL $infile" + numfailed=$(expr $numfailed + 1) + namefailed="$namefailed $basename" + fi + + numtests=$(expr $numtests + 1) +done + +echo "$numtests tests performed ($numtestcases individual testcases)" +echo "$numpassed tests passed" +if [ $numfailed != 0 ] +then + echo "$numfailed tests failed -$namefailed" + exit 1 +else + exit 0 +fi diff --git a/unix/file.c b/unix/file.c index 62e3253b7..056a7b6e8 100644 --- a/unix/file.c +++ b/unix/file.c @@ -82,6 +82,17 @@ STATIC mp_int_t fdfile_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int return r; } +STATIC mp_obj_t fdfile_flush(mp_obj_t self_in) { +#ifndef _WIN32 + mp_obj_fdfile_t *self = self_in; + fsync(self->fd); +#else + //TODO +#endif + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(fdfile_flush_obj, fdfile_flush); + STATIC mp_obj_t fdfile_close(mp_obj_t self_in) { mp_obj_fdfile_t *self = self_in; close(self->fd); @@ -166,6 +177,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&fdfile_flush_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&fdfile_close_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&fdfile___exit___obj }, diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index 73435863b..0831e3fd3 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -27,7 +27,9 @@ // options to control how Micro Python is built #define MICROPY_ALLOC_PATH_MAX (PATH_MAX) +#ifndef MICROPY_EMIT_X64 #define MICROPY_EMIT_X64 (1) +#endif #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) #define MICROPY_ENABLE_GC (1) diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h index de0b3d8fa..05c918017 100644 --- a/unix/qstrdefsport.h +++ b/unix/qstrdefsport.h @@ -32,6 +32,7 @@ Q(fileno) Q(makefile) Q(FileIO) +Q(flush) Q(_os) Q(stat)