473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Bug in shlex??

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
2 1988
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken Fettig | last post by:
Does Python have an equivelent to the Java StringTokenizer? If so, what is it and how do you implement it? Thanks Ken Fettig kenfettig@btinet.net kfettig@state.nd.us
6
by: Maarten van Reeuwijk | last post by:
Hi group, I need to parse various text files in python. I was wondering if there was a general purpose tokenizer available. I know about split(), but this (otherwise very handy method does not...
3
by: Matthias Teege | last post by:
Moin, I'm new to python. I try to build a "easy" search interface to a database table in python. I have a input string like this: "name=matthias;count>10" or this "location!=thisone". From this...
15
by: Freddie | last post by:
Happy new year! Since I have run out of alcohol, I'll ask a question that I haven't really worked out an answer for yet. Is there an elegant way to turn something like: > moo cow "farmer john"...
4
by: bill | last post by:
Consider the following: import os, commands os.environ="string with foo" a = '$QWE ${QWE/foo/baz}' b = commands.getoutput('echo ' + a) This does what I want, which is to expand a...
4
by: bill | last post by:
Consider: >>> import shlex >>> shlex.split('$(which sh)') Is this behavior correct? It seems that I should either get one token, or the list , but certainly breaking it the way it does is...
4
by: p.lavarre | last post by:
How can I instantiate shlex.shlex to behave like shlex.split does? I see shlex.split gives me what I want: import shlex print shlex.split("1.2e+3") # 1.2e+3 But every doc'ed...
10
by: teddyber | last post by:
Hello, first i'm a newbie to python (but i searched the Internet i swear). i'm looking for some way to split up a string into a list of pairs 'key=value'. This code should be able to handle this...
10
by: Julien | last post by:
Hi, I'm fairly new in Python and I haven't used the regular expressions enough to be able to achieve what I want. I'd like to select terms in a string, so I can then do a search in my database....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.