Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. @contextmanager
  2. def smtp_connection(host, user=None, passwd=None, timeout=5):
  3. conn = None # smell here
  4. try:
  5. conn = SMTP(host=host, timeout=timeout)
  6. conn.ehlo_or_helo_if_needed()
  7. if user and passwd:
  8. conn.login(user=user, password=passwd)
  9. logger.debug('SMTP connected')
  10. yield conn
  11. except Exception as e:
  12. raise e
  13. finally:
  14. if conn: # and here
  15. conn.quit()
  16.  
  17. with ExitStack() as stack:
  18. stack.callback(cleanup_resources)
  19. result = perform_operation()
  20. if result:
  21. stack.pop_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement