Skip to content

Commit bdd78c3

Browse files
blmorrisdpgeorge
authored andcommitted
py: Add stream_tell method, and use for unix and stmhal file tell.
1 parent c39093d commit bdd78c3

7 files changed

Lines changed: 18 additions & 11 deletions

File tree

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ Q(readinto)
525525
Q(readline)
526526
Q(readlines)
527527
Q(seek)
528+
Q(tell)
528529
Q(FileIO)
529530
Q(TextIOWrapper)
530531
Q(StringIO)

py/stream.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include <string.h>
29+
#include <unistd.h>
2930

3031
#include "py/nlr.h"
3132
#include "py/objstr.h"
@@ -400,6 +401,14 @@ STATIC mp_obj_t stream_seek(mp_uint_t n_args, const mp_obj_t *args) {
400401
}
401402
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
402403

404+
STATIC mp_obj_t stream_tell(mp_obj_t self) {
405+
mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
406+
mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
407+
const mp_obj_t args[3] = {self, offset, whence};
408+
return stream_seek(3, args);
409+
}
410+
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
411+
403412
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
404413
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
405414
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);

py/stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readline_obj);
3535
MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readlines_obj);
3636
MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
3737
MP_DECLARE_CONST_FUN_OBJ(mp_stream_seek_obj);
38+
MP_DECLARE_CONST_FUN_OBJ(mp_stream_tell_obj);
3839

3940
// Iterator which uses mp_stream_unbuffered_readline_obj
4041
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);

stmhal/file.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,6 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, mp_uint_t arg,
148148
}
149149
}
150150

151-
mp_obj_t file_obj_tell(mp_obj_t self_in) {
152-
pyb_file_obj_t *self = self_in;
153-
return mp_obj_new_int_from_uint(f_tell(&self->fp));
154-
}
155-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_tell_obj, file_obj_tell);
156-
157151
// Note: encoding is ignored for now; it's also not a valid kwarg for CPython's FileIO,
158152
// but by adding it here we can use one single mp_arg_t array for open() and FileIO's constructor
159153
STATIC const mp_arg_t file_open_args[] = {
@@ -231,7 +225,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
231225
{ MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&file_obj_flush_obj },
232226
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
233227
{ MP_OBJ_NEW_QSTR(MP_QSTR_seek), (mp_obj_t)&mp_stream_seek_obj },
234-
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&file_obj_tell_obj },
228+
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&mp_stream_tell_obj },
235229
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&file_obj_close_obj },
236230
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
237231
{ MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&file_obj___exit___obj },

stmhal/qstrdefsport.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ Q(json)
8888
Q(heapq)
8989
Q(hashlib)
9090

91-
// for file class
92-
Q(seek)
93-
Q(tell)
94-
9591
// for USB configuration
9692
Q(usb_mode)
9793
Q(mode)

tests/io/file_seek.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
f = open("io/data/file1", "rb")
22
print(f.seek(6))
33
print(f.read(5))
4+
print(f.tell())
45

56
print(f.seek(0, 1))
67
print(f.read(4))
8+
print(f.tell())
79

810
print(f.seek(-6, 2))
911
print(f.read(20))
12+
print(f.tell())
1013

1114
print(f.seek(0, 0))
1215
print(f.read(5))
16+
print(f.tell())
1317

1418
f.close()
1519

1620
# test text mode
1721
f = open("io/data/file1", "rt")
1822
print(f.seek(6))
1923
print(f.read(5))
24+
print(f.tell())
2025
f.close()

unix/file.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
211211
{ MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj},
212212
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
213213
{ MP_OBJ_NEW_QSTR(MP_QSTR_seek), (mp_obj_t)&mp_stream_seek_obj },
214+
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&mp_stream_tell_obj },
214215
{ MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&fdfile_flush_obj },
215216
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&fdfile_close_obj },
216217
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },

0 commit comments

Comments
 (0)