Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'assuming': 0.09; 'prefix': 0.09; 'subject:skip:t 10': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; 'systems.': 0.12; "wouldn't": 0.14; 'windows': 0.15; 'command,': 0.16; 'did.': 0.16; 'differs': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "he'd": 0.16; 'operation,': 0.16; 'splitting': 0.16; 'ssh,': 0.16; 'string:': 0.16; 'too?': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'variable': 0.18; 'trying': 0.19; "hasn't": 0.19; 'split': 0.19; 'command': 0.22; '>>>': 0.22; 'example': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'byte': 0.24; 'case.': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'skip:" 30': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'skip:p 30': 0.29; '(this': 0.29; 'tim': 0.29; 'unix': 0.29; 'expansion': 0.30; 'message-id:@mail.gmail.com': 0.30; 'asked': 0.31; '(although': 0.31; 'chase': 0.31; 'quite': 0.32; 'running': 0.33; 'totally': 0.33; 'maybe': 0.34; "i'd": 0.34; 'common': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done,': 0.36; 'done': 0.36; 'doing': 0.36; "didn't": 0.36; 'list': 0.37; '(i.e.,': 0.38; 'pm,': 0.38; 'commands': 0.60; 'most': 0.60; 'free': 0.61; 'you.': 0.62; 'back': 0.62; "you'll": 0.62; 'more': 0.64; 'incorporate': 0.68; 'line,': 0.68; 'unusual': 0.74; 'behavior': 0.77; "it'd": 0.84; 'wish.': 0.84; 'to:none': 0.92 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=8LQYYn2GSe54wC8UO3nhkMdRJw7JkFdQxVB4+RVM4OU=; b=SIyE+IL5BOfrIOljga4DRZ3gr/hxaL2e9SZKBtHOvtTg86n7xl0btmvpgZyaXUkWQj dQxUtabmhxGTJ3ExQPz+II7623GHcmEnN3zklTcj2XCnyHmX0YZ+E6izK+st1EfEWgtE 8B6H+uSurn4UkWdFlUPx4scr30FKihsTxao41Nk89qdCwR0Oqti365LoeMR6nekNOoqI r3OAsGSpFnBfD9d10wsVGVTIbm8RBpFJW48NQqBAphnF0k3iyKiL6UagNgmWinUCwswd QY8hyBtO5ljf3IQD9rxLG5UPMCvThuMFsmBntiFlzPiaGkC5RpaFgKIOBNrPCS+5ZFF7 63ew== MIME-Version: 1.0 X-Received: by 10.52.136.98 with SMTP id pz2mr47170vdb.70.1402916941512; Mon, 16 Jun 2014 04:09:01 -0700 (PDT) In-Reply-To: <20140616055124.56ccd3c5@bigbox.christie.dr> References: <539EBF61.4050106@rece.vub.ac.be> <539EC5EF.7040205@rece.vub.ac.be> <20140616055124.56ccd3c5@bigbox.christie.dr> Date: Mon, 16 Jun 2014 21:09:01 +1000 Subject: Re: line to argv transformation 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.15 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402916949 news.xs4all.nl 2873 [2001:888:2000:d::a6]:33156 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73313 On Mon, Jun 16, 2014 at 8:51 PM, Tim Chase wrote: > On 2014-06-16 20:41, Chris Angelico wrote: >> Oops! I made the cardinal error of trying in one and assuming it'd >> work in both. Just needs a b prefix on the split string: >> >> def shell_split(cmd): >> return subprocess.check_output("""python -c 'import sys; >> print("\\0".join(sys.argv[1:]))' >> """+cmd,shell=True)[:-1].split(b"\0") >> >> You'll get back a list of byte strings, in any case. Feel free to >> pass them through a decode operation, or to incorporate a .decode() >> into the above stream, as you wish. > > Tested on Win32? The behavior of "shell expansion" on Windows > cmd.exe differs from most *nix shells (i.e., it *doesn't* expand, so > you have to do it yourself), so Antoon would need to describe the > desired behavior on Win32. Well, his example commands began "ls", which is a common Unix command, but isn't present on most Windows systems. If he'd started out with something that looked more Windowsish, I'd totally understand - you start with a single line, and you need to do what the cmd.exe shell hasn't done for you. (Although splitting is done, so it still wouldn't be quite as clear.) But he said "treated as a command line". So that's exactly what I did. :) He didn't ask about globbing, he asked about doing what the shell does... maybe he wants variable expansion too? >>> shell_split("echo $DISPLAY") [b'echo', b'localhost:10.0'] (This is running over SSH, so my $DISPLAY is a perhaps unusual value.) ChrisA