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

Christian Ebert blacktrash at gmx.net
Fri Jan 25 20:57:18 UTC 2008


* Christian Ebert on Friday, January 25, 2008 at 14:01:37 +0000
> afaics the price for giving up a class is introducing a global
> variable, if we want to reuse an active connection via smptlib.

If we don't want a global var, and without adding an additional
argument to all send calls, the only way I see to solve this is
by adding a _mailer attribute to ui.

external sendmail method has no connection parameter
smtplib methos has additional connection parameter

Here's a proposal with adding ui._mailer

c




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

- for smtplib make _mailer an ui attribute for reusing active
  connection
- make separation of python smtplib and external sendmail call
  more transparent
- only catch smtplib errors when we are actually using it

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -9,8 +9,7 @@
 import os, smtplib, templater, util, socket
 
 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 +37,16 @@
         s.login(username, password)
     return s
 
+def sendmail(ui, sender, recipients, msg):
+    '''send mail with smtplib.'''
+    try:
+        return ui._mailer.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 +64,10 @@
 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)
+        ui._mailer = _smtp(ui)
+        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