Skip to content

Commit

Permalink
Merge pull request #13 from zopefoundation/fix-warnings
Browse files Browse the repository at this point in the history
Fix many (but not all) warnings
  • Loading branch information
mgedmin committed Oct 20, 2018
2 parents 6fe014a + aa08223 commit 1fcd856
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/zope/server/buffers.py
Expand Up @@ -98,11 +98,15 @@ def prune(self):
if not data:
break
nf.write(data)
self.file.close()
self.file = nf

def getfile(self):
return self.file

def close(self):
self.file.close()


class TempfileBasedBuffer(FileBasedBuffer):

Expand Down
31 changes: 18 additions & 13 deletions src/zope/server/ftp/tests/test_ftpserver.py
Expand Up @@ -42,6 +42,11 @@ class TestIntegration(LoopTestMixin,
AsyncoreErrorHookMixin,
unittest.TestCase):

# Avoid DeprecationWarning for assertRaisesRegexp on Python 3 while
# coping with Python 2 not having the Regex spelling variant
assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegex',
unittest.TestCase.assertRaisesRegexp)

task_dispatcher_count = 1

def setUp(self):
Expand Down Expand Up @@ -360,7 +365,7 @@ class mtime(object):

def testMODE(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, 'MODE_UNKNOWN'):
with self.assertRaisesRegex(ftplib.Error, 'MODE_UNKNOWN'):
conn.sendcmd('MODE a b')

conn.sendcmd('MODE s')
Expand All @@ -377,11 +382,11 @@ def testPWD(self):

def testRETR_not_understood(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, "command not understood"):
with self.assertRaisesRegex(ftplib.Error, "command not understood"):
conn.sendcmd('RETR')
def testRETR_no_file(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, "not a file"):
with self.assertRaisesRegex(ftplib.Error, "not a file"):
conn.sendcmd('RETR /DNE')

def testRETR(self):
Expand Down Expand Up @@ -411,33 +416,33 @@ def testRETR_pasv(self):

def testREST(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, 'arguments'):
with self.assertRaisesRegex(ftplib.Error, 'arguments'):
conn.sendcmd("REST a")

def testRMD(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, 'arguments'):
with self.assertRaisesRegex(ftplib.Error, 'arguments'):
conn.sendcmd('RMD')

with self.assertRaisesRegexp(ftplib.Error, 'denied'):
with self.assertRaisesRegex(ftplib.Error, 'denied'):
conn.sendcmd('RMD /foo')

def testRNFR_dne(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, 'No such file'):
with self.assertRaisesRegex(ftplib.Error, 'No such file'):
conn.sendcmd('RNFR /dne')

def testRNTO_bad_state(self):
conn = self.getFTPConnection()

with self.assertRaisesRegexp(ftplib.Error, 'ERR_RENAME'):
with self.assertRaisesRegex(ftplib.Error, 'ERR_RENAME'):
conn.sendcmd('RNTO /dne')

def testRNFR(self):
conn = self.getFTPConnection()

conn.sendcmd('RNFR /existing')
with self.assertRaisesRegexp(ftplib.Error, 'denied'):
with self.assertRaisesRegex(ftplib.Error, 'denied'):
conn.sendcmd('RNTO /existing2')

def testSIZE(self):
Expand All @@ -447,7 +452,7 @@ def testSIZE(self):
# as an int
self.assertEqual(resp, '213 17 Bytes')

with self.assertRaisesRegexp(ftplib.Error, 'No such file'):
with self.assertRaisesRegex(ftplib.Error, 'No such file'):
conn.size('/DNE')

@unittest.expectedFailure
Expand All @@ -457,7 +462,7 @@ def testSIZE_fails(self):

def testSTRU(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, 'Unimplemented'):
with self.assertRaisesRegex(ftplib.Error, 'Unimplemented'):
conn.sendcmd('STRU a b')

