Commit f5da156b authored by Jason Madden's avatar Jason Madden

Fix pylint errors in our tests.

parent d4d19fb0
......@@ -42,6 +42,10 @@
Objects/tupleobject.c: bad argument to internal function``. Reported
in :issue:`1302` by Ulrich Petri.
- Refactored the gevent test runner and test suite to make them more
reusable. In particular, the tests are now run with ``python -m
gevent.tests``. See :issue:`1293`.
1.3.7 (2018-10-12)
==================
......
......@@ -177,8 +177,7 @@ tests on one version of Python during development, begin with the
above instructions to install gevent in a virtual environment and then
run::
(env) $ cd src/greentest
(env) $ python ./testrunner.py
(env) $ python -mgevent.tests
Before submitting a pull request, it's a good idea to run the tests
across all supported versions of Python, and to check the code quality
......@@ -192,8 +191,7 @@ The testrunner accepts a ``--coverage`` argument to enable code
coverage metrics through the `coverage.py`_ package. That would go
something like this::
cd src/greentest
python testrunner.py --coverage
python -m gevent.tests --coverage
coverage combine
coverage html -i
<open htmlcov/index.html>
......
"""
Various tests for synchronization primitives.
"""
# pylint:disable=no-member,abstract-method
import sys
import time
try:
......@@ -131,7 +132,7 @@ class BaseLockTests(BaseTestCase):
def _with(err=None):
with lock:
if err is not None:
raise err
raise err # pylint:disable=raising-bad-type
_with()
# Check the lock is unacquired
Bunch(f, 1).wait_for_finished()
......@@ -153,7 +154,7 @@ class BaseLockTests(BaseTestCase):
self.assertEqual(n, len(threading.enumerate()))
class LockTests(BaseLockTests):
class LockTests(BaseLockTests): # pylint:disable=abstract-method
"""
Tests for non-recursive, weak locks
(which can be acquired and released from different threads).
......@@ -168,7 +169,7 @@ class LockTests(BaseLockTests):
lock.acquire()
phase.append(None)
start_new_thread(f, ())
while len(phase) == 0:
while not phase:
_wait()
_wait()
self.assertEqual(len(phase), 1)
......@@ -436,9 +437,10 @@ class BaseSemaphoreTests(BaseTestCase):
raise NotImplementedError()
def test_constructor(self):
self.assertRaises(ValueError, self.semtype, value = -1)
self.assertRaises(ValueError, self.semtype, value=-1)
# Py3 doesn't have sys.maxint
self.assertRaises(ValueError, self.semtype, value = -getattr(sys, 'maxint', getattr(sys, 'maxsize', None)))
self.assertRaises(ValueError, self.semtype,
value=-getattr(sys, 'maxint', getattr(sys, 'maxsize', None)))
def test_acquire(self):
sem = self.semtype(1)
......@@ -509,7 +511,7 @@ class BaseSemaphoreTests(BaseTestCase):
# There can be a thread switch between acquiring the semaphore and
# appending the result, therefore results will not necessarily be
# ordered.
self.assertEqual(sorted(results), [False] * 7 + [True] * 3 )
self.assertEqual(sorted(results), [False] * 7 + [True] * 3)
def test_default_value(self):
# The default initial value is 1.
......@@ -534,7 +536,7 @@ class BaseSemaphoreTests(BaseTestCase):
with sem:
self.assertFalse(sem.acquire(False))
if err:
raise err
raise err # pylint:disable=raising-bad-type
_with()
self.assertTrue(sem.acquire(False))
sem.release()
......@@ -603,7 +605,7 @@ class BarrierTests(BaseTestCase):
"""
Test that a barrier is passed in lockstep
"""
results = [[],[]]
results = [[], []]
def f():
self.multipass(results, passes)
self.run_threads(f)
......@@ -657,7 +659,6 @@ class BarrierTests(BaseTestCase):
results2.append(True)
except RuntimeError:
self.barrier.abort()
pass
self.run_threads(f)
self.assertEqual(len(results1), 0)
......@@ -713,7 +714,7 @@ class BarrierTests(BaseTestCase):
results2.append(True)
except RuntimeError:
self.barrier.abort()
pass
# Synchronize and reset the barrier. Must synchronize first so
# that everyone has left it when we reset, and after so that no
# one enters it before the reset.
......
......@@ -133,7 +133,7 @@ class Test(unittest.TestCase):
if hasattr(self.stdlib_module, name):
raise AssertionError("'%r' is not an extension, it is found in %r" % (name, self.stdlib_module))
def check_completeness(self):
def check_completeness(self): # pylint:disable=too-many-branches
"""Check that __all__ (or dir()) of the corresponsing stdlib is a subset of __all__ of this module"""
missed = []
for name in self.stdlib_all:
......
......@@ -36,10 +36,10 @@ class Test(greentest.TestCase):
try:
state.append('start')
gevent.sleep(DELAY * 3.0)
except:
except: # pylint:disable=bare-except
state.append('except')
# catching GreenletExit
pass
state.append('finished')
g = gevent.spawn(test)
......@@ -68,7 +68,9 @@ class Test(greentest.TestCase):
def _test_wait_read_invalid_switch(self, sleep):
sock1, sock2 = socket.socketpair()
try:
p = gevent.spawn(util.wrap_errors(AssertionError, socket.wait_read), sock1.fileno())
p = gevent.spawn(util.wrap_errors(AssertionError,
socket.wait_read), # pylint:disable=no-member
sock1.fileno())
gevent.get_hub().loop.run_callback(switch_None, p)
if sleep is not None:
gevent.sleep(sleep)
......
from __future__ import print_function
import gevent.monkey; gevent.monkey.patch_all()
import gevent.monkey
gevent.monkey.patch_all()
import gevent
import os
......
......@@ -6,11 +6,11 @@ import test__core_loop_run # this runs main tests, fails if signal() is not call
assert test__core_loop_run # pyflakes
from gevent.hub import signal as hub_signal
from gevent import signal
from gevent import signal as top_signal # pylint:disable=reimported
assert gevent.signal is hub_signal
assert gevent.signal is signal
assert gevent.signal is top_signal
assert hasattr(gevent.signal, 'signal')
s = signal(2, sys.stderr.write, 'INTERRUPT')
assert isinstance(s, signal)
s = top_signal(2, sys.stderr.write, 'INTERRUPT')
assert isinstance(s, top_signal)
assert isinstance(s, hub_signal)
......@@ -94,7 +94,7 @@ class TestTimerResolution(Test):
# On CI, with *all* backends, sometimes we get timer values of
# 0.02 or higher.
@reraises_flaky_timeout(AssertionError)
def test_resolution(self):
def test_resolution(self): # pylint:disable=too-many-locals
# Make sure that having an active IO watcher
# doesn't badly throw off our timer resolution.
# (This was a specific problem with libuv)
......
......@@ -14,7 +14,7 @@ class TestExec(unittest.TestCase):
def make_exec_test(path, module):
def test(self):
def test(_):
#sys.stderr.write('%s %s\n' % (module, path))
with open(path, 'rb') as f:
src = f.read()
......@@ -24,18 +24,20 @@ def make_exec_test(path, module):
test.__name__ = name
setattr(TestExec, name, test)
def make_all_tests():
for path, module in walk_modules():
ignored = False
for x in NON_APPLICABLE_SUFFIXES:
if module.endswith(x):
ignored = True
break
if ignored:
continue
for path, module in walk_modules():
ignored = False
for x in NON_APPLICABLE_SUFFIXES:
if module.endswith(x):
ignored = True
break
if ignored:
continue
make_exec_test(path, module)
make_exec_test(path, module)
make_all_tests()
if __name__ == '__main__':
main()
......@@ -142,7 +142,7 @@ class Test(greentest.TestCase):
def f():
try:
gevent.sleep(1.5)
except:
except: # pylint:disable=bare-except
gevent.sleep(1)
p1 = GreenletSubclass.spawn(f)
p2 = GreenletSubclass.spawn(f)
......
......@@ -23,7 +23,7 @@ def main():
workers = [gevent.spawn(worker, i) for i in range(3)]
workers.append(done_worker)
for g in gevent.iwait(workers):
for _ in gevent.iwait(workers):
finished += 1
# Simulate doing something that causes greenlets to switch;
# a non-zero timeout is crucial
......
......@@ -160,7 +160,7 @@ class TestGeventLocal(greentest.TestCase):
self.assertEqual(a.initialized, 2)
# The slot overrides dict values
a.__dict__['initialized'] = 42
a.__dict__['initialized'] = 42 # pylint:disable=unsupported-assignment-operation
self.assertEqual(a.initialized, 2)
# Deleting the slot deletes the slot, but not the dict
......
import sys
import unittest
from gevent.testing import TestCase, main
import gevent
from gevent.timeout import Timeout
@unittest.skipUnless(
hasattr(sys, 'gettotalrefcount'),
"Needs debug build"
)
class TestQueue(TestCase):
# pylint:disable=bare-except,no-member
def test(self):
result = ''
......@@ -43,12 +47,10 @@ class TestQueue(TestCase):
result += '%s' % sys.gettotalrefcount()
a, b, c = result.split()
_, b, c = result.split()
assert b == c, 'total refcount mismatch: %s' % result
if not hasattr(sys, 'gettotalrefcount'):
del TestQueue
if __name__ == '__main__':
main()
......@@ -16,7 +16,7 @@ def _inner_lock(lock):
def checkLocks(kind, ignore_none=True):
handlers = logging._handlerList
assert len(handlers) > 0
assert handlers
for weakref in handlers:
# In py26, these are actual handlers, not weakrefs
......
......@@ -11,7 +11,7 @@ pid = None
awaiting_child = []
def handle_sigchld(*args):
def handle_sigchld(*_args):
# Make sure we can do a blocking operation
gevent.sleep()
# Signal completion
......
......@@ -16,12 +16,12 @@ import sys
import signal
import subprocess
def _waitpid(pid):
def _waitpid(p):
try:
_, stat = os.waitpid(pid, 0)
_, stat = os.waitpid(p, 0)
except OSError:
# Interrupted system call
_, stat = os.waitpid(pid, 0)
_, stat = os.waitpid(p, 0)
assert stat == 0, stat
if hasattr(signal, 'SIGCHLD'):
......
......@@ -78,7 +78,7 @@ class TestCoroutinePool(unittest.TestCase):
timer_fired.append(True)
def some_work():
gevent.timer(0, fire_timer)
gevent.timer(0, fire_timer) # pylint:disable=no-member
pool = self.klass(2)
pool.apply(some_work)
......
......@@ -611,10 +611,12 @@ class TestChunkedPost(TestCase):
if env['PATH_INFO'] == '/a':
data = env['wsgi.input'].read(6)
return [data]
elif env['PATH_INFO'] == '/b':
if env['PATH_INFO'] == '/b':
lines = [x for x in iter(lambda: env['wsgi.input'].read(6), b'')]
return lines
elif env['PATH_INFO'] == '/c':
if env['PATH_INFO'] == '/c':
return [x for x in iter(lambda: env['wsgi.input'].read(1), b'')]
def test_014_chunked_post(self):
......
......@@ -23,7 +23,7 @@
are not leaked by the hub.
"""
from __future__ import print_function
from _socket import socket
from _socket import socket as c_socket
import sys
if sys.version_info[0] >= 3:
# Python3 enforces that __weakref__ appears only once,
......@@ -32,9 +32,9 @@ if sys.version_info[0] >= 3:
# (because socket.socket defines __slots__ with __weakref__),
# so import socket.socket before that can happen.
__import__('socket')
Socket = socket
Socket = c_socket
else:
class Socket(socket):
class Socket(c_socket):
"Something we can have a weakref to"
import _socket
......@@ -117,7 +117,7 @@ def run_interaction(run_client):
# strong refs to the socket still around.
try:
sleep(0.1 + SOCKET_TIMEOUT)
except Exception:
except Exception: # pylint:disable=broad-except
pass
return w
......@@ -139,8 +139,9 @@ def run_and_check(run_client):
@greentest.skipOnCI("Often fail with timeouts or force closed connections; not sure why.")
@greentest.skipIf(greentest.RUN_LEAKCHECKS and greentest.PY3,
"Often fail with force closed connections; not sure why. "
@greentest.skipIf(
greentest.RUN_LEAKCHECKS and greentest.PY3,
"Often fail with force closed connections; not sure why. "
)
class Test(greentest.TestCase):
......
......@@ -14,7 +14,7 @@ from gevent.server import StreamServer
class SimpleStreamServer(StreamServer):
def handle(self, client_socket, _address):
def handle(self, client_socket, _address): # pylint:disable=method-hidden
fd = client_socket.makefile()
try:
request_line = fd.readline()
......@@ -487,7 +487,7 @@ class TestSSLGetCertificate(TestCase):
self.init_server()
server_host, server_port, _family = self.get_server_host_port_family()
ssl.get_server_certificate((server_host, server_port))
ssl.get_server_certificate((server_host, server_port)) # pylint:disable=no-member
def test_wrap_socket_and_handle_wrap_failure(self):
......
......@@ -6,7 +6,7 @@ import pkg_resources
try:
cffi_version = pkg_resources.get_distribution('cffi').parsed_version
except Exception:
except Exception: # pylint:disable=broad-except
# No cffi installed. Shouldn't happen to gevent standard tests,
# but maybe some downstream distributor removed it.
cffi_version = None
......@@ -50,7 +50,7 @@ if hasattr(signal, 'SIGALRM'):
except Expected as ex:
assert str(ex) == 'TestSignal', ex
finally:
sig.cancel()
sig.cancel() # pylint:disable=no-member
@greentest.skipIf((greentest.PY3
......
......@@ -320,7 +320,7 @@ class TestTCP(greentest.TestCase):
# Issue 944
# If we have SOCK_CLOEXEC or similar, we shouldn't be passing
# them through to the getaddrinfo call that connect() makes
SOCK_CLOEXEC = socket.SOCK_CLOEXEC
SOCK_CLOEXEC = socket.SOCK_CLOEXEC # pylint:disable=no-member
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM | SOCK_CLOEXEC)
def accept_once():
......@@ -421,7 +421,7 @@ class TestFunctions(greentest.TestCase):
gevent.sleep(10)
with self.assertRaises(gevent.socket.timeout):
gevent.socket.wait(io(), timeout=0.01)
gevent.socket.wait(io(), timeout=0.01) # pylint:disable=no-member
def test_signatures(self):
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
# pylint:disable=broad-except
import gevent
from gevent import monkey
......@@ -83,9 +83,9 @@ def run(function, *args):
return result, delta
def log_call(result, time, function, *args):
def log_call(result, runtime, function, *args):
log(format_call(function, args))
log_fresult(result, time)
log_fresult(result, runtime)
def compare_relaxed(a, b):
......@@ -332,9 +332,11 @@ class TestCase(greentest.TestCase):
# If we're using the ares resolver, allow the real resolver to generate an
# error that the ares resolver actually gets an answer to.
if (RESOLVER_NOT_SYSTEM
and isinstance(real_result, errors)
and not isinstance(gevent_result, errors)):
if (
RESOLVER_NOT_SYSTEM
and isinstance(real_result, errors)
and not isinstance(gevent_result, errors)
):
return
# From 2.7 on, assertEqual does a better job highlighting the results than we would
......@@ -453,7 +455,7 @@ class SanitizedHostsFile(HostsFile):
# the system's etc hosts?
class TestEtcHosts(TestCase):
MAX_HOSTS = os.getenv('GEVENTTEST_MAX_ETC_HOSTS', 10)
MAX_HOSTS = int(os.getenv('GEVENTTEST_MAX_ETC_HOSTS', '10'))
@classmethod
def populate_tests(cls):
......@@ -493,7 +495,7 @@ class TestFamily(TestCase):
cls._result = getattr(socket, 'getaddrinfo')(TestGeventOrg.HOSTNAME, None)
return cls._result
def assert_error(self, error, function, *args):
def assert_error(self, error, function, *args): # pylint:disable=arguments-differ
try:
result = function(*args)
raise AssertionError('%s: Expected to raise %s, instead returned %r' % (function, error, result))
......
#!/usr/bin/python
from gevent import monkey; monkey.patch_all()
import sys
import gevent.testing as greentest
from gevent import monkey
monkey.patch_all()
import unittest
try:
import httplib
except ImportError:
from http import client as httplib
import socket
if not hasattr(socket, 'ssl'):
sys.exit(0)
import gevent.testing as greentest
@unittest.skipUnless(
hasattr(socket, 'ssl'),
"Needs socket.ssl"
)
class AmazonHTTPSTests(greentest.TestCase):
__timeout__ = 30
......@@ -25,7 +30,7 @@ class AmazonHTTPSTests(greentest.TestCase):
def test_str_and_repr(self):
conn = socket.socket()
conn.connect(('sdb.amazonaws.com', 443))
ssl_conn = socket.ssl(conn)
ssl_conn = socket.ssl(conn) # pylint:disable=no-member
assert str(ssl_conn)
assert repr(ssl_conn)
......
......@@ -28,7 +28,7 @@ class TestSSL(test__socket.TestTCP):
self._close_on_teardown(raw_listener)
return listener
def create_connection(self, *args, **kwargs):
def create_connection(self, *args, **kwargs): # pylint:disable=arguments-differ
return ssl.wrap_socket(super(TestSSL, self).create_connection(*args, **kwargs))
# The SSL library can take a long time to buffer the large amount of data we're trying
......
......@@ -222,7 +222,7 @@ class Test(greentest.TestCase):
def test_check_output_keyword_error(self):
try:
subprocess.check_output([sys.executable, '-c', 'import sys; sys.exit(44)'])
except subprocess.CalledProcessError as e:
except subprocess.CalledProcessError as e: # pylint:disable=no-member
self.assertEqual(e.returncode, 44)
else:
raise AssertionError('must fail with CalledProcessError')
......@@ -282,7 +282,6 @@ class Test(greentest.TestCase):
# If the file is in universal_newlines mode, we should always get a str when
# there is no output.
# https://github.com/gevent/gevent/pull/939
kwargs = {'universal_newlines': True}
self.__test_no_output({'universal_newlines': True}, str)
@greentest.skipIf(sys.version_info[:2] < (3, 6), "Need encoding argument")
......@@ -397,11 +396,11 @@ class RunFuncTestCase(greentest.TestCase):
# call() function with sequence argument
cp = self.run_python("import sys; sys.exit(47)")
self.assertEqual(cp.returncode, 47)
with self.assertRaises(subprocess.CalledProcessError):
with self.assertRaises(subprocess.CalledProcessError): # pylint:disable=no-member
cp.check_returncode()
def test_check(self):
with self.assertRaises(subprocess.CalledProcessError) as c:
with self.assertRaises(subprocess.CalledProcessError) as c: # pylint:disable=no-member
self.run_python("import sys; sys.exit(47)", check=True)
self.assertEqual(c.exception.returncode, 47)
......
......@@ -35,7 +35,7 @@ class Test(greentest.TestCase):
thread.start()
try:
thread.join()
except:
except: # pylint:disable=bare-except
# XXX: This can raise LoopExit in some cases.
greentest.reraiseFlakyTestRaceCondition()
......
from gevent import monkey; monkey.patch_all()
import threading
localdata = threading.local()
localdata.x = "hello"
assert localdata.x == 'hello'
......@@ -9,7 +10,7 @@ success = []
def func():
try:
localdata.x
getattr(localdata, 'x')
raise AssertionError('localdata.x must raise AttributeError')
except AttributeError:
pass
......
......@@ -89,7 +89,7 @@ class Test(greentest.TestCase):
except TypeError as ex:
self.assertTrue(greentest.PY3, "Py3 raises a TypeError for non-BaseExceptions")
self.assert_type_err(ex)
except:
except: # pylint:disable=bare-except
self.assertTrue(greentest.PY2, "Old style classes can only be raised on Py2")
t = sys.exc_info()[0]
self.assertEqual(t, OldStyle)
......@@ -103,7 +103,7 @@ class Test(greentest.TestCase):
except TypeError as ex:
self.assertTrue(greentest.PY3, "Py3 raises a TypeError for non-BaseExceptions")
self.assert_type_err(ex)
except:
except: # pylint:disable=bare-except
self.assertTrue(greentest.PY2, "Old style classes can only be raised on Py2")
t = sys.exc_info()[0]
self.assertEqual(t, OldStyle)
......
......@@ -231,6 +231,9 @@ class ThreadTests(unittest.TestCase):
worker_saw_exception = threading.Event()
class Worker(threading.Thread):
id = None
finished = False
def run(self):
self.id = thread.get_ident()
self.finished = False
......@@ -278,7 +281,7 @@ class ThreadTests(unittest.TestCase):
def test_limbo_cleanup(self):
# Issue 7481: Failure to start thread should cleanup the limbo map.
def fail_new_thread(*args):
def fail_new_thread(*_args):
raise thread.error()
_start_new_thread = threading._start_new_thread
threading._start_new_thread = fail_new_thread
......@@ -406,7 +409,7 @@ class ThreadTests(unittest.TestCase):
kwargs={'yet_another': self})
self.thread.start()
def _run(self, other_ref, yet_another):
def _run(self, _other_ref, _yet_another):
if self.should_raise:
raise SystemExit
......@@ -542,23 +545,24 @@ class ThreadJoinOnShutdown(unittest.TestCase):
class ThreadingExceptionTests(unittest.TestCase):
# A RuntimeError should be raised if Thread.start() is called
# multiple times.
# pylint:disable=bad-thread-instantiation
def test_start_thread_again(self):
thread = threading.Thread()
thread.start()
self.assertRaises(RuntimeError, thread.start)
thread_ = threading.Thread()
thread_.start()
self.assertRaises(RuntimeError, thread_.start)
def test_joining_current_thread(self):
current_thread = threading.current_thread()
self.assertRaises(RuntimeError, current_thread.join)
def test_joining_inactive_thread(self):
thread = threading.Thread()
self.assertRaises(RuntimeError, thread.join)
thread_ = threading.Thread()
self.assertRaises(RuntimeError, thread_.join)
def test_daemonize_active_thread(self):
thread = threading.Thread()
thread.start()
self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
thread_ = threading.Thread()
thread_.start()
self.assertRaises(RuntimeError, setattr, thread_, "daemon", True)
class LockTests(lock_tests.LockTests):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment