should return outside finally and is the exception handled perfectly?

这一生的挚爱 提交于 2020-08-03 05:50:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!