conn.sendcmd('STRU f')
Expand All @@ -469,10 +474,10 @@ def testSYST(self):

def testTYPE(self):
conn = self.getFTPConnection()
with self.assertRaisesRegexp(ftplib.Error, 'arguments'):
with self.assertRaisesRegex(ftplib.Error, 'arguments'):
conn.sendcmd('TYPE g')

with self.assertRaisesRegexp(ftplib.Error, 'size must be 8'):
with self.assertRaisesRegex(ftplib.Error, 'size must be 8'):
conn.sendcmd('TYPE l 9 x')

class MockChannel(object):
Expand Down
1 change: 1 addition & 0 deletions src/zope/server/ftp/tests/test_publisher.py
Expand Up @@ -128,6 +128,7 @@ def bind(self, addr):
return

server = NonBinding(Request, 'name', None, 80, start=False)
self.addCleanup(server.close)
self.assertIsNotNone(server.fs_access)

class TestPublisherFileSystemAccess(unittest.TestCase):
Expand Down
2 changes: 2 additions & 0 deletions src/zope/server/http/tests/test_wsgiserver.py
Expand Up @@ -567,6 +567,8 @@ def test_run_paste_loop(self):
class Server(object):
def __init__(self, *args, **kwargs):
pass
def close(self):
pass

class asyncore(object):
looped = False
Expand Down
10 changes: 6 additions & 4 deletions src/zope/server/http/wsgihttpserver.py
Expand Up @@ -16,10 +16,12 @@
import asyncore
import re
import sys
from contextlib import closing

import six
import zope.security.management
from zope.server.http.httpserver import HTTPServer
from zope.server.taskthreads import ThreadedTaskDispatcher
import zope.security.management


