Skip to content

Commit

Permalink
Make sure Channel is actually iterable in py3. Pointed out by pylint.…
Browse files Browse the repository at this point in the history
… cleanup queue.

cleanup threadpool.
various small cleanups.
cleanup _fileobjectposix.
  • Loading branch information
jamadden committed Mar 11, 2016
1 parent 913b18b commit bdd1a4e
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .landscape.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ ignore-paths:
- gevent/_tblib.py
# likewise
- greentest/six.py
# sadly, this one is complicated
- setup.py
- greentest/getaddrinfo_module.py
ignore-patterns:
# disabled code
Expand Down Expand Up @@ -55,3 +57,5 @@ pyflakes:
- F401
# F811: redefined function; same story
- F811
# F403: wildcard import; same story
- F403
4 changes: 3 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ generated-members=exc_clear
# List of classes names for which member attributes should not be checked
# (useful for classes with attributes dynamically set). This supports can work
# with qualified names.
ignored-classes=SSLContext, SSLSocket, greenlet
# greenlet, Greenlet, parent, dead: all attempts to fix issues in greenlet.py
# only seen on the service, e.g., self.parent.loop: class parent has no loop
ignored-classes=SSLContext, SSLSocket, greenlet, Greenlet, parent, dead

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
Expand Down
2 changes: 2 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- Remove module ``gevent.coros`` which was replaced by ``gevent.lock``
and has been deprecated since 1.0b2.
- The ``ref`` parameter to :func:`gevent.os.fork_and_watch` was being ignored.
- Python 3: :class:`gevent.queue.Channel` is now correctly iterable, instead of
raising a :exc:`TypeError`.

1.1.0 (Mar 5, 2016)
===================
Expand Down
2 changes: 1 addition & 1 deletion gevent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import sys
if sys.platform == 'win32':
# trigger WSAStartup call
import socket # pylint:disable=unused-import
import socket # pylint:disable=unused-import,useless-suppression
del socket

from gevent.hub import get_hub, iwait, wait, PYPY
Expand Down
22 changes: 13 additions & 9 deletions gevent/_fileobjectposix.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class GreenFileDescriptorIO(RawIOBase):
# the type's tp_dealloc slot; prior to Python 3, the object doesn't
# appear to have a __del__ method, even though it functionally does)

_read_event = None
_write_event = None

def __init__(self, fileno, mode='r', closefd=True):
RawIOBase.__init__(self)
self._closed = False
Expand All @@ -33,15 +36,14 @@ def __init__(self, fileno, mode='r', closefd=True):
self._readable = 'r' in mode
self._writable = 'w' in mode
self.hub = get_hub()
io = self.hub.loop.io

io_watcher = self.hub.loop.io
if self._readable:
self._read_event = io(fileno, 1)
else:
self._read_event = None
self._read_event = io_watcher(fileno, 1)

if self._writable:
self._write_event = io(fileno, 2)
else:
self._write_event = None
self._write_event = io_watcher(fileno, 2)

self._seekable = None

def readable(self):
Expand Down Expand Up @@ -231,10 +233,12 @@ def __init__(self, fobj, mode='rb', bufsize=-1, close=True):
bufsize = 1

if mode == 'r':
self.io = BufferedReader(self.fileio, bufsize)
IOFamily = BufferedReader
else:
assert mode == 'w'
self.io = BufferedWriter(self.fileio, bufsize)
IOFamily = BufferedWriter

self.io = IOFamily(self.fileio, bufsize)
#else: # QQQ: not used, not reachable
#
# self.io = BufferedRandom(self.fileio, bufsize)
Expand Down
2 changes: 1 addition & 1 deletion gevent/_semaphore.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _notify_links(self):
return
try:
link(self) # Must use Cython >= 0.23.4 on PyPy else this leaks memory
except:
except: # pylint:disable=bare-except
getcurrent().handle_error((link, self), *sys.exc_info())
if self._dirty:
# We mutated self._links so we need to start over
Expand Down
2 changes: 1 addition & 1 deletion gevent/_ssl3.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def selected_npn_protocol(self):
# 3.5+
def selected_alpn_protocol(self):
self._checkClosed()
if not self._sslobj or not _ssl.HAS_ALPN:
if not self._sslobj or not _ssl.HAS_ALPN: # pylint:disable=no-member
return None
else:
return self._sslobj.selected_alpn_protocol()
Expand Down
2 changes: 1 addition & 1 deletion gevent/_sslgte279.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def selected_npn_protocol(self):
# 2.7.10+
def selected_alpn_protocol(self):
self._checkClosed()
if not self._sslobj or not _ssl.HAS_ALPN:
if not self._sslobj or not _ssl.HAS_ALPN: # pylint:disable=no-member
return None
else:
return self._sslobj.selected_alpn_protocol()
Expand Down
8 changes: 4 additions & 4 deletions gevent/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ def patch_socket(dns=True, aggressive=True):
# However, because gevent.socket.socket.connect is a Python function, the exception raised by it causes
# _socket object to be referenced by the frame, thus causing the next invocation of bind(source_address) to fail.
if dns:
items = socket.__implements__
items = socket.__implements__ # pylint:disable=no-member
else:
items = set(socket.__implements__) - set(socket.__dns__)
items = set(socket.__implements__) - set(socket.__dns__) # pylint:disable=no-member
patch_module('socket', items=items)
if aggressive:
if 'ssl' not in socket.__implements__:
if 'ssl' not in socket.__implements__: # pylint:disable=no-member
remove_item(socket, 'ssl')


