8000 Control-C isn't handled in all cases · Issue #1722 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content
Control-C isn't handled in all cases #1722
Closed
@dhylands

Description

@dhylands

This was raised over here: http://forum.micropython.org/viewtopic.php?p=7566#p7566

A very specific patch that addresses that particular issue (very narrowly) would be something like this (patching unix/file.c):

@@ -71,7 +71,14 @@ STATIC mp_uint_t fdfile_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errc
     check_fd_is_open(o);
     mp_int_t r = read(o->fd, buf, size);
     if (r == -1) {
-        *errcode = errno;
+        if (errno == EINTR && MP_STATE_VM(mp_pending_exception) == MP_STATE_VM(keyboard_interrupt_obj)) {
+            // User pressed Control-C. We return no bytes read so that the
+            // vm will get around to checking mp_pending_exception and raising
+            // a KeyboardInterrupt
+
+            return 0;
+        }
+        *errcode = errno; 
         return MP_STREAM_ERROR;
     }
     return r;

I think that every system call needs something like this in order to have Control-C throw a KeyboardInterrupt rather than some other error (OSError 4 in the above example).

The if check probably best belongs in a macro or function (to improve readability and/or to minimize code size increase), but I figured the issue probably warranted further discussion, so I just posted the patch above rather than creating a PR which I knew would need to be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0