Completed
Pull Request — master (#307)
by Björn
36:00 queued 10:58
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
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
    # On windows use ProactorEventLoop which support pipes and is backed by the
33
    # more powerful IOCP facility
34 5
    # NOTE: we override in the stdio case, because it doesn't work.
35
    loop_cls = asyncio.ProactorEventLoop
36
37
    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...
38
    from ctypes import windll, byref, wintypes, GetLastError, WinError, POINTER
0 ignored issues
show
Unused Code introduced by
Unused GetLastError imported from ctypes
Loading history...
39 5
    from ctypes.wintypes import HANDLE, DWORD, BOOL
40
41 5
    LPDWORD = POINTER(DWORD)
42 5
43 5
    PIPE_NOWAIT = wintypes.DWORD(0x00000001)
44
45 5
    ERROR_NO_DATA = 232
46
47
    def pipe_no_wait(pipefd):
48
        SetNamedPipeHandleState = windll.kernel32.SetNamedPipeHandleState
49 5
        SetNamedPipeHandleState.argtypes = [HANDLE, LPDWORD, LPDWORD, LPDWORD]
50
        SetNamedPipeHandleState.restype = BOOL
51
52
        h = msvcrt.get_osfhandle(pipefd)
53
54
        res = windll.kernel32.SetNamedPipeHandleState(h, byref(PIPE_NOWAIT), None, None)
55
        if res == 0:
56 5
            print(WinError())
57
            return False
58 4
        return True
59
60 5
class AsyncioEventLoop(BaseEventLoop, asyncio.Protocol,
61
                       asyncio.SubprocessProtocol):
62 5
63 4
    """`BaseEventLoop` subclass that uses `asyncio` as a backend."""
64 5
65 5
    def connection_made(self, transport):
66
        """Used to signal `asyncio.Protocol` of a successful connection."""
67
        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...
68
        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...
69 5
        if isinstance(transport, asyncio.SubprocessTransport):
70
            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...
71
72
    def connection_lost(self, exc):
73 5
        """Used to signal `asyncio.Protocol` of a lost connection."""
74 5
        self._on_error(exc.args[0] if exc else 'EOF')
75 5
76 5
    def data_received(self, data):
77
        """Used to signal `asyncio.Protocol` of incoming data."""
78 5
        if self._on_data:
79
            self._on_data(data)
80
            return
81
        self._queued_data.append(data)
82 5
83
    def pipe_connection_lost(self, fd, exc):
84
        """Used to signal `asyncio.SubprocessProtocol` of a lost connection."""
85
        self._on_error(exc.args[0] if exc else 'EOF')
86
87
    def pipe_data_received(self, fd, data):
88
        """Used to signal `asyncio.SubprocessProtocol` of incoming data."""
89 5
        if fd == 2:  # stderr fd number
90
            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...
91
        elif self._on_data:
92
            self._on_data(data)
93 1
        else:
94
            self._queued_data.append(data)
95 5
96 5
    def process_exited(self):
97 5
        """Used to signal `asyncio.SubprocessProtocol` when the child exits."""
98
        self._on_error('EOF')
99 5
100 5
    def _init(self):
101
        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...
102 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...
103 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...
104
        self._raw_stdio = False
0 ignored issues
show
Coding Style introduced by
The attribute _raw_stdio 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
106 5
    def _connect_tcp(self, address, port):
107
        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...
108 5
        coroutine = self._loop.create_connection(self._fact, address, port)
109
        self._loop.run_until_complete(coroutine)
110 5
111 5
    def _connect_socket(self, path):
112
        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...
113 5
        if os.name == 'nt':
114 5
            coroutine = self._loop.create_pipe_connection(self._fact, path)
115
        else:
116 5
            coroutine = self._loop.create_unix_connection(self._fact, path)
117 5
        self._loop.run_until_complete(coroutine)
118
119
    def _on_stdin(self):
120
        while True:
121
            data = sys.stdin.buffer.read(1)
122 5
            if len(data) == 0:
123 5
                break
124 5
            self.data_received(data)
125
126 5
    def _stdin_reader(self):
127 5
        sys.stderr.write("a {}\n".format(os.getpid()))
128 5
        while True: #self._active
129
            data = sys.stdin.buffer.read(1)
130
            sys.stderr.write("b {}\n".format(len(data)))
131
            self._loop.call_soon_threadsafe(self.data_received, data)
132
133
    def _connect_stdio(self):
134
        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...
135
        if True or os.name == "nt":
136
            if os.name == "nt":
137
                pipe_no_wait(sys.stdin.fileno())
138
            else:
139
                import fcntl
0 ignored issues
show
Unused Code introduced by
The variable fcntl seems to be unused.
Loading history...
140
                #orig_fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
141
                #fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, orig_fl | os.O_NONBLOCK)
142
            # work around broken windows implementation
143
            #self._loop = asyncio.SelectorEventLoop()
144
            self._raw_stdio = True
0 ignored issues
show
Coding Style introduced by
The attribute _raw_stdio 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...
145
            #coro = self._loop.run_in_executor(None, self._stdin_reader)
146
            #self._loop.create_task(coro)
147
            sys.stderr.write("a {}\n".format(os.getpid()))
148
            import threading
149
            threading.Thread(target=self._stdin_reader).start()
150
        else:
151
            #self._loop = loop_cls()
152
            coroutine = self._loop.connect_read_pipe(self._fact, sys.stdin)
153
            self._loop.run_until_complete(coroutine)
154
            coroutine = self._loop.connect_write_pipe(self._fact, sys.stdout)
155
            self._loop.run_until_complete(coroutine)
156
157
    def _connect_child(self, argv):
158
        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...
159
        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...
160
        self._child_watcher.attach_loop(self._loop)
161
        coroutine = self._loop.subprocess_exec(self._fact, *argv)
162
        self._loop.run_until_complete(coroutine)
163
164
    def _start_reading(self):
165
        pass
166
167
    def _send(self, data):
168
        debug("sent %s %s", repr(data), str(self._raw_stdio))
169
        if self._raw_stdio:
170
            sys.stdout.buffer.write(data)
0 ignored issues
show
Bug introduced by
The Instance of RedirectStream does not seem to have a member named buffer.

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...
171
            sys.stdout.buffer.flush()
0 ignored issues
show
Bug introduced by
The Instance of RedirectStream does not seem to have a member named buffer.

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...
172
        else:
173
            self._transport.write(data)
174
175
    def _run(self):
176
        while self._queued_data:
177
            self._on_data(self._queued_data.popleft())
178
        self._loop.run_forever()
179
180
    def _stop(self):
181
        self._loop.stop()
182
183
    def _close(self):
184
        if self._raw_transport is not None:
185
            self._raw_transport.close()
186
        self._loop.close()
187
188
    def _threadsafe_call(self, fn):
189
        self._loop.call_soon_threadsafe(fn)
190
191
    def _setup_signals(self, signals):
192
        if os.name == 'nt':
193
            # add_signal_handler is not supported in win32
194
            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...
195
            return
196
197
        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...
198
        for signum in self._signals:
199
            self._loop.add_signal_handler(signum, self._on_signal, signum)
200
201
    def _teardown_signals(self):
202
        for signum in self._signals:
203
            self._loop.remove_signal_handler(signum)
204