summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/qstrdefs.h1
-rw-r--r--py/stream.c9
-rw-r--r--py/stream.h1
-rw-r--r--stmhal/file.c8
-rw-r--r--stmhal/qstrdefsport.h4
-rw-r--r--tests/io/file_seek.py5
-rw-r--r--unix/file.c1
7 files changed, 18 insertions, 11 deletions
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 0f066e3249..ea107f5f47 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -525,6 +525,7 @@ Q(readinto)
Q(readline)
Q(readlines)
Q(seek)
+Q(tell)
Q(FileIO)
Q(TextIOWrapper)
Q(StringIO)
diff --git a/py/stream.c b/py/stream.c
index 258f916e08..ba672ce2c5 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -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);
diff --git a/py/stream.h b/py/stream.h
index f9c77aac6e..f9ba3387a9 100644
--- a/py/stream.h
+++ b/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_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);
diff --git a/stmhal/file.c b/stmhal/file.c
index d09d0a0a89..d7d8009b3a 100644
--- a/stmhal/file.c
+++ b/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,
// 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 },
diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h
index 410848ae9e..fe6077ec47 100644
--- a/stmhal/qstrdefsport.h
+++ b/stmhal/qstrdefsport.h
@@ -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)
diff --git a/tests/io/file_seek.py b/tests/io/file_seek.py
index 922b169280..d6be662cbf 100644
--- a/tests/io/file_seek.py
+++ b/tests/io/file_seek.py
@@ -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()
diff --git a/unix/file.c b/unix/file.c
index a7886fd3ce..c98212bc28 100644
--- a/unix/file.c
+++ b/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_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 },