问题
Should I not put the return from this method below in finally ? Pylint gives error for this saying:
3: return statement in finally block may swallow exception (lost-exception)
def sendMessage(self, subject, msgContent, files, mailto):
""" Send the email message
Args:
subject(string): subject for the email
msgContent(string): email message Content
files(List): list of files to be attached
mailto(string): email address to be sent to
"""
msg = self.prepareMail(subject, msgContent, files, mailto)
# connect to server and send email
server=smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
server.ehlo()
# use encrypted SSL mode
server.starttls()
# to make starttls work
server.ehlo()
server.login(self.usrname, self.password)
server.set_debuglevel(self.debug)
try:
failed = server.sendmail(self.mailFrom, mailto, msg.as_string())
except Exception as er:
print er
finally:
server.quit()
if failed:
return False
return True
回答1:
alright, I fixed the problem, @Nabla pointed the right!!
def sendMessage(self, subject, msgContent, files, mailto):
""" Send the email message
Args:
subject(string): subject for the email
msgContent(string): email message Content
files(List): list of files to be attached
mailto(string): email address to be sent to
"""
msg = self.prepareMail(subject, msgContent, files, mailto)
# connect to server and send email
server = smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
server.ehlo()
# use encrypted SSL mode
server.starttls()
# to make starttls work
server.ehlo()
server.login(self.usrname, self.password)
server.set_debuglevel(self.debug)
try:
server.sendmail(self.mailFrom, mailto, msg.as_string())
except Exception as er:
print er
return False
finally:
server.quit()
return True
回答2:
This is not a direct answer to your question, but if I may suggest a slightly-different implementation.
Put the connect-to-server and disconnect-from-server in two different methods:
class MyClass():
serverDict = {
"gmail" :"smtp.gmail.com",
"hotmail":"smtp.live.com",
"yahoo" :"smtp.mail.yahoo.com"
}
def __init__(self,username,password):
self.username = username
self.password = password
self.serverName = MyClass.serverDict[username[username.index("@")+1:username.index(".")]]
def sendMessage(self,subject,msgContent,files,mailto):
server = Connect()
if not server:
return False
failed = True
try:
server.login(self.username,self.password)
if not server.sendmail(self.mailFrom,mailto,msg.as_string()):
failed = False
except Exception,error:
print error
Disconnect(server)
return failed
def Connect():
try:
server = smtplib.SMTP(self.serverName)
except smtplib.SMTPException,error:
print error
return None
try:
server.ehlo()
if server.has_extn("starttls"):
server.starttls()
server.ehlo()
except (smtplib.SMTPException,ssl.SSLError),error:
print error
Disconnect(server)
return None
return server
def Disconnect(server):
try:
server.quit()
except smtplib.SMTPException,error:
print error
来源:https://stackoverflow.com/questions/21808107/should-return-outside-finally-and-is-the-exception-handled-perfectly