incorrect(?) shlex behaviour

M.E.Farmer mefjr75 at hotmail.com
Sun May 15 01:34:33 EDT 2005


bill wrote:
> Consider:
> >>> import shlex
> >>> shlex.split('$(which sh)')
> ['$(which', 'sh)']
>
> Is this behavior correct?  It seems that I should
> either get one token, or the list
> ['$','(','which','sh',')'],
> but certainly breaking it the way it does is
> erroneous.
>
> Can anyone explain why the string is being split
> that way?
This may help.
http://www.python.org/dev/doc/devel/lib/module-shlex.html
This works on Python 2.4:
>>> import shlex
>>> sh = shlex.shlex('$(which sh)')
>>> sh.get_token()
'$'
>>> sh.get_token()
'('
>>> sh.get_token()
'which'
>>> sh.get_token()
'sh'
>>> sh.get_token()
')'
>>> sh.get_token()
etc...

Python 2.2 and maybe lower:
>>> import shlex
>>> import StringIO
>>> s = StringIO.StringIO('$(which sh)')
>>> sh = shlex.shlex(s)
>>> sh.get_token()
'$'
>>> sh.get_token()
'('
>>> sh.get_token()
'which'
>>> sh.get_token()
'sh'
>>> sh.get_token()
')'
>>> sh.get_token()
etc...

Hth,
M.E.Farmer




More information about the Python-list mailing list