By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Bug in shlex??

P: n/a
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/...html#tag_02_03

Thanks for any help!
Apr 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
En Thu, 03 Apr 2008 19:20:59 -0300, sa*******@gmail.com
<sa*******@gmail.comescribió:
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

Apr 3 '08 #2

P: n/a
Gabriel...

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

Thank you so much!

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
Apr 3 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.