From eaea0b16b5a2fc31024090a26b93954d019688c2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 8 Mar 2018 13:38:16 +0530 Subject: [PATCH] Fix URL hints not working on macOS Fixes #363 --- kittens/tui/loop.py | 13 +++++++++---- kitty/child-monitor.c | 8 ++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/kittens/tui/loop.py b/kittens/tui/loop.py index 9de577bda..6fc0a94b0 100644 --- a/kittens/tui/loop.py +++ b/kittens/tui/loop.py @@ -16,7 +16,7 @@ from collections import namedtuple from contextlib import closing, contextmanager from functools import partial -from kitty.fast_data_types import parse_input_from_terminal +from kitty.fast_data_types import parse_input_from_terminal, safe_pipe from kitty.icat import screen_size from kitty.key_encoding import ( ALT, CTRL, PRESS, RELEASE, REPEAT, SHIFT, C, D, backspace_key, @@ -144,8 +144,10 @@ class Loop: sanitize_bracketed_paste='[\x03\x04\x0e\x0f\r\x07\x7f\x8d\x8e\x8f\x90\x9b\x9d\x9e\x9f]'): self.input_fd = sys.stdin.fileno() if input_fd is None else input_fd self.output_fd = sys.stdout.fileno() if output_fd is None else output_fd - self.wakeup_read_fd, self.wakeup_write_fd = os.pipe() - self.sel = s = selectors.DefaultSelector() + self.wakeup_read_fd, self.wakeup_write_fd = safe_pipe() + # For some reason on macOS the DefaultSelector fails when input_fd is + # open('/dev/tty') + self.sel = s = selectors.PollSelector() s.register(self.input_fd, selectors.EVENT_READ, self._read_ready) s.register( self.wakeup_read_fd, selectors.EVENT_READ, self._wakeup_ready @@ -169,7 +171,10 @@ class Loop: def _read_ready(self, handler): if not self.read_allowed: return - data = os.read(self.input_fd, io.DEFAULT_BUFFER_SIZE) + try: + data = os.read(self.input_fd, io.DEFAULT_BUFFER_SIZE) + except BlockingIOError: + return if not data: raise EOFError('The input stream is closed') data = self.decoder.decode(data) diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 2d8652a5c..1509a60ec 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -1255,8 +1255,16 @@ PyTypeObject ChildMonitor_Type = { .tp_new = new, }; +static PyObject* +safe_pipe(PyObject *self UNUSED) { + int fds[2] = {0}; + if (!self_pipe(fds)) return PyErr_SetFromErrno(PyExc_OSError); + return Py_BuildValue("ii", fds[0], fds[1]); +} + static PyMethodDef module_methods[] = { METHOD(simple_render_screen, METH_VARARGS) + METHODB(safe_pipe, METH_NOARGS), {NULL} /* Sentinel */ };