diff --git a/Lib/smtplib.py b/Lib/smtplib.py index d37b0e247dd..44a144cd8ca 100644 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -309,7 +309,7 @@ class SMTP: try: port = int(port) except ValueError: - raise socket.error("nonnumeric port") + raise OSError("nonnumeric port") if not port: port = self.default_port if self.debuglevel > 0: @@ -330,7 +330,7 @@ class SMTP: s = s.encode("ascii") try: self.sock.sendall(s) - except socket.error: + except OSError: self.close() raise SMTPServerDisconnected('Server not connected') else: @@ -363,7 +363,7 @@ class SMTP: while 1: try: line = self.file.readline() - except socket.error as e: + except OSError as e: self.close() raise SMTPServerDisconnected("Connection unexpectedly closed: " + str(e)) @@ -920,13 +920,13 @@ class LMTP(SMTP): self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.file = None self.sock.connect(host) - except socket.error as msg: + except OSError: if self.debuglevel > 0: print('connect fail:', host, file=stderr) if self.sock: self.sock.close() self.sock = None - raise socket.error(msg) + raise (code, msg) = self.getreply() if self.debuglevel > 0: print('connect:', msg, file=stderr) diff --git a/Lib/test/mock_socket.py b/Lib/test/mock_socket.py index d09e78c1d51..8ef0ec8c8da 100644 --- a/Lib/test/mock_socket.py +++ b/Lib/test/mock_socket.py @@ -140,12 +140,8 @@ def gethostbyname(name): return "" -class gaierror(Exception): - pass - - -class error(Exception): - pass +gaierror = socket_module.gaierror +error = socket_module.error # Constants diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index befc49e9d3d..6fe2df96df7 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -542,9 +542,9 @@ class NonConnectingTests(unittest.TestCase): def testNonnumericPort(self): # check that non-numeric port raises socket.error - self.assertRaises(mock_socket.error, smtplib.SMTP, + self.assertRaises(OSError, smtplib.SMTP, "localhost", "bogus") - self.assertRaises(mock_socket.error, smtplib.SMTP, + self.assertRaises(OSError, smtplib.SMTP, "localhost:bogus")