Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'explicitly': 0.05; 'true,': 0.05; 'defaults': 0.07; 'string': 0.09; 'default.': 0.09; 'iterate': 0.09; 'literal': 0.09; 'parameter': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'command,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'kw:': 0.16; 'overrule': 0.16; 'personally,': 0.16; 'stdout': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '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; '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; 'default,': 0.31; 'layer': 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; 'enough': 0.39; '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; '20,': 0.68; 'yourself': 0.78; '2015': 0.84; 'one;': 0.84; 'reliability': 0.84; 'exposing': 0.91; 'subject:Best': 0.91; 'to:none': 0.92; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=PsZ1jsfoNGIcnLAPNaWO595mklRa0mhP1+paPiZLrQI=; b=fsfUcu+UT1m1xvSpWwT+Cfmf4i8xG5PFYfcENIXeqexaho4BdtTrDysMycvbAYLGRQ PfXNMne7tDN6ZCdoHn6PU8HmLamTd2+1Wfjs7frOCVbbHYeD6JEjVMkgthzbJ/wDstEt qm/Kx63ghM7HCg/GTJ8yiAP/uGt3e/xKcLaSDsmuTjEV74pWQsacb6Ly4APd10NgheXy F2Hg6UnyrPmmno+F+Rw8l/ugaAOmVdKMmxAwpvhl1lLFniSaw5lTWIBzvXZoMVqN2xQ+ GvtJYJqlm6HvrnamRtV5mnIkHNyLQFxUGK1Zp4oRvub+QpdV2DlERVJ9NZQabUj+BHB6 +CHw== MIME-Version: 1.0 X-Received: by 10.50.141.164 with SMTP id rp4mr491729igb.2.1432057662735; Tue, 19 May 2015 10:47:42 -0700 (PDT) In-Reply-To: <87siastsby.fsf@Equus.decebal.nl> References: <87siastsby.fsf@Equus.decebal.nl> Date: Wed, 20 May 2015 03:47:42 +1000 Subject: Re: Best way to rewrite Popen From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432057670 news.xs4all.nl 2843 [2001:888:2000:d::a6]:58889 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5567 X-Received-Body-CRC: 2931514127 Xref: csiph.com comp.lang.python:90881 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 return subprocess.Popen(*a, **kw).stdout so I can directly iterate over the thing. Or something else. One change isn't really enough to justify the extra layer of indirection. ChrisA