Bug in shlex??

S

samslists

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?

Here is the relevant section of the Posix specification on shell
parsing:
2.2.2 Single-Quotes

Enclosing characters in single-quotes ( '' ) shall preserve the
literal value of each character within the single-quotes. A single-
quote cannot occur within single-quotes.

That's from
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_03

Thanks for any help!
 
G

Gabriel Genellina

En Thu, 03 Apr 2008 19:20:59 -0300, (e-mail address removed)
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]
 
O

ockman

Gabriel...

I feel foolish...(and wish I had the two hours back I spent on
this). :)

Thank you so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top