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: 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>': 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; '>': 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: References: <87siastsby.fsf@Equus.decebal.nl> 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 To: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --e89a8fb1f33c89f684051673054a Content-Type: text/plain; charset=UTF-8 On May 19, 2015 12:48 PM, "Chris Angelico" wrote: > > On Wed, May 20, 2015 at 3:01 AM, Cecil Westerhof 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) --e89a8fb1f33c89f684051673054a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

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:
> >=C2=A0 =C2=A0 =C2=A0p =3D subprocess.Popen('ls -l', shell = =3D True, stdout =3D 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 defau= lts
> > for shell and stdout (and maybe stderr).
>
> Well... I would have to start by saying that you probably _don't_ = want
> to use shell=3DTrue by default. Putting it explicitly on the cases whe= re
> 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=3DFalse:
>
> p =3D subprocess.Popen(['ls','-l'], stdout=3Dsubproces= s.PIPE)
>
> Running everything via the shell is unnecessary, and a dangerous
> default. (Maybe it's not a problem when you use a string literal a= s
> the command, but if you make that the default, you'll end up expos= ing
> yourself in some situation where it isn't hard-coded.) With that > change, there's really only one parameter that you're defaulti= ng, so
> there's not as much point making the change, but the technique sti= ll
> works, and maybe you'll add more to the setup:
>
> @functools.wraps(subprocess.Popen)
> def Popen(*a, **kw):
> =C2=A0 =C2=A0 if 'stdout' not in kw: kw['stdout'] =3D = subprocess.PIPE
> =C2=A0 =C2=A0 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):
> =C2=A0 =C2=A0 if 'stdout' not in kw: kw['stdout'] =3D = subprocess.PIPE

Just a quick note that this line can be simplified nicely to= :

kw.setdefault('stdout', subprocess.PIPE)

Regards,
--
Zach
(on a phone)

--e89a8fb1f33c89f684051673054a--