[PATCH] mail: fix regression introduced in dc6ed2736c81 (issue #943)

Christian Ebert blacktrash at gmx.net
Fri Jan 25 08:01:37 CST 2008


Hi,

dc6ed2736c81 either broke sending mail with external sendmail
(TypeError) or caused problems eg. with sending patch series
(issue #943).

afaics the price for giving up a class is introducing a global
variable, if we want to reuse an active connection via smptlib.

c



# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1201267090 0
# Node ID acbd90e664f080f9f9221a0e71f2e659818d02f1
# Parent  28a79c259fcfd52ac6a80696bff612a2dd0be829
mail: fix regression introduced in dc6ed2736c81 (issue #943)

- make separation of python smtplib and external sendmail call
  more transparent
- only catch smtplib errors when we are actually using it
- introduce global variable _smtpconnection
  needed for reusing an already active connection

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -8,9 +8,11 @@
 from i18n import _
 import os, smtplib, templater, util, socket
 
+# do we have an active smtp connection?
+_smtpconnection = None
+
 def _smtp(ui):
-    '''send mail using smtp.'''
-
+    '''activate smtp connection with smtplib.'''
     local_hostname = ui.config('smtp', 'local_hostname')
     s = smtplib.SMTP(local_hostname=local_hostname)
     mailhost = ui.config('smtp', 'host')
@@ -38,6 +40,19 @@
         s.login(username, password)
     return s
 
+def sendmail(ui, sender, recipients, msg):
+    '''send mail with smtplib.'''
+    global _smtpconnection
+    if not _smtpconnection:
+        _smtpconnection = _smtp(ui)
+    try:
+        return _smtpconnection.sendmail(sender, recipients, msg)
+    except smtplib.SMTPRecipientsRefused, inst:
+        recipients = [r[1] for r in inst.recipients.values()]
+        raise util.Abort('\n' + '\n'.join(recipients))
+    except smtplib.SMTPException, inst:
+        raise util.Abort(inst)
+
 def _sendmail(ui, sender, recipients, msg):
     '''send mail using sendmail.'''
     program = ui.config('email', 'method')
@@ -55,31 +70,9 @@
 def connect(ui):
     '''make a mail connection. return a function to send mail.
     call as sendmail(sender, list-of-recipients, msg).'''
-
-    func =  _sendmail
     if ui.config('email', 'method', 'smtp') == 'smtp':
-        func = _smtp(ui)
-
-    def send(ui, sender, recipients, msg):
-        try:
-            return func.sendmail(sender, recipients, msg)
-        except smtplib.SMTPRecipientsRefused, inst:
-            recipients = [r[1] for r in inst.recipients.values()]
-            raise util.Abort('\n' + '\n'.join(recipients))
-        except smtplib.SMTPException, inst:
-            raise util.Abort(inst)
-
-    return send
-
-def sendmail(ui, sender, recipients, msg):
-    try:
-        send = connect(ui)
-        return send(sender, recipients, msg)
-    except smtplib.SMTPRecipientsRefused, inst:
-        recipients = [r[1] for r in inst.recipients.values()]
-        raise util.Abort('\n' + '\n'.join(recipients))
-    except smtplib.SMTPException, inst:
-        raise util.Abort(inst)
+        return sendmail
+    return _sendmail
 
 def validateconfig(ui):
     '''determine if we have enough config data to try sending email.'''



More information about the Mercurial-devel mailing list