Browse Source

py: Add stream_tell method, and use for unix and stmhal file tell.

pull/1411/merge
blmorris 9 years ago
committed by Damien George
parent
commit
bdd78c31b6
  1. 1
      py/qstrdefs.h
  2. 9
      py/stream.c
  3. 1
      py/stream.h
  4. 8
      stmhal/file.c
  5. 4
      stmhal/qstrdefsport.h
  6. 5
      tests/io/file_seek.py
  7. 1
      unix/file.c

1
py/qstrdefs.h

@ -525,6 +525,7 @@ Q(readinto)
Q(readline) Q(readline)
Q(readlines) Q(readlines)
Q(seek) Q(seek)
Q(tell)
Q(FileIO) Q(FileIO)
Q(TextIOWrapper) Q(TextIOWrapper)
Q(StringIO) Q(StringIO)

9
py/stream.c

@ -26,6 +26,7 @@
*/ */
#include <string.h> #include <string.h>
#include <unistd.h>
#include "py/nlr.h" #include "py/nlr.h"
#include "py/objstr.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); 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_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_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);

1
py/stream.h

@ -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_unbuffered_readlines_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_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_seek_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_stream_tell_obj);
// Iterator which uses mp_stream_unbuffered_readline_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_unbuffered_iter(mp_obj_t self);

8
stmhal/file.c

@ -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, // 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 // 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[] = { 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_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_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_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___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___enter__), (mp_obj_t)&mp_identity_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&file_obj___exit___obj }, { MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&file_obj___exit___obj },

4
stmhal/qstrdefsport.h

@ -88,10 +88,6 @@ Q(json)
Q(heapq) Q(heapq)
Q(hashlib) Q(hashlib)
// for file class
Q(seek)
Q(tell)
// for USB configuration // for USB configuration
Q(usb_mode) Q(usb_mode)
Q(mode) Q(mode)

5
tests/io/file_seek.py

@ -1,15 +1,19 @@
f = open("io/data/file1", "rb") f = open("io/data/file1", "rb")
print(f.seek(6)) print(f.seek(6))
print(f.read(5)) print(f.read(5))
print(f.tell())
print(f.seek(0, 1)) print(f.seek(0, 1))
print(f.read(4)) print(f.read(4))
print(f.tell())
print(f.seek(-6, 2)) print(f.seek(-6, 2))
print(f.read(20)) print(f.read(20))
print(f.tell())
print(f.seek(0, 0)) print(f.seek(0, 0))
print(f.read(5)) print(f.read(5))
print(f.tell())
f.close() f.close()
@ -17,4 +21,5 @@ f.close()
f = open("io/data/file1", "rt") f = open("io/data/file1", "rt")
print(f.seek(6)) print(f.seek(6))
print(f.read(5)) print(f.read(5))
print(f.tell())
f.close() f.close()

1
unix/file.c

@ -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_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_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_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_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_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___enter__), (mp_obj_t)&mp_identity_obj },

Loading…
Cancel
Save