Expand All @@ -423,7 +423,7 @@ def patch_dns():
by that method if requested.
"""
from gevent import socket
patch_module('socket', items=socket.__dns__)
patch_module('socket', items=socket.__dns__) # pylint:disable=no-member


def patch_ssl():
Expand Down
8 changes: 7 additions & 1 deletion gevent/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def copy(self):
return type(self)(self.maxsize, self.queue)

def _init(self, maxsize, items=None):
# FIXME: Why is maxsize unused or even passed?
# pylint:disable=unused-argument
if items:
self.queue = collections.deque(items)
else:
Expand Down Expand Up @@ -321,7 +323,7 @@ def _unlock(self):
try:
putter = self.putters.popleft()
self._put(putter.item)
except:
except: # pylint:disable=bare-except
putter.throw(*sys.exc_info())
else:
putter.switch(putter)
Expand Down Expand Up @@ -378,9 +380,11 @@ def _init(self, maxsize, items=None):
self.queue = []

def _put(self, item, heappush=heapq.heappush):
# pylint:disable=arguments-differ
heappush(self.queue, item)

def _get(self, heappop=heapq.heappop):
# pylint:disable=arguments-differ
return heappop(self.queue)


Expand Down Expand Up @@ -593,3 +597,5 @@ def next(self):
if result is StopIteration:
raise result
return result

__next__ = next # py3
6 changes: 4 additions & 2 deletions gevent/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def _decrease_size(self):
self._size -= 1

def _worker(self):
# pylint:disable=too-many-branches
need_decrease = True
try:
while True:
Expand All @@ -205,7 +206,7 @@ def _worker(self):
func, args, kwargs, thread_result = task
try:
value = func(*args, **kwargs)
except:
except: # pylint:disable=bare-except
exc_info = getattr(sys, 'exc_info', None)
if exc_info is None:
return
Expand All @@ -219,7 +220,7 @@ def _worker(self):
del func, args, kwargs, thread_result, task
finally:
if sys is None:
return
return # pylint:disable=lost-exception
task_queue.task_done()
finally:
if need_decrease:
Expand All @@ -230,6 +231,7 @@ def apply_e(self, expected_errors, function, args=None, kwargs=None):
.. deprecated:: 1.1a2
Identical to :meth:`apply`; the ``expected_errors`` argument is ignored.
"""
# pylint:disable=unused-argument
# Deprecated but never documented. In the past, before
# self.apply() allowed all errors to be raised to the caller,
# expected_errors allowed a caller to specify a set of errors
Expand Down
1 change: 1 addition & 0 deletions gevent/timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class _FakeTimer(object):
active = False

def start(self, *args, **kwargs):
# pylint:disable=unused-argument
raise AssertionError("non-expiring timer cannot be started")

def stop(self):
Expand Down
2 changes: 1 addition & 1 deletion gevent/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Use :mod:`gevent.pywsgi`
"""

from gevent.pywsgi import *
from gevent.pywsgi import * # pylint:disable=wildcard-import
import gevent.pywsgi as _pywsgi
__all__ = _pywsgi.__all__
del _pywsgi
6 changes: 4 additions & 2 deletions greentest/patched_tests_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
def make_re(tests):
tests = [x.strip().replace(r'\.', r'\\.').replace('*', '.*?')
for x in tests.split('\n') if x.strip()]
tests = re.compile('^%s$' % '|'.join(tests))
return tests
return re.compile('^%s$' % '|'.join(tests))


no_switch_tests = make_re(no_switch_tests)
Expand All @@ -68,6 +67,9 @@ def get_switch_expected(fullname):
>>> get_switch_expected("test_patched_httplib.BasicTest.test_bad_status_repr")
False
"""
# certain pylint versions mistype the globals as
# str, not re.
# pylint:disable=no-member
if ignore_switch_tests.match(fullname) is not None:
return None
if no_switch_tests.match(fullname) is not None:
Expand Down
6 changes: 6 additions & 0 deletions greentest/test__queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ def test_task_done(self):
channel.task_done()
assert channel.unfinished_tasks == 0, channel.unfinished_tasks

def test_iterable(self):
channel = queue.Channel()
gevent.spawn(channel.put, StopIteration)
r = list(channel)
self.assertEqual(r, [])


class TestNoWait(TestCase):

Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# N813: camelCase imported as lowercase; socketcommon
# N806: variable in function should be lowercase; but sometimes we want constant-looking names, especially for closures
# N812: lowercase imported as non-lowercase; from greenlet import greenlet as RawGreenlet
ignore=E702,E265,E402,E731,E266,E261,W503,E129,N801,N802,N803,N813,N806,N812
# N805: first arg should be self; fails on metaclasses and classmethods; pylint does a better job
ignore=E702,E265,E402,E731,E266,E261,W503,E129,N801,N802,N803,N813,N806,N812,N805
max_line_length=160
exclude=.runtimes,.eggs,.tox,.git,build,2.6,2.7,2.7pypy,3.3,3.5,test_support.py,test_queue.py,patched_tests_setup.py,test_threading_2.py,lock_tests.py,_sslgte279.py,3.4

Expand Down

0 comments on commit bdd1a4e

Please sign in to comment.