Completed
Pull Request — master (#307)
by Björn
30:43 queued 05:45
created

AsyncioEventLoop._setup_signals()   A

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 12
rs 9.6666
1
"""Event loop implementation that uses the `asyncio` standard module.
2
3
The `asyncio` module was added to python standard library on 3.4, and it
4
provides a pure python implementation of an event loop library. It is used
5
as a fallback in case pyuv is not available(on python implementations other
6
than CPython).
7
8
Earlier python versions are supported through the `trollius` package, which
9
is a backport of `asyncio` that works on Python 2.6+.
10
"""
11 5
from __future__ import absolute_import
12
13 5
import logging
14 5
import os
15 5
import sys
16
from collections import deque
17 5
import threading
18
try:
19 5
    # For python 3.4+, use the standard library module
20 3
    import asyncio
21
except (ImportError, SyntaxError):
22 3
    # Fallback to trollius
23
    import trollius as asyncio
0 ignored issues
show
Configuration introduced by
The import trollius could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

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.

Loading history...
24 5
25
from .base import BaseEventLoop
26
27 5
logger = logging.getLogger(__name__)
28 5
debug, info, warn = (logger.debug, logger.info, logger.warning,)
29
30
loop_cls = asyncio.SelectorEventLoop
31
if os.name == 'nt':
32
    from asyncio.windows_utils import PipeHandle
33
34 5
    # On windows use ProactorEventLoop which support pipes and is backed by the
35
    # more powerful IOCP facility
36
    # NOTE: we override in the stdio case, because it doesn't work.
37
    loop_cls = asyncio.ProactorEventLoop
38
39 5
    import msvcrt
0 ignored issues
show
Configuration introduced by
The import msvcrt could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

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.

Loading history...
40
    from ctypes import windll, byref, wintypes, GetLastError, WinError, POINTER
0 ignored issues
show
Unused Code introduced by
Unused GetLastError imported from ctypes
Loading history...
41 5
    from ctypes.wintypes import HANDLE, DWORD, BOOL
42 5
43 5
    LPDWORD = POINTER(DWORD)
44
45 5
    PIPE_NOWAIT = wintypes.DWORD(0x00000001)
46
47
    ERROR_NO_DATA = 232
48
49 5
    def pipe_no_wait(pipefd):
50
        SetNamedPipeHandleState = windll.kernel32.SetNamedPipeHandleState
51
        SetNamedPipeHandleState.argtypes = [HANDLE, LPDWORD, LPDWORD, LPDWORD]
52
        SetNamedPipeHandleState.restype = BOOL
53
54
        h = msvcrt.get_osfhandle(pipefd)
55
56 5
        res = windll.kernel32.SetNamedPipeHandleState(h, byref(PIPE_NOWAIT), None, None)
57
        if res == 0:
58 4
            print(WinError())
59
            return False
60 5
        return True
61
62 5
class AsyncioEventLoop(BaseEventLoop, asyncio.Protocol,
63 4
                       asyncio.SubprocessProtocol):
64 5
65 5
    """`BaseEventLoop` subclass that uses `asyncio` as a backend."""
66
67
    def connection_made(self, transport):
68
        """Used to signal `asyncio.Protocol` of a successful connection."""
69 5
        self._transport = transport
0 ignored issues
show
Coding Style introduced by
The attribute _transport was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
70
        self._raw_transport = transport
0 ignored issues
show
Coding Style introduced by
The attribute _raw_transport was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
71
        if isinstance(transport, asyncio.SubprocessTransport):
72
            self._transport = transport.get_pipe_transport(0)
0 ignored issues
show
Coding Style introduced by
The attribute _transport was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
73 5
74 5
    def connection_lost(self, exc):
75 5
        """Used to signal `asyncio.Protocol` of a lost connection."""
76 5
        self._on_error(exc.args[0] if exc else 'EOF')
77
78 5
    def data_received(self, data):
79
        """Used to signal `asyncio.Protocol` of incoming data."""
80
        if self._on_data:
81
            self._on_data(data)
82 5
            return
83
        self._queued_data.append(data)
84
85
    def pipe_connection_lost(self, fd, exc):
86
        """Used to signal `asyncio.SubprocessProtocol` of a lost connection."""
87
        self._on_error(exc.args[0] if exc else 'EOF')
88
89 5
    def pipe_data_received(self, fd, data):
90
        """Used to signal `asyncio.SubprocessProtocol` of incoming data."""
91
        if fd == 2:  # stderr fd number
92
            self._on_stderr(data)
0 ignored issues
show
Bug introduced by
The Instance of AsyncioEventLoop does not seem to have a member named _on_stderr.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
93 1
        elif self._on_data:
94
            self._on_data(data)
95 5
        else:
96 5
            self._queued_data.append(data)
97 5
98
    def process_exited(self):
99 5
        """Used to signal `asyncio.SubprocessProtocol` when the child exits."""
100 5
        self._on_error('EOF')
101
102 5
    def _init(self):
103 5
        self._loop = loop_cls()
0 ignored issues
show
Coding Style introduced by
The attribute _loop was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
104
        self._queued_data = deque()
0 ignored issues
show
Coding Style introduced by
The attribute _queued_data was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
105 5
        self._fact = lambda: self
0 ignored issues
show
Coding Style introduced by
The attribute _fact was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
106 5
        self._raw_transport = None
0 ignored issues
show
Coding Style introduced by
The attribute _raw_transport was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
107
        self._raw_stdout = False
0 ignored issues
show
Coding Style introduced by
The attribute _raw_stdout was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
108 5
        self._active = False
0 ignored issues
show
Coding Style introduced by
The attribute _active was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
109
110 5
    def _connect_tcp(self, address, port):
111 5
        coroutine = self._loop.create_connection(self._fact, address, port)
112
        self._loop.run_until_complete(coroutine)
113 5
114 5
    def _connect_socket(self, path):
115
        if os.name == 'nt':
116 5
            coroutine = self._loop.create_pipe_connection(self._fact, path)
117 5
        else:
118
            coroutine = self._loop.create_unix_connection(self._fact, path)
119
        self._loop.run_until_complete(coroutine)
120
121
    def _stdin_reader(self):
122 5
        debug("started reader thread")
123 5
        while True: #self._active
124 5
            data = self._stdin.read(1)
125
            debug("reader thread read %d", len(data))
126 5
            self._loop.call_soon_threadsafe(self.data_received, data)
127 5
128 5
    def _connect_stdio(self):
129
        try:
130
            if os.name == 'nt':
131
                pipe = PipeHandle(msvcrt.get_osfhandle(sys.stdin.fileno()))
132
            else:
133
                pipe = sys.stdin
134
            coroutine = self._loop.connect_read_pipe(self._fact, pipe)
135
            self._loop.run_until_complete(coroutine)
136
            debug("native stdin connection successful")
137
        except OSError:
138
            debug("native stdin connection failed, using reader thread")
139
            if os.name == "nt":
140
                pass
141
                #pipe_no_wait(sys.stdin.fileno())
142
            else:
143
                import fcntl
0 ignored issues
show
Unused Code introduced by
The variable fcntl seems to be unused.
Loading history...
144
                #orig_fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
145
                #fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, orig_fl | os.O_NONBLOCK)
146
            #coro = self._loop.run_in_executor(None, self._stdin_reader)
147
            #self._loop.create_task(coro)
148
            self._stdin = sys.stdin.buffer
0 ignored issues
show
Coding Style introduced by
The attribute _stdin was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
149
            self._active = True
0 ignored issues
show
Coding Style introduced by
The attribute _active was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
150
            threading.Thread(target=self._stdin_reader).start()
151
152
153
        try:
154
            if os.name == 'nt':
155
                pipe = PipeHandle(msvcrt.get_osfhandle(sys.stdout.fileno()))
156
            else:
157
                pipe = sys.stdout
158
            coroutine = self._loop.connect_write_pipe(self._fact, pipe)
159
            self._loop.run_until_complete(coroutine)
160
            debug("native stdout connection successful")
161
        except OSError:
162
            debug("native stdout connection failed, using blocking writes")
163
164
            self._stdout = sys.stdout.buffer
0 ignored issues
show
Coding Style introduced by
The attribute _stdout was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
165
            self._raw_stdout = True
0 ignored issues
show
Coding Style introduced by
The attribute _raw_stdout was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
166
167
    def _connect_child(self, argv):
168
        self._child_watcher = asyncio.get_child_watcher()
0 ignored issues
show
Coding Style introduced by
The attribute _child_watcher was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
169
        self._child_watcher.attach_loop(self._loop)
170
        coroutine = self._loop.subprocess_exec(self._fact, *argv)
171
        self._loop.run_until_complete(coroutine)
172
173
    def _start_reading(self):
174
        pass
175
176
    def _send(self, data):
177
        debug("sent %s %s", repr(data), str(self._raw_stdout))
178
        if self._raw_stdout:
179
            self._stdout.write(data)
180
            self._stdout.flush()
181
        else:
182
            self._transport.write(data)
183
184
    def _run(self):
185
        while self._queued_data:
186
            self._on_data(self._queued_data.popleft())
187
        self._loop.run_forever()
188
189
    def _stop(self):
190
        self._loop.stop()
191
192
    def _close(self):
193
        if self._raw_transport is not None:
194
            self._raw_transport.close()
195
        # FIXME: this is racy and stuff
196
        self._active = False
0 ignored issues
show
Coding Style introduced by
The attribute _active was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
197
        self._loop.close()
198
199
    def _threadsafe_call(self, fn):
200
        self._loop.call_soon_threadsafe(fn)
201
202
    def _setup_signals(self, signals):
203
        if os.name == 'nt':
204
            # add_signal_handler is not supported in win32
205
            self._signals = []
0 ignored issues
show
Coding Style introduced by
The attribute _signals was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
206
            return
207
208
        self._signals = list(signals)
0 ignored issues
show
Coding Style introduced by
The attribute _signals was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
209
        for signum in self._signals:
210
            self._loop.add_signal_handler(signum, self._on_signal, signum)
211
212
    def _teardown_signals(self):
213
        for signum in self._signals:
214
            self._loop.remove_signal_handler(signum)
215