1
|
|
|
"""Event loop implementation that uses pyuv(libuv-python bindings).""" |
2
|
6 |
|
import sys |
3
|
6 |
|
from collections import deque |
4
|
|
|
|
5
|
6 |
|
import pyuv |
|
|
|
|
6
|
|
|
|
7
|
|
|
from .base import BaseEventLoop |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class UvEventLoop(BaseEventLoop): |
11
|
|
|
|
12
|
|
|
"""`BaseEventLoop` subclass that uses `pvuv` as a backend.""" |
13
|
|
|
|
14
|
|
|
def _init(self): |
15
|
|
|
self._loop = pyuv.Loop() |
|
|
|
|
16
|
|
|
self._async = pyuv.Async(self._loop, self._on_async) |
|
|
|
|
17
|
|
|
self._connection_error = None |
|
|
|
|
18
|
|
|
self._error_stream = None |
|
|
|
|
19
|
|
|
self._callbacks = deque() |
|
|
|
|
20
|
|
|
|
21
|
|
|
def _on_connect(self, stream, error): |
22
|
|
|
self.stop() |
23
|
|
|
if error: |
24
|
|
|
msg = 'Cannot connect to {0}: {1}'.format( |
25
|
|
|
self._connect_address, pyuv.errno.strerror(error)) |
26
|
|
|
self._connection_error = IOError(msg) |
|
|
|
|
27
|
|
|
return |
28
|
|
|
self._read_stream = self._write_stream = stream |
|
|
|
|
29
|
|
|
|
30
|
|
|
def _on_read(self, handle, data, error): |
31
|
|
|
if error or not data: |
32
|
|
|
msg = pyuv.errno.strerror(error) if error else 'EOF' |
33
|
|
|
self._on_error(msg) |
34
|
|
|
return |
35
|
|
|
if handle == self._error_stream: |
36
|
|
|
return |
37
|
|
|
self._on_data(data) |
38
|
|
|
|
39
|
|
|
def _on_write(self, handle, error): |
|
|
|
|
40
|
|
|
if error: |
41
|
|
|
msg = pyuv.errno.strerror(error) |
42
|
|
|
self._on_error(msg) |
43
|
|
|
|
44
|
|
|
def _on_exit(self, handle, exit_status, term_signal): |
|
|
|
|
45
|
|
|
self._on_error('EOF') |
46
|
|
|
|
47
|
|
|
def _disconnected(self, *args): |
|
|
|
|
48
|
|
|
raise IOError('Not connected to Nvim') |
49
|
|
|
|
50
|
|
|
def _connect_tcp(self, address, port): |
51
|
|
|
stream = pyuv.TCP(self._loop) |
52
|
|
|
self._connect_address = '{0}:{1}'.format(address, port) |
|
|
|
|
53
|
|
|
stream.connect((address, port), self._on_connect) |
54
|
|
|
|
55
|
|
|
def _connect_socket(self, path): |
56
|
|
|
stream = pyuv.Pipe(self._loop) |
57
|
|
|
self._connect_address = path |
|
|
|
|
58
|
|
|
stream.connect(path, self._on_connect) |
59
|
|
|
|
60
|
|
|
def _connect_stdio(self): |
61
|
|
|
self._read_stream = pyuv.Pipe(self._loop) |
|
|
|
|
62
|
|
|
self._read_stream.open(sys.stdin.fileno()) |
63
|
|
|
self._write_stream = pyuv.Pipe(self._loop) |
|
|
|
|
64
|
|
|
self._write_stream.open(sys.stdout.fileno()) |
|
|
|
|
65
|
|
|
|
66
|
|
|
def _connect_child(self, argv): |
67
|
|
|
self._write_stream = pyuv.Pipe(self._loop) |
|
|
|
|
68
|
|
|
self._read_stream = pyuv.Pipe(self._loop) |
|
|
|
|
69
|
|
|
self._error_stream = pyuv.Pipe(self._loop) |
|
|
|
|
70
|
|
|
stdin = pyuv.StdIO(self._write_stream, |
71
|
|
|
flags=pyuv.UV_CREATE_PIPE + pyuv.UV_READABLE_PIPE) |
72
|
|
|
stdout = pyuv.StdIO(self._read_stream, |
73
|
|
|
flags=pyuv.UV_CREATE_PIPE + pyuv.UV_WRITABLE_PIPE) |
74
|
|
|
stderr = pyuv.StdIO(self._error_stream, |
75
|
|
|
flags=pyuv.UV_CREATE_PIPE + pyuv.UV_WRITABLE_PIPE) |
76
|
|
|
self._process = pyuv.Process(self._loop) |
|
|
|
|
77
|
|
|
self._process.spawn(file=argv[0], |
78
|
|
|
exit_callback=self._on_exit, |
79
|
|
|
args=argv[1:], |
80
|
|
|
flags=pyuv.UV_PROCESS_WINDOWS_HIDE, |
81
|
|
|
stdio=(stdin, stdout, stderr,)) |
82
|
|
|
self._error_stream.start_read(self._on_read) |
83
|
|
|
|
84
|
|
|
def _start_reading(self): |
85
|
|
|
if self._transport_type in ['tcp', 'socket']: |
86
|
|
|
self._loop.run() |
87
|
|
|
if self._connection_error: |
88
|
|
|
self.run = self.send = self._disconnected |
89
|
|
|
raise self._connection_error |
|
|
|
|
90
|
|
|
self._read_stream.start_read(self._on_read) |
91
|
|
|
|
92
|
|
|
def _send(self, data): |
93
|
|
|
self._write_stream.write(data, self._on_write) |
94
|
|
|
|
95
|
|
|
def _run(self): |
96
|
|
|
self._loop.run(pyuv.UV_RUN_DEFAULT) |
97
|
|
|
|
98
|
|
|
def _stop(self): |
99
|
|
|
self._loop.stop() |
100
|
|
|
|
101
|
|
|
def _threadsafe_call(self, fn): |
102
|
|
|
self._callbacks.append(fn) |
103
|
|
|
self._async.send() |
104
|
|
|
|
105
|
|
|
def _on_async(self, handle): |
|
|
|
|
106
|
|
|
while self._callbacks: |
107
|
|
|
self._callbacks.popleft()() |
108
|
|
|
|
109
|
|
|
def _poll_fd(self, fd, on_readable, on_writable): |
110
|
|
|
poll = pyuv.Poll(self._loop, fd) |
111
|
|
|
events = 0 |
112
|
|
|
if on_readable is not None: |
113
|
|
|
events |= pyuv.UV_READABLE |
114
|
|
|
if on_writable is not None: |
115
|
|
|
events |= pyuv.UV_WRITABLE |
116
|
|
|
def callback(poll_handle, evts, errorno): |
|
|
|
|
117
|
|
|
if evts & pyuv.UV_READABLE: |
118
|
|
|
on_readable() |
119
|
|
|
if evts & pyuv.UV_WRITABLE: |
120
|
|
|
on_writable() |
121
|
|
|
|
122
|
|
|
poll.start(events, callback) |
123
|
|
|
return poll.stop |
124
|
|
|
|
125
|
|
|
def _setup_signals(self, signals): |
126
|
|
|
self._signal_handles = [] |
|
|
|
|
127
|
|
|
|
128
|
|
|
def handler(h, signum): |
|
|
|
|
129
|
|
|
self._on_signal(signum) |
130
|
|
|
|
131
|
|
|
for signum in signals: |
132
|
|
|
handle = pyuv.Signal(self._loop) |
133
|
|
|
handle.start(handler, signum) |
134
|
|
|
self._signal_handles.append(handle) |
135
|
|
|
|
136
|
|
|
def _teardown_signals(self): |
137
|
|
|
for handle in self._signal_handles: |
138
|
|
|
handle.stop() |
139
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.