8000 ports/unix: implement PEP 475 by dlech · Pull Request #5723 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ports/unix: implement PEP 475 #5723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions extmod/vfs_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/mpthread.h"
#include "extmod/vfs.h"
#include "extmod/vfs_posix.h"
Expand Down Expand Up @@ -290,12 +291,8 @@ STATIC mp_obj_t vfs_posix_stat(mp_obj_t self_in, mp_obj_t path_in) {
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
struct stat sb;
const char *path = vfs_posix_get_path_str(self, path_in);
MP_THREAD_GIL_EXIT();
int ret = stat(path, &sb);
MP_THREAD_GIL_ENTER();
if (ret != 0) {
mp_raise_OSError(errno);
}
int ret;
MP_HAL_RETRY_SYSCALL(ret, stat(path, &sb), mp_raise_OSError(err));
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.st_mode);
t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.st_ino);
Expand Down Expand Up @@ -335,12 +332,8 @@ STATIC mp_obj_t vfs_posix_statvfs(mp_obj_t self_in, mp_obj_t path_in) {
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
STRUCT_STATVFS sb;
const char *path = vfs_posix_get_path_str(self, path_in);
MP_THREAD_GIL_EXIT();
int ret = STATVFS(path, &sb);
MP_THREAD_GIL_ENTER();
if (ret != 0) {
mp_raise_OSError(errno);
}
int ret;
MP_HAL_RETRY_SYSCALL(ret, STATVFS(path, &sb), mp_raise_OSError(err));
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.f_bsize);
t-& 10000 gt;items[1] = MP_OBJ_NEW_SMALL_INT(sb.f_frsize);
Expand Down
58 changes: 20 additions & 38 deletions extmod/vfs_posix_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* THE SOFTWARE.
*/

#include "py/mphal.h"
#include "py/mpthread.h"
#include "py/runtime.h"
#include "py/stream.h"
Expand Down Expand Up @@ -102,12 +103,8 @@ mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_
}

const char *fname = mp_obj_str_get_str(fid);
MP_THREAD_GIL_EXIT();
int fd = open(fname, mode_x | mode_rw, 0644);
MP_THREAD_GIL_ENTER();
if (fd == -1) {
mp_raise_OSError(errno);
}
int fd;
MP_HAL_RETRY_SYSCALL(fd, open(fname, mode_x | mode_rw, 0644), mp_raise_OSError(err));
o->fd = fd;
return MP_OBJ_FROM_PTR(o);
}
Expand Down Expand Up @@ -139,14 +136,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vfs_posix_file___exit___obj, 4, 4, vf
STATIC mp_uint_t vfs_posix_file_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in);
check_fd_is_open(o);
MP_THREAD_GIL_EXIT();
mp_int_t r = read(o->fd, buf, size);
MP_THREAD_GIL_ENTER();
if (r == -1) {
*errcode = errno;
ssize_t r;
MP_HAL_RETRY_SYSCALL(r, read(o->fd, buf, size), {
*errcode = err;
return MP_STREAM_ERROR;
}
return r;
});
return (mp_uint_t)r;
}

STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
Expand All @@ -158,46 +153,33 @@ STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t
return size;
}
#endif
MP_THREAD_GIL_EXIT();
mp_int_t r = write(o->fd, buf, size);
MP_THREAD_GIL_ENTER();
while (r == -1 && errno == EINTR) {
if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
nlr_raise(obj);
}
MP_THREAD_GIL_EXIT();
r = write(o->fd, buf, size);
MP_THREAD_GIL_ENTER();
}
if (r == -1) {
*errcode = errno;
ssize_t r;
MP_HAL_RETRY_SYSCALL(r, write(o->fd, buf, size), {
*errcode = err;
return MP_STREAM_ERROR;
}
return r;
});
return (mp_uint_t)r;
}

STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in);
check_fd_is_open(o);
switch (request) {
case MP_STREAM_FLUSH:
MP_THREAD_GIL_EXIT();
int ret = fsync(o->fd);
MP_THREAD_GIL_ENTER();
if (ret == -1) {
if (errno == EINVAL
case MP_STREAM_FLUSH: {
int ret;
MP_HAL_RETRY_SYSCALL(ret, fsync(o->fd), {
if (err == EINVAL
&& (o->fd == STDIN_FILENO || o->fd == STDOUT_FILENO || o->fd == STDERR_FILENO)) {
// fsync(stdin/stdout/stderr) may fail with EINVAL, but don't propagate that
// error out. Because data is not buffered by us, and stdin/out/err.flush()
// should just be a no-op.
return 0;
}
*errcode = errno;
*errcode = err;
return MP_STREAM_ERROR;
}
});
return 0;
}
case MP_STREAM_SEEK: {
struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)arg;
MP_THREAD_GIL_EXIT();
Expand Down
14 changes: 10 additions & 4 deletions ports/unix/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* THE SOFTWARE.
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -74,6 +75,9 @@ void prompt_read_history(void) {
char c;
int sz = read(fd, &c, 1);
if (sz < 0) {
if (errno == EINTR) {
continue;
}
break;
}
if (sz == 0 || c == '\n') {
Expand Down Expand Up @@ -107,10 +111,12 @@ void prompt_write_history(void) {
for (int i = MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist)) - 1; i >= 0; i--) {
const char *line = MP_STATE_PORT(readline_hist)[i];
if (line != NULL) {
int res;
res = write(fd, line, strlen(line));
res = write(fd, "\n", 1);
(void)res;
while (write(fd, line, strlen(line)) == -1 && errno == EINTR) {
;
}
while (write(fd, "\n", 1) == -1 && errno == EINTR) {
;
}
}
}
close(fd);
Expand Down
6 changes: 2 additions & 4 deletions ports/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4);

STATIC void stderr_print_strn(void *env, const char *str, size_t len) {
(void)env;
MP_THREAD_GIL_EXIT();
ssize_t dummy = write(STDERR_FILENO, str, len);
MP_THREAD_GIL_ENTER();
ssize_t ret;
MP_HAL_RETRY_SYSCALL(ret, write(STDERR_FILENO, str, len), {});
mp_uos_dupterm_tx_strn(str, len);
(void)dummy;
}

const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};
Expand Down
12 changes: 4 additions & 8 deletions ports/unix/modos.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) {
struct stat sb;
const char *path = mp_obj_str_get_str(path_in);

MP_THREAD_GIL_EXIT();
int res = stat(path, &sb);
MP_THREAD_GIL_ENTER();
RAISE_ERRNO(res, errno);
int res;
MP_HAL_RETRY_SYSCALL(res, stat(path, &sb), mp_raise_OSError(err));

mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.st_mode);
Expand Down Expand Up @@ -95,10 +93,8 @@ STATIC mp_obj_t mod_os_statvfs(mp_obj_t path_in) {
STRUCT_STATVFS sb;
const char *path = mp_obj_str_get_str(path_in);

MP_THREAD_GIL_EXIT();
int res = STATVFS(path, &sb);
MP_THREAD_GIL_ENTER();
RAISE_ERRNO(res, errno);
int res;
MP_HAL_RETRY_SYSCALL(res, STATVFS(path, &sb), mp_raise_OSError(err));

mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.f_bsize);
Expand Down
14 changes: 10 additions & 4 deletions ports/unix/modtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
}
RAISE_ERRNO(res, errno);
#else
// TODO: Handle EINTR
MP_THREAD_GIL_EXIT();
sleep(mp_obj_get_int(arg));
MP_THREAD_GIL_ENTER();
int seconds = mp_obj_get_int(arg);
for (;;) {
MP_THREAD_GIL_EXIT();
seconds = sleep(seconds);
MP_THREAD_GIL_ENTER();
if (seconds == 0) {
break;
}
mp_handle_pending(true);
}
#endif
return mp_const_none;
}
Expand Down
6 changes: 2 additions & 4 deletions ports/unix/moduselect.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,8 @@ STATIC int poll_poll_internal(size_t n_args, const mp_obj_t *args) {

self->flags = flags;

MP_THREAD_GIL_EXIT();
int n_ready = poll(self->entries, self->len, timeout);
MP_THREAD_GIL_ENTER();
RAISE_ERRNO(n_ready, errno);
int n_ready;
MP_HAL_RETRY_SYSCALL(n_ready, poll(self->entries, self->len, timeout), mp_raise_OSError(err));
return n_ready;
}

Expand Down
Loading
0