应该最终回到外面并完成处理的例外吗?

时间:2014-02-16 06:32:25

标签: python exception-handling smtp smtplib

我最后不应该从下面这个方法中得到回报吗? Pylint给出了这个说法的错误: 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

2 个答案:

答案 0 :(得分:1)

好吧,我解决了这个问题,@ Nabla指向右边!!

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

答案 1 :(得分:1)

这不是您问题的直接答案,但如果我可能会建议稍微不同的实施。

connect-to-server disconnect-from-server 置于两种不同的方法中:

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