def fakeWrite(body):
Expand Down Expand Up @@ -163,6 +165,6 @@ def run_paste(wsgi_app, global_conf, name='zope.server.http',

task_dispatcher = ThreadedTaskDispatcher()
task_dispatcher.setThreadCount(threads)
server = WSGIHTTPServer(wsgi_app, name, host, port,
task_dispatcher=task_dispatcher)
asyncore.loop()
with closing(WSGIHTTPServer(wsgi_app, name, host, port,
task_dispatcher=task_dispatcher)):
asyncore.loop()
18 changes: 11 additions & 7 deletions src/zope/server/serverbase.py
Expand Up @@ -41,16 +41,20 @@ def __init__(self, ip, port, task_dispatcher=None, adj=None, start=1,
asyncore.dispatcher.__init__(self)
self.port = port
self.task_dispatcher = task_dispatcher
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((ip, port))
self.verbose = verbose
self.hit_log = hit_log
self.logger = logging.getLogger(self.__class__.__name__)
self.server_name = self.computeServerName(ip)

if start:
self.accept_connections()
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.set_reuse_addr()
self.bind((ip, port))
self.server_name = self.computeServerName(ip)

if start:
self.accept_connections()
except BaseException:
self.close()
raise

level_mapping = {
'info': logging.INFO,
Expand Down
8 changes: 5 additions & 3 deletions src/zope/server/tests/__init__.py
Expand Up @@ -22,11 +22,13 @@ def setUp(self):
super(LoopTestMixin, self).setUp()
td = self.td = ThreadedTaskDispatcher()
td.setThreadCount(self.task_dispatcher_count)
if len(asyncore.socket_map) != 1:
if len(asyncore.socket_map) != 1: # pragma: no cover
# Let sockets die off.
# TODO tests should be more careful to clear the socket map.
# (tests should be more careful to clear the socket map, and they
# currently do, but let's keep this backstop just in case, to avoid
# confusing failures).
gc.collect()
asyncore.poll(0.1) # pragma: no cover
asyncore.poll(0.1)
self.orig_map_size = len(asyncore.socket_map)

self.server = self._makeServer()
Expand Down
5 changes: 4 additions & 1 deletion src/zope/server/tests/test_buffers.py
Expand Up @@ -14,7 +14,9 @@ def _getFUT(self):
return buffers.StringIOBasedBuffer

def _makeOne(self):
return self._getFUT()()
buf = self._getFUT()()
self.addCleanup(buf.close)
return buf

def test_cannot_skip_empty(self):
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -48,6 +50,7 @@ def test_construct_from_buffer(self):
buf1.append(b'data')

buf2 = self._getFUT()(buf1)
self.addCleanup(buf2.close)

self.assertEqual(buf2.get(), b'data')

Expand Down
13 changes: 12 additions & 1 deletion src/zope/server/tests/test_serverbase.py
Expand Up @@ -37,6 +37,9 @@ def send(self, data):
self.data += data
return len(data)

def close(self):
pass


class NonBindingServerBase(serverbase.ServerBase):

Expand Down Expand Up @@ -68,7 +71,8 @@ class ServerBaseForTest(NonBindingServerBase):
def bind(self, addr):
ip, port = addr
bound.append("Listening on %s:%d" % (ip or '*', port))
ServerBaseForTest('127.0.0.1', 80, start=False, verbose=True)
sb = ServerBaseForTest('127.0.0.1', 80, start=False, verbose=True)
self.addCleanup(sb.close)
self.assertEqual(bound,
['Listening on 127.0.0.1:80'])

Expand All @@ -79,6 +83,7 @@ def test_ServerBase_startup_logging(self):
# not actually try to bind to ports.

sb = NonBindingServerBase('example.com', 80, start=True, verbose=True)
self.addCleanup(sb.close)
self.assertEqual(sb.logs[0],
"zope.server.serverbase started.\n"
" Hostname: example.com\n"
Expand All @@ -91,6 +96,7 @@ def getExtraLogMessage(self):
return '\n\tURL: http://example.com/'

sb = ServerForTest('example.com', 80, start=True, verbose=True)
self.addCleanup(sb.close)
self.assertEqual(sb.logs[0],
"zope.server.serverbase started.\n"
" Hostname: example.com\n"
Expand All @@ -99,12 +105,14 @@ def getExtraLogMessage(self):

def test_computeServerName(self):
sb = NonBindingServerBase('', 80, start=False)
self.addCleanup(sb.close)

self.assertNotEqual(sb.server_name, '')

def test_computeServerName_errors(self):

sb = NonBindingServerBase('', 80, start=False, verbose=True)
self.addCleanup(sb.close)
import socket

class mysocket(object):
Expand All @@ -130,6 +138,7 @@ def gethostbyaddr(self, name):

def test_addTask_no_dispatcher_executes_immediately(self):
sb = NonBindingServerBase('', 80, start=False)
self.addCleanup(sb.close)
self.assertIsNone(sb.task_dispatcher)

class Task(object):
Expand All @@ -148,6 +157,7 @@ def accept(self):
return None

sb = SB('', 80, start=False)
self.addCleanup(sb.close)
self.assertIsNone(sb.handle_accept())

def test_handle_accept_error(self):
Expand All @@ -158,6 +168,7 @@ def accept(self):
raise socket.error()

sb = SB('', 80, start=False)
self.addCleanup(sb.close)
self.assertIsNone(sb.handle_accept())

self.assertEqual(sb.logs,
Expand Down
2 changes: 2 additions & 0 deletions src/zope/server/trigger.py
Expand Up @@ -215,6 +215,8 @@ def __init__(self):
# I've seen on two WinXP Pro SP2 boxes, under
# Pythons 2.3.5 and 2.4.1.
# (Original commit: https://github.com/zopefoundation/ZEO/commit/c4f736a78ca6713fc3dec21f8aa1fa6f144dd82f)
a.close()
w.close()
raise
# (10048, 'Address already in use')
# assert count <= 2 # never triggered in Tim's tests
Expand Down

0 comments on commit 1fcd856

Please sign in to comment.