mirror of
https://github.com/licsber/micropython.git
synced 2024-09-20 09:00:23 +08:00
py: Add stream_tell method, and use for unix and stmhal file tell.
This commit is contained in:
parent
c39093d801
commit
bdd78c31b6
@ -525,6 +525,7 @@ Q(readinto)
|
||||
Q(readline)
|
||||
Q(readlines)
|
||||
Q(seek)
|
||||
Q(tell)
|
||||
Q(FileIO)
|
||||
Q(TextIOWrapper)
|
||||
Q(StringIO)
|
||||
|
@ -26,6 +26,7 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "py/nlr.h"
|
||||
#include "py/objstr.h"
|
||||
@ -400,6 +401,14 @@ STATIC mp_obj_t stream_seek(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
|
||||
|
||||
STATIC mp_obj_t stream_tell(mp_obj_t self) {
|
||||
mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
|
||||
mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
|
||||
const mp_obj_t args[3] = {self, offset, whence};
|
||||
return stream_seek(3, args);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
|
||||
|
@ -35,6 +35,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readline_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readlines_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ(mp_stream_seek_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ(mp_stream_tell_obj);
|
||||
|
||||
// Iterator which uses mp_stream_unbuffered_readline_obj
|
||||
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
|
||||
|
@ -148,12 +148,6 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, mp_uint_t arg,
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t file_obj_tell(mp_obj_t self_in) {
|
||||
pyb_file_obj_t *self = self_in;
|
||||
return mp_obj_new_int_from_uint(f_tell(&self->fp));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_tell_obj, file_obj_tell);
|
||||
|
||||
// Note: encoding is ignored for now; it's also not a valid kwarg for CPython's FileIO,
|
||||
// but by adding it here we can use one single mp_arg_t array for open() and FileIO's constructor
|
||||
STATIC const mp_arg_t file_open_args[] = {
|
||||
@ -231,7 +225,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&file_obj_flush_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_seek), (mp_obj_t)&mp_stream_seek_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&file_obj_tell_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&mp_stream_tell_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&file_obj_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)&file_obj___exit___obj },
|
||||
|
@ -88,10 +88,6 @@ Q(json)
|
||||
Q(heapq)
|
||||
Q(hashlib)
|
||||
|
||||
// for file class
|
||||
Q(seek)
|
||||
Q(tell)
|
||||
|
||||
// for USB configuration
|
||||
Q(usb_mode)
|
||||
Q(mode)
|
||||
|
@ -1,15 +1,19 @@
|
||||
f = open("io/data/file1", "rb")
|
||||
print(f.seek(6))
|
||||
print(f.read(5))
|
||||
print(f.tell())
|
||||
|
||||
print(f.seek(0, 1))
|
||||
print(f.read(4))
|
||||
print(f.tell())
|
||||
|
||||
print(f.seek(-6, 2))
|
||||
print(f.read(20))
|
||||
print(f.tell())
|
||||
|
||||
print(f.seek(0, 0))
|
||||
print(f.read(5))
|
||||
print(f.tell())
|
||||
|
||||
f.close()
|
||||
|
||||
@ -17,4 +21,5 @@ f.close()
|
||||
f = open("io/data/file1", "rt")
|
||||
print(f.seek(6))
|
||||
print(f.read(5))
|
||||
print(f.tell())
|
||||
f.close()
|
||||
|
@ -211,6 +211,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
|
||||
{ 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_seek), (mp_obj_t)&mp_stream_seek_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&mp_stream_tell_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 },
|
||||
|
Loading…
Reference in New Issue
Block a user