473,652 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optparse escaping control characters

optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something? Can this be prevented, or worked around?

This behaviour doesn't occur with non-control characters.

For example, if this program (called test.py):
from optparse import OptionParser
parser = OptionParser()
parser.add_opti on("-d", dest="delimiter ", action="store")
(options, args) = parser.parse_ar gs()
print options

is run as follows:
python test.py -d '\t'

it outputs:
{'delimiter': '\\t'}

i.e. the \t has had an escape character added to give \\t.

Aug 19 '08 #1
7 4108
wa**********@gm ail.com writes:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something? Can this be prevented, or worked around?
It has nothing to do with optparse, it's how Python prints strings:

$ python -c 'import sys; print sys.argv' '\t'
['-c', '\\t']

Note that you're not really passing a control character to Python,
you're passing a two-character string consisting of \ and t. When
representing the string inside a data structure, Python escapes the \
to avoid confusion with a real control character such as \t.

If you try printing the string itself, you'll see that everything is
correct:

$ python -c 'import sys; print sys.argv[1]' '\t'
\t
Aug 19 '08 #2
wa**********@gm ail.com wrote:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something?
you're missing the distinction between the content of a string object,
and how the corresponding string literal looks.
>>x = {'delimiter': '\\t'}
x
{'delimiter': '\\t'}
>>x["delimiter"]
'\\t'
>>print x["delimiter"]
\t
>>len(x["delimiter"])
2

</F>

Aug 19 '08 #3
On Aug 19, 1:45*pm, Hrvoje Niksic <hnik...@xemacs .orgwrote:
wannymaho...@gm ail.com writes:
optparse seems to be escaping control characters that I pass as
arguments on the command line. *Is this a bug? *Am I missing
something? *Can this be prevented, or worked around?

It has nothing to do with optparse, it's how Python prints strings:

$ python -c 'import sys; print sys.argv' '\t'
['-c', '\\t']

Note that you're not really passing a control character to Python,
you're passing a two-character string consisting of \ and t. *When
representing the string inside a data structure, Python escapes the \
to avoid confusion with a real control character such as \t.

If you try printing the string itself, you'll see that everything is
correct:

$ python -c 'import sys; print sys.argv[1]' '\t'
\t
Thanks for the reply, much clearer now, just one more question. How
would I pass a control character to python on the command line?

Aug 19 '08 #4
On Aug 19, 10:35 pm, wannymaho...@gm ail.com wrote:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something? Can this be prevented, or worked around?

This behaviour doesn't occur with non-control characters.

For example, if this program (called test.py):
from optparse import OptionParser
parser = OptionParser()
parser.add_opti on("-d", dest="delimiter ", action="store")
(options, args) = parser.parse_ar gs()
print options

is run as follows:
python test.py -d '\t'

it outputs:
{'delimiter': '\\t'}

i.e. the \t has had an escape character added to give \\t.
You are inputting a TWO-byte string composed of a backslash and a
lowercase t, and feeding that to OptionParser.

C:\junk>type test.py
import sys; a = sys.argv[1]; d = {'delimiter': a}
print len(a), a, str(a), repr(a)
print d

# Note: this is Windows, where the shell quote is ", not '
C:\junk>python test.py "\t"
2 \t \t '\\t'
{'delimiter': '\\t'}

The extra backslash that you see is caused by the (implicit) use of
repr() to display the string.

If you want/need to enter a literal TAB character in the command line,
consult the manual for your shell.

HTH,
John
Aug 19 '08 #5
On Tue, 19 Aug 2008 05:35:27 -0700, wannymahoots wrote:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing something?
Can this be prevented, or worked around?
You are misinterpreting the evidence. Here's the short explanation:

optparse isn't escaping a control character, because you're not supplying
it with a control character. You're supplying it with two normal
characters, which merely *look* like five (including the quote marks)
because of Python's special handling of backslashes.
If you need it, here's the long-winded explanation.

I've made a small change to your test.py file to demonstrate:

# test.py (modified)
from optparse import OptionParser
parser = OptionParser()
parser.add_opti on("-d", dest="delimiter ", action="store")
(options, args) = parser.parse_ar gs()
print "Options:", options
print "str of options.delimit er =", str(options.del imiter)
print "repr of options.delimit er =", repr(options.de limiter)
print "len of options.delimit er =", len(options.del imiter)
Here's what it does when I call it:

