Bug in shlex??

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 3 18:44:16 EDT 2008


En Thu, 03 Apr 2008 19:20:59 -0300, samslists at gmail.com  
<samslists at gmail.com> escribió:

> I'm trying to use shlex.split to simulate what would happen in the
> shell.  The docs say that it should be as close as possible to the
> posix shell parsing rules.
>
> If you type the following into a posix compliant shell
>
> echo '\?foo'
>
> you get back:
> \?foo
>
> (I've tested this in dash, bash and zsh---all give the same results.)
>
> Now here's what happens in python:
>
> Python 2.5.1 (r251:54863, Mar  7 2008, 03:39:23)
> [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import shlex
>>>> shlex.split("'\?foo'")
> ['\\?foo']
>>>>
>
> I think this is a bug?  Or am I just misunderstanding?

The result is a list containing a single string. The string contains 5  
characters: a single backslash, a question mark, three letters. The  
backslash is the escape character, as in '\n' (a single character,  
newline). A backslash by itself is represented (both by repr() and in  
string literals) by doubling it.

If you print the value, you'll see a single \:

print shlex.split("'\?foo'")[0]

-- 
Gabriel Genellina




More information about the Python-list mailing list