Skip to content

Commit 0429d35

Browse files
committed
2 parents 847a6b3 + dce8876 commit 0429d35

9 files changed

Lines changed: 99 additions & 9 deletions

File tree

py/emitbc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
struct _emit_t {
5151
pass_kind_t pass : 8;
5252
uint last_emit_was_return_value : 8;
53-
byte dummy_data[DUMMY_DATA_SIZE];
5453

5554
int stack_size;
5655

@@ -67,6 +66,8 @@ struct _emit_t {
6766
uint bytecode_offset;
6867
uint bytecode_size;
6968
byte *code_base; // stores both byte code and code info
69+
// Accessed as uint, so must be aligned as such
70+
byte dummy_data[DUMMY_DATA_SIZE];
7071
};
7172

7273
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) {
207208
emit_write_bytecode_byte(emit, b);
208209
emit_align_bytecode_to_machine_word(emit);
209210
mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
211+
// Verify thar c is already uint-aligned
212+
assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
210213
*c = (mp_uint_t)ptr;
211214
}
212215

py/misc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ int m_get_peak_bytes_allocated(void);
8484
// get the number of elements in a fixed-size array
8585
#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
8686

87+
// align ptr to the nearest multiple of "alignment"
88+
#define MP_ALIGN(ptr, alignment) (void*)(((mp_uint_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
89+
8790
/** unichar / UTF-8 *********************************************/
8891

8992
typedef int unichar; // TODO

py/stream.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,15 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
9898
}
9999
}
100100

101-
STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
101+
mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) {
102102
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
103103
if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
104104
// CPython: io.UnsupportedOperation, OSError subclass
105105
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
106106
}
107107

108-
mp_buffer_info_t bufinfo;
109-
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
110-
111108
int error;
112-
mp_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
109+
mp_int_t out_sz = o->type->stream_p->write(self_in, buf, len, &error);
113110
if (out_sz == -1) {
114111
if (is_nonblocking_error(error)) {
115112
// 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) {
125122
}
126123
}
127124

125+
STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
126+
mp_buffer_info_t bufinfo;
127+
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
128+
return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
129+
}
130+
128131
STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
129132
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
130133
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) {
248251
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
249252
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
250253
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
251-
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);
254+
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);

py/stream.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
3232

3333
// Iterator which uses mp_stream_unbuffered_readline_obj
3434
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
35+
36+
mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len);

tests/run-tests

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def rm_f(fname):
2020
if os.path.exists(fname):
2121
os.remove(fname)
2222

23-
def run_tests(pyb, tests):
23+
def run_tests(pyb, tests, args):
2424
test_count = 0
2525
testcase_count = 0
2626
passed_count = 0
@@ -54,9 +54,15 @@ def run_tests(pyb, tests):
5454
# run CPython to work out expected output
5555
try:
5656
output_expected = subprocess.check_output([CPYTHON3, '-B', test_file])
57+
if args.write_exp:
58+
with open(test_file_expected, 'wb') as f:
59+
f.write(output_expected)
5760
except subprocess.CalledProcessError:
5861
output_expected = b'CPYTHON3 CRASH'
5962

63+
if args.write_exp:
64+
continue
65+
6066
# run Micro Python
6167
if pyb is None:
6268
# run on PC
@@ -113,6 +119,7 @@ def main():
113119
cmd_parser = argparse.ArgumentParser(description='Run tests for Micro Python.')
114120
cmd_parser.add_argument('--pyboard', action='store_true', help='run the tests on the pyboard')
115121
cmd_parser.add_argument('-d', '--test-dirs', nargs='*', help='input test directories (if no files given)')
122+
cmd_parser.add_argument('--write-exp', action='store_true', help='save .exp files to run tests w/o CPython')
116123
cmd_parser.add_argument('files', nargs='*', help='input test files')
117124
args = cmd_parser.parse_args()
118125

@@ -139,7 +146,7 @@ def main():
139146
# tests explicitly given
140147
tests = args.files
141148

142-
if not run_tests(pyb, tests):
149+
if not run_tests(pyb, tests, args):
143150
sys.exit(1)
144151

145152
if __name__ == "__main__":

tests/run-tests-exp.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
#
3+
# This is plain shell variant of run-tests script, which uses .exp files
4+
# as generated by run-tests --write-exp. It is useful to run testsuite
5+
# on embedded systems which doesn't have CPython3.
6+
#
7+
8+
RM="rm -f"
9+
MP_PY=micropython
10+
11+
numtests=0
12+
numtestcases=0
13+
numpassed=0
14+
numfailed=0
15+
namefailed=
16+
17+
if [ $# -eq 0 ]
18+
then
19+
tests="basics/*.py micropython/*.py float/*.py import/*.py io/*.py misc/*.py"
20+
else
21+
tests="$@"
22+
fi
23+
24+
for infile in $tests
25+
do
26+
basename=`basename $infile .py`
27+
outfile=${basename}.out
28+
expfile=$infile.exp
29+
30+
$MP_PY $infile > $outfile
31+
numtestcases=$(expr $numtestcases + $(cat $expfile | wc -l))
32+
33+
diff --brief $expfile $outfile > /dev/null
34+
35+
if [ $? -eq 0 ]
36+
then
37+
echo "pass $infile"
38+
$RM $outfile
39+
numpassed=$(expr $numpassed + 1)
40+
else
41+
echo "FAIL $infile"
42+
numfailed=$(expr $numfailed + 1)
43+
namefailed="$namefailed $basename"
44+
fi
45+
46+
numtests=$(expr $numtests + 1)
47+
done
48+
49+
echo "$numtests tests performed ($numtestcases individual testcases)"
50+
echo "$numpassed tests passed"
51+
if [ $numfailed != 0 ]
52+
then
53+
echo "$numfailed tests failed -$namefailed"
54+
exit 1
55+
else
56+
exit 0
57+
fi

unix/file.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ STATIC mp_int_t fdfile_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int
8282
return r;
8383
}
8484

85+
STATIC mp_obj_t fdfile_flush(mp_obj_t self_in) {
86+
#ifndef _WIN32
87+
mp_obj_fdfile_t *self = self_in;
88+
fsync(self->fd);
89+
#else
90+
//TODO
91+
#endif
92+
return mp_const_none;
93+
}
94+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fdfile_flush_obj, fdfile_flush);
95+
8596
STATIC mp_obj_t fdfile_close(mp_obj_t self_in) {
8697
mp_obj_fdfile_t *self = self_in;
8798
close(self->fd);
@@ -166,6 +177,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
166177
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
167178
{ MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj},
168179
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
180+
{ MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&fdfile_flush_obj },
169181
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&fdfile_close_obj },
170182
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
171183
{ MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&fdfile___exit___obj },

unix/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
// options to control how Micro Python is built
2828

2929
#define MICROPY_ALLOC_PATH_MAX (PATH_MAX)
30+
#ifndef MICROPY_EMIT_X64
3031
#define MICROPY_EMIT_X64 (1)
32+
#endif
3133
#define MICROPY_EMIT_THUMB (0)
3234
#define MICROPY_EMIT_INLINE_THUMB (0)
3335
#define MICROPY_ENABLE_GC (1)

unix/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Q(fileno)
3232
Q(makefile)
3333

3434
Q(FileIO)
35+
Q(flush)
3536

3637
Q(_os)
3738
Q(stat)

0 commit comments

Comments
 (0)