$ python test.py -d '\t'
Options: {'delimiter': '\\t'}
str of options.delimit er = \t
repr of options.delimit er = '\\t'
len of options.delimit er = 2
When you pass '\t' in the command line, the shell sends a literal
backslash followed by a lowercase t to Python. That is, it sends the
literal string '\t', not a control character.

Proof: pass the same string to the "wc" program using "echo". Don't
forget that echo adds a newline to the string:

$ echo 't' | wc # just a t
1 1 2
$ echo '\t' | wc # a backslash and a t, not a control character
1 1 3
That's the first half of the puzzle. Now the second half -- why is Python
adding a *second* backslash to the backslash-t? Actually, it isn't, but
it *seems* to be adding not just a second backslash but also two quote
marks.

The backslash in Python is special. If you wanted a literal backslash t
in a Python string, you would have to type *two* backslashes:

'\\t'

because a single backslash followed by t is escaped to make a tab
character.

But be careful to note that even though you typed five characters (quote,
backslash, backslash, t, quote) Python creates a string of length two: a
single backslash and a t.

Now, when you print something using the str() function, Python hides all
that complexity from you. Hence the line of output that looks like this:

str of options.delimit er = \t

The argument is a literal backslash followed by a t, not a tab character.

But when you print using the repr() function, Python shows you what you
would have typed -- five characters as follows:

repr of options.delimit er = '\\t'

But that's just the *display* of a two character string. The actual
string itself is only two characters, despite the two quotes and the two
backslashes.

Now for the final piece of the puzzle: when you print most composite
objects, like the OptParse Value objects -- the object named "options" in
your code -- Python prints the internals of it using repr() rather than
str().

--
Steven
Aug 19 '08 #6
Thanks for all the responses!
Aug 19 '08 #7
Dan Halligan <da**********@g mail.comwrites:
How would I pass a control character to python on the command line?
It depends on which command line you are using. Most Unix-like shells
will allow you to input a control character by preceding it with ^V.
Since \t is the TAB character, you should be able to input it like
this:

$ python -c 'import sys; print sys.argv' '^V<tab>'
['-c', '\t'] # note single backslash
Aug 19 '08 #8

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

Similar topics

0
1958
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records. I have specified <xsl:output method="html"/> and encoding="iso-8859-1". When I apply the xsl:value-of and set the disable-output-escaping to "yes", the HTML formatting tags are displayed correctly, but the French accented characters are...
5
2317
by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e"; Regex re = new Regex( string.Format(@"(+)", escaped), RegexOptions.CultureInvariant ); string s = re.Replace( input, "" ); It doesn't seem to work, regular expression return without filter out any character
11
2172
by: Geoff Caplan | last post by:
Hi folks, The thread on injection attacks was very instructive, but seemed to run out of steam at an interesting point. Now you guys have kindly educated me about the real nature of the issues, can I ask again what effective escaping really means? Are the standard escaping functions found in the PHP, Tcl etc APIs to Postgres bombproof? Are there any encodings that might slip through and be cast to malicious strings inside Postgres?...
4
3228
by: agarwalpiyush | last post by:
Hello, I am going nuts with trying to get the following to work: This is what I intend to do: I have a line in /etc/syslog.conf which I need to delete based on ip-address provided to me in a variable. Forgetting that variable part for now .. this is what i want in the grep command: grep -suob "\*\.\* *...@172.23.62.12"
4
85009
by: James Black | last post by:
I have an application that relies on JSON, and I realized yesterday that I when the user types at least some of these characters (most likely all, haven't had time to verify) that the parser has problems: , { } " ' How can I escape these characters so that it is in the file, but can still be parsed? For example: {data: '", "and {more data}"]}
4
9169
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output contain < not <), while it works when using MSXML2 to do the transform. Does anyone have the same problem and how to make the escape work? Thanks. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3
5379
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier to read in
4
1645
by: Mathias Waack | last post by:
We've integrated python into a legacy application. Everything works fine (of course because its python;). There's only one small problem: the application reads the commandline and consumes all arguments prefixed with a '-' sign. Thus its not possible to call a python module from the commandline with a parameter list containing options prefixed by '-' or '--' signs. Thats not a major problem, but it prevents us from using th optparse...
1
1994
by: Mariano Mara | last post by:
Hi everyone. I'm building a script with optparse. One of the parameters will be a password. How can I code the optparse to accept/handle/format the password so that the user does not have to write it in plain/visible text in the terminal? TIA, Mariano.
0
8367
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...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
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
8589
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
7302
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
6160
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
4145
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...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
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

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.