diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncore.py | 17 | ||||
-rwxr-xr-x | Lib/smtpd.py | 16 | ||||
-rw-r--r-- | Lib/test/test_asyncore.py | 33 | ||||
-rw-r--r-- | Lib/test/test_ftplib.py | 3 | ||||
-rw-r--r-- | Lib/test/test_poplib.py | 3 | ||||
-rw-r--r-- | Lib/test/test_smtplib.py | 3 | ||||
-rw-r--r-- | Lib/test/test_ssl.py | 3 |
7 files changed, 46 insertions, 32 deletions
diff --git a/Lib/asyncore.py b/Lib/asyncore.py index 8745276..a277bdd 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -352,12 +352,15 @@ class dispatcher: # XXX can return either an address pair or None try: conn, addr = self.socket.accept() - return conn, addr + except TypeError: + return None except socket.error as why: - if why.args[0] == EWOULDBLOCK: - pass + if why.args[0] in (EWOULDBLOCK, ECONNABORTED): + return None else: raise + else: + return conn, addr def send(self, data): try: @@ -506,7 +509,13 @@ class dispatcher: self.log_info('unhandled connect event', 'warning') def handle_accept(self): - self.log_info('unhandled accept event', 'warning') + pair = self.accept() + if pair is not None: + self.handle_accepted(*pair) + + def handle_accepted(self, sock, addr): + sock.close() + self.log_info('unhandled accepted event', 'warning') def handle_close(self): self.log_info('unhandled close event', 'warning') diff --git a/Lib/smtpd.py b/Lib/smtpd.py index 179a1b9..23787fd 100755 --- a/Lib/smtpd.py +++ b/Lib/smtpd.py @@ -421,21 +421,7 @@ class SMTPServer(asyncore.dispatcher): self.__class__.__name__, time.ctime(time.time()), localaddr, remoteaddr), file=DEBUGSTREAM) - def handle_accept(self): - try: - conn, addr = self.accept() - except TypeError: - # sometimes accept() might return None - return - except socket.error as err: - # ECONNABORTED might be thrown - if err.args[0] != errno.ECONNABORTED: - raise - return - else: - # sometimes addr == None instead of (ip, port) - if addr == None: - return + def handle_accepted(self, conn, addr): print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) channel = self.channel_class(self, conn, addr) diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py index ce0a84f..205efb9 100644 --- a/Lib/test/test_asyncore.py +++ b/Lib/test/test_asyncore.py @@ -296,7 +296,6 @@ class DispatcherTests(unittest.TestCase): d.handle_read() d.handle_write() d.handle_connect() - d.handle_accept() finally: sys.stdout = stdout @@ -304,8 +303,7 @@ class DispatcherTests(unittest.TestCase): expected = ['warning: unhandled incoming priority event', 'warning: unhandled read event', 'warning: unhandled write event', - 'warning: unhandled connect event', - 'warning: unhandled accept event'] + 'warning: unhandled connect event'] self.assertEqual(lines, expected) def test_issue_8594(self): @@ -451,6 +449,9 @@ class BaseTestHandler(asyncore.dispatcher): def handle_accept(self): raise Exception("handle_accept not supposed to be called") + def handle_accepted(self): + raise Exception("handle_accepted not supposed to be called") + def handle_connect(self): raise Exception("handle_connect not supposed to be called") @@ -481,8 +482,7 @@ class TCPServer(asyncore.dispatcher): def address(self): return self.socket.getsockname()[:2] - def handle_accept(self): - sock, addr = self.accept() + def handle_accepted(self, sock, addr): self.handler(sock) def handle_error(self): @@ -546,6 +546,29 @@ class BaseTestAPI(unittest.TestCase): client = BaseClient(server.address) self.loop_waiting_for_flag(server) + def test_handle_accepted(self): + # make sure handle_accepted() is called when a client connects + + class TestListener(BaseTestHandler): + + def __init__(self): + BaseTestHandler.__init__(self) + self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + self.bind((HOST, 0)) + self.listen(5) + self.address = self.socket.getsockname()[:2] + + def handle_accept(self): + asyncore.dispatcher.handle_accept(self) + + def handle_accepted(self, sock, addr): + self.flag = True + + server = TestListener() + client = BaseClient(server.address) + self.loop_waiting_for_flag(server) + + def test_handle_read(self): # make sure handle_read is called on data received diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index 8164ede..c9bb06e 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -244,8 +244,7 @@ class DummyFTPServer(asyncore.dispatcher, threading.Thread): self.active = False self.join() - def handle_accept(self): - conn, addr = self.accept() + def handle_accepted(self, conn, addr): self.handler_instance = self.handler(conn) def handle_connect(self): diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py index 2adc849..81af569 100644 --- a/Lib/test/test_poplib.py +++ b/Lib/test/test_poplib.py @@ -144,8 +144,7 @@ class DummyPOP3Server(asyncore.dispatcher, threading.Thread): self.active = False self.join() - def handle_accept(self): - conn, addr = self.accept() + def handle_accepted(self, conn, addr): self.handler_instance = self.handler(conn) def handle_connect(self): diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index 831f2d7..230899b 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -379,8 +379,7 @@ class SimSMTPServer(smtpd.SMTPServer): self._extra_features = [] smtpd.SMTPServer.__init__(self, *args, **kw) - def handle_accept(self): - conn, addr = self.accept() + def handle_accepted(self, conn, addr): self._SMTPchannel = SimSMTPChannel(self._extra_features, self, conn, addr) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 6b79615..4f29a64 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -838,8 +838,7 @@ else: asyncore.dispatcher.__init__(self, sock) self.listen(5) - def handle_accept(self): - sock_obj, addr = self.accept() + def handle_accepted(self, sock_obj, addr): if support.verbose: sys.stdout.write(" server: new connection from %s:%s\n" %addr) self.ConnectionHandler(sock_obj, self.certfile) |