[PATCH 4 of 4] mail: switch from os to subprocess module when invoking sendmail

Yuya Nishihara yuya at tcha.org
Tue Jan 5 09:11:56 CST 2016


On Mon, 04 Jan 2016 21:56:10 -0800, Bryan O'Sullivan wrote:
> # HG changeset patch
> # User Bryan O'Sullivan <bos at serpentine.com>
> # Date 1451973286 28800
> #      Mon Jan 04 21:54:46 2016 -0800
> # Node ID 09ce74e1f92f8838bbcd2ac1309519f0448ffbed
> # Parent  3838e999a656ccd5644fadac75ed91f15ad22854
> mail: switch from os to subprocess module when invoking sendmail
> 
> This gets rid of some ugly string mangling; avoids a potentially
> unsafe trip through the shell; and (bonus!) reports the correct
> exit code if sendmail fails.
> 
> diff --git a/mercurial/mail.py b/mercurial/mail.py
> --- a/mercurial/mail.py
> +++ b/mercurial/mail.py
> @@ -12,6 +12,7 @@ import os
>  import quopri
>  import smtplib
>  import socket
> +import subprocess
>  import sys
>  import time
>  
> @@ -161,12 +162,13 @@ def _smtp(ui):
>  def _sendmail(ui, sender, recipients, msg):
>      '''send mail using sendmail.'''
>      program = ui.config('email', 'method', 'smtp')
> -    cmdline = '%s -f %s %s' % (program, util.email(sender),
> -                               ' '.join(map(util.email, recipients)))
> -    ui.note(_('sending mail: %s\n') % cmdline)
> -    fp = util.popen(cmdline, 'w')
> -    fp.write(msg)
> -    ret = fp.close()
> +    cmdline = ([program, '-f', util.email(sender)] +
> +               [util.email(r) for r in recipients])
> +    ui.note(_('sending mail: %s\n') % ' '.join(util.shellquote(c)
> +                                               for c in cmdline))
> +    p = subprocess.Popen(cmdline, stdin=subprocess.PIPE)

Can we assume that email.method is a single executable path?

I don't oppose to this change, but I'm asking it because ui.editor can be
a shell command with options.


More information about the Mercurial-devel mailing list