Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90883

Re: Best way to rewrite Popen

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!2.eu.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <zachary.ware@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'explicitly': 0.05; 'true,': 0.05; 'defaults': 0.07; 'nicely': 0.07; 'string': 0.09; 'default.': 0.09; 'literal': 0.09; 'parameter': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'command,': 0.16; 'kw:': 0.16; 'overrule': 0.16; 'personally,': 0.16; 'phone)': 0.16; 'simplified': 0.16; 'stdout': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'wed,': 0.18; 'email addr:gmail.com&gt;': 0.22; 'putting': 0.22; 'saying': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'this?': 0.23; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '&gt;': 0.26; 'this:': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'point': 0.28; 'function': 0.29; 'leave': 0.29; 'am,': 0.29; 'patch': 0.29; 'change,': 0.30; 'message- id:@mail.gmail.com': 0.30; '(maybe': 0.31; '(on': 0.31; 'default,': 0.31; 'probably': 0.32; 'running': 0.33; 'cases': 0.33; 'moment': 0.34; 'maybe': 0.34; "i'd": 0.34; 'problem': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; 'doing': 0.36; 'list': 0.37; 'pm,': 0.38; 'dangerous': 0.60; 'most': 0.60; 'helps': 0.61; 'providing': 0.61; 'simple': 0.61; "you're": 0.61; 'times': 0.62; "you'll": 0.62; 'making': 0.63; 'more': 0.64; 'situation': 0.65; 'to:addr:gmail.com': 0.65; '20,': 0.68; 'yourself': 0.78; '2015': 0.84; 'one;': 0.84; 'reliability': 0.84; 'exposing': 0.91; 'subject:Best': 0.91; 'technique': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=atPJquJAKbdX5ehjvAHm2Wy4JCLO4c2UIE1TlPX78tE=; b=JZlXgwrvClTnuCJaeJf9NUMvCGJk8n6yQepEiK+R9VAhrCIpLUP+y9ssIfgK3S3IDW T+x3gxHMHcj4DKM1ysYLyN0sMU2AzI/2279Q7NIPK8CmC7rlLOEMD8U19rITDyJZlk4E X9husqWHYxVRWRnOyutPCADIhUIfCC0OrP2JAVRsGW/G2DP/ZWZ1egE7WwAswtFar8o5 6Q9cXd5ux1OxO4iOhg+tgcuZimxQ5A8uF0/c8T7w508WRnLIjfnYCuIOuJFKrsvFMI4x rjEDrxOMKcEAKkhkQcPPAGAw9FbnYnAkUkwpsYI/G/R+Wo35J052h4XFTX0Xj0g04SMJ kMcg==
MIME-Version 1.0
X-Received by 10.182.91.4 with SMTP id ca4mr25422184obb.39.1432058153529; Tue, 19 May 2015 10:55:53 -0700 (PDT)
Sender zachary.ware@gmail.com
In-Reply-To <CAPTjJmpMApx8Ptm=fcstWDY5MxZsLn5T7_Yod4QCyHWYHKAGOg@mail.gmail.com>
References <87siastsby.fsf@Equus.decebal.nl> <CAPTjJmpMApx8Ptm=fcstWDY5MxZsLn5T7_Yod4QCyHWYHKAGOg@mail.gmail.com>
Date Tue, 19 May 2015 12:55:53 -0500
X-Google-Sender-Auth TUv26A53fd8vg1-S0EBd-v_MKnQ
Subject Re: Best way to rewrite Popen
From Zachary Ware <zachary.ware+pylist@gmail.com>
To Chris Angelico <rosuav@gmail.com>
Cc python-list@python.org
Content-Type multipart/alternative; boundary=e89a8fb1f33c89f684051673054a
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.149.1432058162.17265.python-list@python.org> (permalink)
Lines 120
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432058162 news.xs4all.nl 2843 [2001:888:2000:d::a6]:36160
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90883

Show key headers only | View raw


[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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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