Skip to content

Commit a63a476

Browse files
committed
unix/file: Stop assuming that O_RDWR == O_RDONLY | O_WRONLY.
That's not true e.g. on Linux.
1 parent 71206f0 commit a63a476

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

unix/file.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,22 @@ STATIC mp_obj_t fdfile_open(const mp_obj_type_t *type, mp_arg_val_t *args) {
156156
mp_obj_fdfile_t *o = m_new_obj(mp_obj_fdfile_t);
157157
const char *mode_s = mp_obj_str_get_str(args[1].u_obj);
158158

159-
int mode = 0;
159+
int mode_rw = 0, mode_x = 0;
160160
while (*mode_s) {
161161
switch (*mode_s++) {
162-
// Note: these assume O_RDWR = O_RDONLY | O_WRONLY
163162
case 'r':
164-
mode |= O_RDONLY;
163+
mode_rw = O_RDONLY;
165164
break;
166165
case 'w':
167-
mode |= O_WRONLY | O_CREAT | O_TRUNC;
166+
mode_rw = O_WRONLY;
167+
mode_x = O_CREAT | O_TRUNC;
168168
break;
169169
case 'a':
170-
mode |= O_WRONLY | O_CREAT | O_APPEND;
170+
mode_rw = O_WRONLY;
171+
mode_x = O_CREAT | O_APPEND;
171172
break;
172173
case '+':
173-
mode |= O_RDWR;
174+
mode_rw = O_RDWR;
174175
break;
175176
#if MICROPY_PY_IO_FILEIO
176177
// If we don't have io.FileIO, then files are in text mode implicitly
@@ -194,7 +195,7 @@ STATIC mp_obj_t fdfile_open(const mp_obj_type_t *type, mp_arg_val_t *args) {
194195
}
195196

196197
const char *fname = mp_obj_str_get_str(fid);
197-
int fd = open(fname, mode, 0644);
198+
int fd = open(fname, mode_x | mode_rw, 0644);
198199
if (fd == -1) {
199200
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
200201
}

0 commit comments

Comments
 (0)