473,699 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incorrect(?) shlex behaviour

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?

Jul 19 '05 #1
4 1480
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/...ule-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.String IO('$(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

Jul 19 '05 #2
Its gets worse:
from shlex import StringIO
from shlex import shlex
t = shlex(StringIO( "2>&1"))
while True: .... b = t.read_token()
.... if not b: break
.... print b
....
2
&
1 <----------- where's the '>' !? import shlex
print shlex.split("2> &1")

['2>&1']

It strikes me that split should be behaving exactly the same way as
read_token, but that may be a misunderstandin g on my part of what split
is doing.

However, it is totally bizarre that read_token discards the '>' symbol
in the string! I don't know much about lexical analysis, but it
strikes me that discarding characters is a bad thing.

Jul 19 '05 #3
bill wrote:
Its gets worse:
from shlex import StringIO
from shlex import shlex
t = shlex(StringIO( "2>&1"))
while True: ... b = t.read_token()
... if not b: break
... print b
...
2
&
1 <----------- where's the '>' !? import shlex
print shlex.split("2> &1") ['2>&1']

It strikes me that split should be behaving exactly the same way as
read_token, but that may be a misunderstandin g on my part of what split is doing.

However, it is totally bizarre that read_token discards the '>' symbol in the string! I don't know much about lexical analysis, but it
strikes me that discarding characters is a bad thing.
From the docs: split(s[, comments])
Split the string s using shell-like syntax. If comments is False
(the default), the parsing of comments in the given string will be
disabled (setting the commenters member of the shlex instance to the
empty string). This function operates in POSIX mode. New in version
2.3.

Maybe looking at string methods split might help.
"$(which sh)".split() ['($(which', 'sh)']
From the docs:

read_token()
Read a raw token. Ignore the pushback stack, and do not interpret
source requests. (This is not ordinarily a useful entry point, and is
documented here only for the sake of completeness.)

# Just like in my first post from StringIO import StringIO
from shlex import shlex
t = shlex(StringIO( "2>&1"))
t.get_token() '2' t.get_token() '>' t.get_token() '&' t.get_token() '1' t.get_token() ''
# Your way t = shlex(StringIO( "2>&1"))
t.read_token() '2' t.read_token() '&' t.read_token() '1' t.read_token() ''


Hth,
M.E.Farmer

Jul 19 '05 #4
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
"bill" <bi**********@g mail.com> 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?


Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright" , "credits" or "license" for more information.
import shlex
print shlex.__doc__

A lexical analyzer class for simple shell-like syntaxes.
This has a little potential to mislead. Bourne shell
syntax is naturally "shell-like", but it is not "simple" -
as grammars go, it's a notorious mess. In theory, someone
could certainly write Python code to accurately parse Bourne
shell statements, but that doesn't appear to have been the
intention here. The "Parsing Rules" section of the documentation
describes what you can expect, and right off hand I don't see
how the result you got was erroneous.

Donn Cave, do**@u.washingt on.edu
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1328
by: Severus Snape | last post by:
If you open a window with resizable=no, Opera doesn't comply.
2
2165
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of virtual method calls. To further complicate (or perhaps simplify) matters, some compilers (GCC and MingW) produce the expected behaviour, while others (MSVS 7.1) do not. I can only offer two explanations for my observations: 1. The Microsoft...
7
2387
by: Netocrat | last post by:
The code at the bottom illustrates the nature of a situation discussed on comp.lang.asm.x86. Basically an unsigned integer is being used as a negative index in a loop, and it works as though the integer were signed. I want to confirm my interpretation of this behaviour as there have been differing understandings of why it works. On my system (P4 using gcc under linux), the results are as follows: len before sign swap : 9 shlen...
11
7100
by: Mark Findlay | last post by:
Hello Experts! I am attempting to use the OleDbCommand.ExecuteScaler() function within my ASP.NET C# web page to perform a simple validation, but receive the following error: "Incorrect syntax near the keyword 'DEFAULT'" The form has 2 fields on it, called tb_username and tb_password. (see code snippet below).
8
1344
by: yuliy | last post by:
hello, is this code has undefined behavour? int integer = 1234; int * int_ptr = &integer; cout << "*int_ptr = " << *int_ptr << "\n"; Why?
45
3341
by: simnav | last post by:
In the following code something strange happens ! If I keep pressed any of ALT+Arrow, keys, they are extracted two times from buffer then getch seems to stop; if I release and press again ALT+arrow nothing changes: the only way to exit from this condition is press another key a single time. What seems to happen is that kbhit say some keys are present in buffer but for getch don't see any data and waits for a key. Why this happen ? If I...
0
2385
by: TraceyAnnison | last post by:
I wonder if you can help me - I'm new to this, and working in a project that has already been configured to work with Axis. We have a Java project built with the Spring framework, in order to provide webservices for access via SOAP. As I understand it (ie not very well!) Axis works to accept these SOAP messages from the client and transform them into calls on our Java methods, and do the reverse with the responses - cool! Which is excellent,...
2
1362
by: PaulCarmo | last post by:
Hello, I'm validating a form containing various types of data. One of the fields to be validated is an amount which must be between 0.01 and 999999.99. The code for this field validation in the validation.xml is; <field property="amount" depends="required,webxvalidator,floatRange"> <arg0 key="label.authorization.txn.amount"/> <arg1 name="floatRange" key="${var:min}" resource="false"/>
285
8847
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/ specs gcc version 2.95.3 20010315 (release)
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.