Skip to content

Commit fa55b15

Browse files
aykevljepler
authored andcommitted
extmod/vfs_fat_file: Implement SEEK_CUR for non-zero offset.
CPython doesn't allow SEEK_CUR with non-zero offset for files in text mode, and uPy inherited this behaviour for both text and binary files. It makes sense to provide full support for SEEK_CUR of binary-mode files in uPy, and to do this in a minimal way means also allowing to use SEEK_CUR with non-zero offsets on text-mode files. That seems to be a fair compromise.
1 parent 9ab39eb commit fa55b15

File tree

3 files changed

+4
-10
lines changed

3 files changed

+4
-10
lines changed

extmod/vfs_fat_file.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,7 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
125125
break;
126126

127127
case 1: // SEEK_CUR
128-
if (s->offset != 0) {
129-
*errcode = MP_EOPNOTSUPP;
130-
return MP_STREAM_ERROR;
131-
}
132-
// no-operation
128+
f_lseek(&self->fp, f_tell(&self->fp) + s->offset);
133129
break;
134130

135131
case 2: // SEEK_END

tests/extmod/vfs_fat_fileio1.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ def ioctl(self, op, arg):
9191

9292
f2.seek(0, 1) # SEEK_CUR
9393
print(f2.read(1))
94-
try:
95-
f2.seek(1, 1) # SEEK_END
96-
except OSError as e:
97-
print(e.args[0] == uerrno.EOPNOTSUPP)
94+
f2.seek(2, 1) # SEEK_CUR
95+
print(f2.read(1))
9896

9997
f2.seek(-2, 2) # SEEK_END
10098
print(f2.read(1))

tests/extmod/vfs_fat_fileio1.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ hello!world!
77
12
88
h
99
e
10-
True
10+
o
1111
d
1212
True
1313
[('foo_dir', 16384, 0)]

0 commit comments

Comments
 (0)