Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90883
| References | <87siastsby.fsf@Equus.decebal.nl> <CAPTjJmpMApx8Ptm=fcstWDY5MxZsLn5T7_Yod4QCyHWYHKAGOg@mail.gmail.com> |
|---|---|
| Date | 2015-05-19 12:55 -0500 |
| Subject | Re: Best way to rewrite Popen |
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.149.1432058162.17265.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On May 19, 2015 12:48 PM, "Chris Angelico" <rosuav@gmail.com> wrote:
>
> On Wed, May 20, 2015 at 3:01 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> > At the moment I am playing with things like:
> > p = subprocess.Popen('ls -l', shell = True, stdout =
subprocess.PIPE)
> >
> > I think that most of the times this are the values I want. So it would
> > be nice to overrule the defaults. What is the best way to do this? So
> > creating a function that is exactly the same except for the defaults
> > for shell and stdout (and maybe stderr).
>
> Well... I would have to start by saying that you probably _don't_ want
> to use shell=True by default. Putting it explicitly on the cases where
> you need it helps you remember its danger. You also don't need it for
> simple cases like that one; improve your reliability by providing a
> list instead of a string, and then you can leave shell=False:
>
> p = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE)
>
> Running everything via the shell is unnecessary, and a dangerous
> default. (Maybe it's not a problem when you use a string literal as
> the command, but if you make that the default, you'll end up exposing
> yourself in some situation where it isn't hard-coded.) With that
> change, there's really only one parameter that you're defaulting, so
> there's not as much point making the change, but the technique still
> works, and maybe you'll add more to the setup:
>
> @functools.wraps(subprocess.Popen)
> def Popen(*a, **kw):
> if 'stdout' not in kw: kw['stdout'] = subprocess.PIPE
> return subprocess.Popen(*a, **kw)
>
> That's a simple way to patch in some function defaults. But
> personally, I'd probably end up doing something like this:
>
> def capture_stdout(*a, **kw):
> if 'stdout' not in kw: kw['stdout'] = subprocess.PIPE
Just a quick note that this line can be simplified nicely to:
kw.setdefault('stdout', subprocess.PIPE)
Regards,
--
Zach
(on a phone)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Best way to rewrite Popen Cecil Westerhof <Cecil@decebal.nl> - 2015-05-19 19:01 +0200
Re: Best way to rewrite Popen Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-19 17:36 +0000
Re: Best way to rewrite Popen Cecil Westerhof <Cecil@decebal.nl> - 2015-05-19 21:13 +0200
Re: Best way to rewrite Popen Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-19 20:14 +0000
Re: Best way to rewrite Popen Cecil Westerhof <Cecil@decebal.nl> - 2015-05-19 22:19 +0200
Re: Best way to rewrite Popen Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-19 21:28 +0000
Re: Best way to rewrite Popen Cecil Westerhof <Cecil@decebal.nl> - 2015-05-20 00:23 +0200
Re: Best way to rewrite Popen MRAB <python@mrabarnett.plus.com> - 2015-05-20 00:20 +0100
Re: Best way to rewrite Popen Cecil Westerhof <Cecil@decebal.nl> - 2015-05-20 02:15 +0200
Re: Best way to rewrite Popen Jonas Wielicki <jonas@wielicki.name> - 2015-05-19 19:43 +0200
Re: Best way to rewrite Popen Chris Angelico <rosuav@gmail.com> - 2015-05-20 03:47 +1000
Re: Best way to rewrite Popen Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-05-19 12:55 -0500
Re: Best way to rewrite Popen Chris Angelico <rosuav@gmail.com> - 2015-05-20 04:13 +1000
Re: Best way to rewrite Popen Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-20 03:23 +0200
csiph-web