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


Groups > comp.lang.python > #73310 > unrolled thread

Re: line to argv transformation

Started byChris Angelico <rosuav@gmail.com>
First post2014-06-16 20:41 +1000
Last post2014-06-16 21:25 +1000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: line to argv transformation Chris Angelico <rosuav@gmail.com> - 2014-06-16 20:41 +1000
    Re: line to argv transformation Marko Rauhamaa <marko@pacujo.net> - 2014-06-16 13:59 +0300
      Re: line to argv transformation Chris Angelico <rosuav@gmail.com> - 2014-06-16 21:25 +1000

#73310 — Re: line to argv transformation

FromChris Angelico <rosuav@gmail.com>
Date2014-06-16 20:41 +1000
SubjectRe: line to argv transformation
Message-ID<mailman.11082.1402915284.18130.python-list@python.org>
On Mon, Jun 16, 2014 at 8:24 PM, Antoon Pardon
<antoon.pardon@rece.vub.ac.be> wrote:
> On 16-06-14 12:06, Chris Angelico wrote:
>
>> def shell_split(cmd):
>>     return subprocess.check_output("""python -c 'import sys;
>> print("\\0".join(sys.argv[1:]))' """+cmd,shell=True)[:-1].split("\0")
>
> Nice idea, unfortunatly it doesn't work in python3.3
>
>>>> shell_split("ls *.py")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 3, in shell_split
> TypeError: Type str doesn't support the buffer API
>>>>

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.

ChrisA

[toc] | [next] | [standalone]


#73311

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-06-16 13:59 +0300
Message-ID<871tupp1bb.fsf@elektro.pacujo.net>
In reply to#73310
Chris Angelico <rosuav@gmail.com>:

> 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.

Now you are subject to the quirks of /bin/sh. For example, '*.xyz'
expands itself ('*.xyz') if there is no match in the current working
directory.

Moreover, you need to guard against arguments like

   $(cat ~/.ssh/id_dsa)

which would spill the beans.


Marko

[toc] | [prev] | [next] | [standalone]


#73315

FromChris Angelico <rosuav@gmail.com>
Date2014-06-16 21:25 +1000
Message-ID<mailman.11086.1402917952.18130.python-list@python.org>
In reply to#73311
On Mon, Jun 16, 2014 at 8:59 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>
>> 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.
>
> Now you are subject to the quirks of /bin/sh. For example, '*.xyz'
> expands itself ('*.xyz') if there is no match in the current working
> directory.
>
> Moreover, you need to guard against arguments like
>
>    $(cat ~/.ssh/id_dsa)
>
> which would spill the beans.

As I said in response to Tim, the somewhat underspecified question
does leave open followups of whether both of those would be features,
rather than bugs. For instance, if "*.py" should expand to a list of
all files matching that glob, should "[123].py" expand to any files
matching that pattern? I'm not sure that your typical glob function
handles that. And should "spam{eggs,spam}" become
"spameggs","spamspam"? (Though that one's bash-specific, I believe.)
Where do you draw the line?

My reading is that it should be one of two options:

1) Split, according to shell rules, and then glob. Nothing more. No
square brackets (probably), no braces, nothing.
2) Do exactly what $CHELL would do, for some value of $CHELL.

But neither is quite clear, and I can see exactly why there isn't
anything in the stdlib. And what shell do you want to imitate, for
option 2? Using /bin/sh makes a lot of sense... but so does /bin/bash.
Or maybe you should use the user's own login shell, if you're wrapping
a command prompt in some way. Perhaps you want to use GLaDOS; but
remember that although fun and learning are the primary goals of this
activity, serious injuries may occur, especially when using backticks
or $( ) in the command.

OP needs to specify better. Otherwise Black Mesa will get the contract.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web