Timeline for Context manager for SMTP connections
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 21, 2016 at 9:12 | vote | accept | user1685095 | ||
Apr 21, 2016 at 9:12 | comment | added | user1685095 | @MathiasEttinger hm... I guess you right! I've just checked. SMTP closes socket anyway in case of error. | |
Apr 21, 2016 at 8:30 | comment | added | 301_Moved_Permanently |
@user1685095 And what do you think happens in your code in the exact same case? SMTP(...) raise an exception so the assignement conn = .. doesn't occur. Thus, in the finally block, the if conn: doesn't trigger either. Both code handle that the same way.
|
|
Apr 21, 2016 at 8:10 | comment | added | user1685095 | @MathiasEttinger Judging by the code of stmplib it's wouldn't close the socket in case of errors. So AFAIK you code has resource leak. | |
Apr 21, 2016 at 7:26 | comment | added | user1685095 |
@MathiasEttinger I agree on first point about reraising. Separation of concerns isn't a concern here (pun intended) because the connection without login will be never needed, but that's just details. What I'm dubious about is removing conn = SMTP(...) from try except block. If we got for example connection refused exception the finally block wouldn't be called. And shouldn't connection have some resources in that case (like socket that it probably opened to trying to connect etc)?
|
|
Apr 21, 2016 at 6:53 | comment | added | 301_Moved_Permanently | @SjoerdJobPostmus Right, I'm not quite using the decorator so I got the behavior somewhat mixed up. | |
Apr 21, 2016 at 6:51 | history | edited | 301_Moved_Permanently | CC BY-SA 3.0 |
added 1233 characters in body
|
Apr 21, 2016 at 5:07 | comment | added | Sjoerd Job Postmus |
You're missing a try /finally in smtp_connection . This way quit won't be called on exceptions.
|
|
Apr 20, 2016 at 13:13 | history | answered | 301_Moved_Permanently | CC BY-SA 3.0 |