Skip to content

Commit fa2edab

Browse files
committed
stmhal: Switch to file.seek() implementation using stream ioctl.
1 parent c7d5500 commit fa2edab

2 files changed

Lines changed: 31 additions & 36 deletions

File tree

stmhal/file.c

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -114,45 +114,38 @@ mp_obj_t file_obj___exit__(mp_uint_t n_args, const mp_obj_t *args) {
114114
}
115115
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(file_obj___exit___obj, 4, 4, file_obj___exit__);
116116

117-
mp_obj_t file_obj_seek(mp_uint_t n_args, const mp_obj_t *args) {
118-
pyb_file_obj_t *self = args[0];
119-
mp_int_t offset = mp_obj_get_int(args[1]);
120-
mp_int_t whence = 0;
121-
if (n_args == 3) {
122-
whence = mp_obj_get_int(args[2]);
123-
}
117+
STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
118+
pyb_file_obj_t *self = o_in;
124119

125-
switch (whence) {
126-
case 0: // SEEK_SET
127-
f_lseek(&self->fp, offset);
128-
break;
129-
130-
case 1: // SEEK_CUR
131-
if (offset != 0) {
132-
goto error;
133-
}
134-
// no-operation
135-
break;
136-
137-
case 2: // SEEK_END
138-
if (offset != 0) {
139-
goto error;
140-
}
141-
f_lseek(&self->fp, f_size(&self->fp));
142-
break;
143-
144-
default:
145-
goto error;
146-
}
120+
if (request == MP_STREAM_SEEK) {
121+
struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
147122

148-
return mp_obj_new_int_from_uint(f_tell(&self->fp));
123+
switch (s->whence) {
124+
case 0: // SEEK_SET
125+
f_lseek(&self->fp, s->offset);
126+
break;
127+
128+
case 1: // SEEK_CUR
129+
if (s->offset != 0) {
130+
*errcode = ENOTSUP;
131+
return MP_STREAM_ERROR;
132+
}
133+
// no-operation
134+
break;
149135

150-
error:
151-
// A bad whence is a ValueError, while offset!=0 is an io.UnsupportedOperation.
152-
// But the latter inherits ValueError (as well as IOError), so we just raise ValueError.
153-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid whence and/or offset"));
136+
case 2: // SEEK_END
137+
f_lseek(&self->fp, f_size(&self->fp) + s->offset);
138+
break;
139+
}
140+
141+
s->offset = f_tell(&self->fp);
142+
return 0;
143+
144+
} else {
145+
*errcode = EINVAL;
146+
return MP_STREAM_ERROR;
147+
}
154148
}
155-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(file_obj_seek_obj, 2, 3, file_obj_seek);
156149

157150
mp_obj_t file_obj_tell(mp_obj_t self_in) {
158151
pyb_file_obj_t *self = self_in;
@@ -236,7 +229,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
236229
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
237230
{ MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&file_obj_flush_obj },
238231
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
239-
{ MP_OBJ_NEW_QSTR(MP_QSTR_seek), (mp_obj_t)&file_obj_seek_obj },
232+
{ MP_OBJ_NEW_QSTR(MP_QSTR_seek), (mp_obj_t)&mp_stream_seek_obj },
240233
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&file_obj_tell_obj },
241234
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&file_obj_close_obj },
242235
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
@@ -249,6 +242,7 @@ STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);
249242
STATIC const mp_stream_p_t fileio_stream_p = {
250243
.read = file_obj_read,
251244
.write = file_obj_write,
245+
.ioctl = file_obj_ioctl,
252246
};
253247

254248
const mp_obj_type_t mp_type_fileio = {

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ typedef int mp_int_t; // must be pointer size
124124
typedef unsigned int mp_uint_t; // must be pointer size
125125
typedef void *machine_ptr_t; // must be of pointer size
126126
typedef const void *machine_const_ptr_t; // must be of pointer size
127+
typedef long mp_off_t;
127128

128129
// We have inlined IRQ functions for efficiency (they are generally
129130
// 1 machine instruction).

0 commit comments

Comments
 (0)