Skip to content

Commit

Permalink
various module cleanups.
Browse files Browse the repository at this point in the history
clean up socketcommon.
cleanup examples and servers.
cleanup server
cleanup util.
cleanup sslgte279
cleanup greentest.
cleanup corecffi.
cleanup fileobject.
cleanup select.py
cleanup socket2
fix naming.
cleanup socket3
cleanup hub.
cleanup threading.
cleanup lock
cleanup pywsgi.
cleanup _threading
cleanup patched_test_setup.
  • Loading branch information
jamadden committed Mar 11, 2016
1 parent b098197 commit 69a53c6
Show file tree
Hide file tree
Showing 32 changed files with 464 additions and 304 deletions.
8 changes: 7 additions & 1 deletion .landscape.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ python-targets:
- 2
- 3
ignore-paths:
- examples/webchat
- examples/webchat/
- doc/
- build
- dist
Expand All @@ -23,6 +23,10 @@ ignore-paths:
# This file has invalid syntax for Python 3, which is how
# landscape.io runs things
- gevent/_util_py2.py
# This is vendored with minimal changes
- gevent/_tblib.py
# likewise
- greentest/six.py
- greentest/getaddrinfo_module.py
ignore-patterns:
# disabled code
Expand All @@ -47,3 +51,5 @@ pyflakes:
# F821: undefined name; caught better by pylint, where it can be
# controlled for the whole file/per-line
- F821
# F401: unused import; same story
- F401
29 changes: 29 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,32 @@ max-line-length=160
#notes=FIXME,XXX,TODO
# Disable that, we don't want them in the report (???)
notes=

[VARIABLES]

dummy-variables-rgx=_.*

[TYPECHECK]

# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
# gevent: this is helpful for py3/py2 code.
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

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=gevent._corecffi

[DESIGN]
max-attributes=10

[BASIC]
bad-functions=input
1 change: 1 addition & 0 deletions examples/geventsendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[1] http://pypi.python.org/pypi/py-sendfile/
"""
# pylint:disable=import-error
from errno import EAGAIN
from sendfile import sendfile as original_sendfile
from gevent.socket import wait_write
Expand Down
2 changes: 1 addition & 1 deletion examples/portforwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, listener, dest, **kwargs):
StreamServer.__init__(self, listener, **kwargs)
self.dest = dest

def handle(self, source, address):
def handle(self, source, address): # pylint:disable=method-hidden
log('%s:%s accepted', *address[:2])
try:
dest = create_connection(self.dest)
Expand Down
6 changes: 5 additions & 1 deletion examples/psycopg2_pool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
# pylint:disable=import-error,broad-except,bare-except
import sys
import contextlib

Expand Down Expand Up @@ -146,7 +147,7 @@ def create_connection(self):
return self.connect(*self.args, **self.kwargs)


if __name__ == '__main__':
def main():
import time
pool = PostgresConnectionPool("dbname=postgres", maxsize=3)
start = time.time()
Expand All @@ -155,3 +156,6 @@ def create_connection(self):
gevent.wait()
delay = time.time() - start
print('Running "select pg_sleep(1);" 4 times with 3 connections. Should take about 2 seconds: %.2fs' % delay)

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion examples/udp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EchoServer(DatagramServer):

def handle(self, data, address):
def handle(self, data, address): # pylint:disable=method-hidden
print('%s: got %r' % (address[0], data))
self.socket.sendto(('Received %s bytes' % len(data)).encode('utf-8'), address)

Expand Down
1 change: 1 addition & 0 deletions examples/unixsocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


def application(environ, start_response):
assert environ
start_response('200 OK', [])
return []

Expand Down
6 changes: 4 additions & 2 deletions examples/webproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import re
import traceback
from cgi import escape

try:
import urllib2
from urlparse import urlparse
Expand Down Expand Up @@ -74,13 +75,14 @@ def proxy(path, start_response, proxy_url):
response = ex
print('%s: %s %s' % (path, response.code, response.msg))
headers = [(k, v) for (k, v) in response.headers.items() if k not in drop_headers]
scheme, netloc, path, params, query, fragment = urlparse(path)
scheme, netloc, path, _params, _query, _fragment = urlparse(path)
host = (scheme or 'http') + '://' + netloc
except Exception as ex:
except Exception as ex: # pylint:disable=broad-except
sys.stderr.write('error while reading %s:\n' % path)
traceback.print_exc()
tb = traceback.format_exc()
start_response('502 Bad Gateway', [('Content-Type', 'text/html')])
# pylint:disable=deprecated-method
error_str = escape(str(ex) or ex.__class__.__name__ or 'Error')
error_str = '<h1>%s</h1><h2>%s</h2><pre>%s</pre>' % (error_str, escape(path), escape(tb))
return [_as_bytes(error_str)]
Expand Down
2 changes: 1 addition & 1 deletion examples/webpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from gevent import monkey; monkey.patch_all()
from gevent.pywsgi import WSGIServer
import time
import web
import web # pylint:disable=import-error

urls = ("/", "index",
'/long', 'long_polling')
Expand Down
11 changes: 6 additions & 5 deletions gevent/_socket2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# changing the behaviour of the stdlib if we're just imported; OTOH,
# under Python 2.6/2.7, test_urllib2net.py asserts that the class IS
# socket._fileobject (sigh), so we have to work around that.
class _fileobject(_fileobject):
class _fileobject(_fileobject): # pylint:disable=function-redefined

def __enter__(self):
return self
Expand Down Expand Up @@ -74,7 +74,7 @@ def _get_memory(data):
class _closedsocket(object):
__slots__ = []

def _dummy(*args, **kwargs):
def _dummy(*args, **kwargs): # pylint:disable=no-method-argument,unused-argument
raise error(EBADF, 'Bad file descriptor')
# All _delegate_methods must also be initialized here.
send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy
Expand Down Expand Up @@ -132,6 +132,7 @@ def __str__(self):
return '<%s %s>' % (type(self).__name__, self._formatinfo())

def _formatinfo(self):
# pylint:disable=broad-except
try:
fileno = self.fileno()
except Exception as ex:
Expand Down Expand Up @@ -246,7 +247,7 @@ def connect_ex(self, address):
except timeout:
return EAGAIN
except error as ex:
if type(ex) is error:
if type(ex) is error: # pylint:disable=unidiomatic-typecheck
return ex.args[0]
else:
raise # gaierror is not silented by connect_ex
Expand Down Expand Up @@ -399,7 +400,7 @@ def sendall(self, data, flags=0):
# example, bench_sendall.py yields ~264MB/s, while using 1MB yields
# ~653MB/s (matching CPython). 1MB is arbitrary and might be better
# chosen, say, to match a page size?
chunk_size = max(self.getsockopt(SOL_SOCKET, SO_SNDBUF), 1024 * 1024)
chunk_size = max(self.getsockopt(SOL_SOCKET, SO_SNDBUF), 1024 * 1024) # pylint:disable=no-member

data_sent = 0
end = None
Expand Down Expand Up @@ -468,7 +469,7 @@ def shutdown(self, how):
# delegate the functions that we haven't implemented to the real socket object

_s = "def %s(self, *args): return self._sock.%s(*args)\n\n"

_m = None
for _m in set(_socketmethods) - set(locals()):
exec(_s % (_m, _m,))
del _m, _s
Expand Down
32 changes: 18 additions & 14 deletions gevent/_socket3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""
# Our import magic sadly makes this warning useless
# pylint: disable=undefined-variable
# pylint: disable=too-many-statements,too-many-branches
# pylint: disable=too-many-public-methods,unused-argument

import io
import os
Expand All @@ -25,7 +27,7 @@
__dns__ = _socketcommon.__dns__


SocketIO = __socket__.SocketIO
SocketIO = __socket__.SocketIO # pylint:disable=no-member


def _get_memory(data):
Expand Down Expand Up @@ -91,7 +93,7 @@ def __getattr__(self, name):
def type(self):
# See https://github.com/gevent/gevent/pull/399
if self.timeout != 0.0:
return self._sock.type & ~_socket.SOCK_NONBLOCK
return self._sock.type & ~_socket.SOCK_NONBLOCK # pylint:disable=no-member
else:
return self._sock.type

Expand All @@ -106,7 +108,7 @@ def __repr__(self):
"""Wrap __repr__() to reveal the real class name."""
try:
s = _socket.socket.__repr__(self._sock)
except Exception as ex:
except Exception as ex: # pylint:disable=broad-except
# Observed on Windows Py3.3, printing the repr of a socket
# that just sufferred a ConnectionResetError [WinError 10054]:
# "OverflowError: no printf formatter to display the socket descriptor in decimal"
Expand Down Expand Up @@ -220,7 +222,7 @@ def makefile(self, mode="r", buffering=None, *,
if reading and writing:
buffer = io.BufferedRWPair(raw, raw, buffering)
elif reading:
buffer = io.BufferedReader(raw, buffering)
buffer = io.BufferedReader(raw, buffering) # pylint:disable=redefined-variable-type
else:
assert writing
buffer = io.BufferedWriter(raw, buffering)
Expand Down Expand Up @@ -295,7 +297,7 @@ def connect_ex(self, address):
except timeout:
return EAGAIN
except error as ex:
if type(ex) is error:
if type(ex) is error: # pylint:disable=unidiomatic-typecheck
return ex.args[0]
else:
raise # gaierror is not silented by connect_ex
Expand Down Expand Up @@ -426,7 +428,7 @@ def shutdown(self, how):
# because it's not cooperative.
def _sendfile_use_sendfile(self, file, offset=0, count=None):
# This is called directly by tests
raise __socket__._GiveupOnSendfile()
raise __socket__._GiveupOnSendfile() # pylint:disable=no-member

def _sendfile_use_send(self, file, offset=0, count=None):
self._check_sendfile_params(file, offset, count)
Expand Down Expand Up @@ -503,6 +505,7 @@ def sendfile(self, file, offset=0, count=None):

# get/set_inheritable new in 3.4
if hasattr(os, 'get_inheritable') or hasattr(os, 'get_handle_inheritable'):
# pylint:disable=no-member
if os.name == 'nt':
def get_inheritable(self):
return os.get_handle_inheritable(self.fileno())
Expand Down Expand Up @@ -531,7 +534,7 @@ def set_inheritable(self, inheritable):
# Therefore, on these old versions, we must preserve it as an enum; while this
# seems like it could lead to non-green behaviour, code on those versions
# cannot possibly be using SocketType as a class anyway.
SocketType = __socket__.SocketType
SocketType = __socket__.SocketType # pylint:disable=no-member
# Fixup __all__; note that we get exec'd multiple times during unit tests
if 'SocketType' in __implements__:
__implements__.remove('SocketType')
Expand Down Expand Up @@ -591,9 +594,9 @@ def socketpair(family=None, type=SOCK_STREAM, proto=0):


# PyPy needs drop and reuse
def _do_reuse_or_drop(socket, methname):
def _do_reuse_or_drop(sock, methname):
try:
method = getattr(socket, methname)
method = getattr(sock, methname)
except (AttributeError, TypeError):
pass
else:
Expand Down Expand Up @@ -660,7 +663,7 @@ def close(self):
def __del__(self):
try:
self.close()
except:
except: # pylint:disable=bare-except
# close() may fail if __init__ didn't complete
pass

Expand Down Expand Up @@ -695,7 +698,7 @@ def write(self, data):
self._wbuf.append(data)
self._wbuf_len += len(data)
if (self._wbufsize == 0 or (self._wbufsize == 1 and b'\n' in data) or
(self._wbufsize > 1 and self._wbuf_len >= self._wbufsize)):
(self._wbufsize > 1 and self._wbuf_len >= self._wbufsize)):
self.flush()

def writelines(self, list):
Expand All @@ -704,7 +707,7 @@ def writelines(self, list):
lines = filter(None, map(str, list))
self._wbuf_len += sum(map(len, lines))
self._wbuf.extend(lines)
if (self._wbufsize <= 1 or self._wbuf_len >= self._wbufsize):
if self._wbufsize <= 1 or self._wbuf_len >= self._wbufsize:
self.flush()

def read(self, size=-1):
Expand Down Expand Up @@ -775,6 +778,7 @@ def read(self, size=-1):
return buf.getvalue()

def readline(self, size=-1):
# pylint:disable=too-many-return-statements
buf = self._rbuf
buf.seek(0, 2) # seek end
if buf.tell() > 0:
Expand All @@ -786,7 +790,7 @@ def readline(self, size=-1):
self._rbuf.write(buf.read())
return bline
del bline
if size < 0:
if size < 0: # pylint:disable=too-many-nested-blocks
# Read until \n or EOF, whichever comes first
if self._rbufsize <= 1:
# Speed up unbuffered case
Expand Down Expand Up @@ -931,7 +935,7 @@ def close(self):
def __del__(self):
try:
self.close()
except:
except: # pylint:disable=bare-except
# close() may fail if __init__ didn't complete
pass

Expand Down
Loading

0 comments on commit 69a53c6

Please sign in to comment.