Re: [python-win32] does pipes.quote() work properly on Windows?

2010-05-24 Thread Tim Roberts
Bill Janssen wrote:
 I'm actually not passing file names.  I'm passing argument strings,
 which may contain spaces, quotes, and other things.  For instance,

myprogram --title=That's the game! says Mike Hammer Brotsky --file=...

 myprogram is a Python program, and expects to get the whole --title
 argument as a single element of sys.argv.

 On Unix, I use

arg = --title=%s % pipes.quote(title)

 to achieve that effect.  What's the equivalent on Windows?  Since
 cmd.exe also supports pipelines, I'd sort of expect it to do the right
 thing on Windows, too.
   

Ah, foolish mortal.

The problem with Windows is that, as I said, there is no central
command-line parser, so there is no single set of rules.  Cmd.exe does
not parse the command line.  Cmd.exe does not create argc/argv.  Cmd.exe
just passes the whole command line as a string.  For C programs, the C
run-time library will crack that string into argc/argv.  Python on
Windows has its own parser.

If you are creating a list of arguments to pass AS A LIST, then no
quoting is necessary.  If you intend to mash them together into a single
string, then I'm afraid you will need to experiment.

-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.

___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] does pipes.quote() work properly on Windows?

2010-05-24 Thread Bill Janssen
Tim Roberts t...@probo.com wrote:

 Bill Janssen wrote:
  I'm actually not passing file names.  I'm passing argument strings,
  which may contain spaces, quotes, and other things.  For instance,
 
 myprogram --title=That's the game! says Mike Hammer Brotsky 
  --file=...
 
  myprogram is a Python program, and expects to get the whole --title
  argument as a single element of sys.argv.
 
  On Unix, I use
 
 arg = --title=%s % pipes.quote(title)
 
  to achieve that effect.  What's the equivalent on Windows?  Since
  cmd.exe also supports pipelines, I'd sort of expect it to do the right
  thing on Windows, too.

 
 Ah, foolish mortal.

Quixotic, perhaps, to attempt to achieve consistency on Windows...
Actually, my last sentence there was more about, Why doesn't the pipes
module also work on Windows?, than it was about quoting.

 The problem with Windows is that, as I said, there is no central
 command-line parser, so there is no single set of rules.  Cmd.exe does
 not parse the command line.  Cmd.exe does not create argc/argv.  Cmd.exe
 just passes the whole command line as a string.  For C programs, the C
 run-time library will crack that string into argc/argv.  Python on
 Windows has its own parser.

The subprocess module has an idea of how it should work.  Whether or not
it's correct is a different matter.  So, is the algorithm the same for
the visual studio C runtime's parser, and Python's parser?  I'd expect
that, since CPython is a C program that uses VS C.  If so, I'd be happy
to settle for that standard.

The algorithm in the subprocess module is documented thusly:

Translate a sequence of arguments into a command line
string, using the same rules as the MS C runtime:

1) Arguments are delimited by white space, which is either a
   space or a tab.

2) A string surrounded by double quotation marks is
   interpreted as a single argument, regardless of white space
   or pipe characters contained within.  A quoted string can be
   embedded in an argument.

3) A double quotation mark preceded by a backslash is
   interpreted as a literal double quotation mark.

4) Backslashes are interpreted literally, unless they
   immediately precede a double quotation mark.

5) If backslashes immediately precede a double quotation mark,
   every pair of backslashes is interpreted as a literal
   backslash.  If the number of backslashes is odd, the last
   backslash escapes the next double quotation mark as
   described in rule 3.


# See
# http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
# or search http://msdn.microsoft.com for
# Parsing C++ Command-Line Arguments

Bill
___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] does pipes.quote() work properly on Windows?

2010-05-23 Thread Waldemar Osuch
On Fri, May 21, 2010 at 15:03, Bill Janssen jans...@parc.com wrote:
 Anyone know if the quote() function in the pipes module does the right
 thing for cmd.exe pipes?

 If not, what is the right thing?

twisted.python.win32 has a couple functions that try very hard to get
the quoting right.
http://twistedmatrix.com/documents/current/api/twisted.python.win32.html#quoteArguments
___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] does pipes.quote() work properly on Windows?

2010-05-22 Thread Greg Ewing

Bill Janssen wrote:


   myprogram --title=That's the game! says Mike Hammer Brotsky --file=...



Since
cmd.exe also supports pipelines, I'd sort of expect it to do the right
thing on Windows, too.


Don't know about later versions, but in Python 2.5 the pipes
module is listed under Unix specific services, so I guess
it was never designed with Windows in mind.

The reason it fails on Windows is that it assumes single
quotes can be used to quote a string containing double
quotes. But Windows usually requires double quotes around
arguments, so you will have to escape the inner quotes:

--title=That's the game! says Mike \Hammer\ Brotsky

A quick test suggests that this will work, at least in the
case where the program being passed the args is a Python
program.

--
Greg
___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] does pipes.quote() work properly on Windows?

2010-05-21 Thread Tim Roberts
Bill Janssen wrote:
 Anyone know if the quote() function in the pipes module does the right
 thing for cmd.exe pipes?
   

No, it doesn't.  It uses sh rules, which aren't the same.

 If not, what is the right thing?

Unfortunately, command line parsing in Windows is not centralized in the
shell.  To satisfy cmd.exe, you can safely surround each file name in
double quotes.  Whether the application knows how to handle that or not
is up to the application (although most do it correctly).  Remember that
this is only necessary when passing names to the command shell.  The
Windows APIs don't want the quotes.

-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.

___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] does pipes.quote() work properly on Windows?

2010-05-21 Thread Bill Janssen
Tim Roberts t...@probo.com wrote:

 Bill Janssen wrote:
  Anyone know if the quote() function in the pipes module does the right
  thing for cmd.exe pipes?

 
 No, it doesn't.  It uses sh rules, which aren't the same.
 
  If not, what is the right thing?
 
 Unfortunately, command line parsing in Windows is not centralized in the
 shell.  To satisfy cmd.exe, you can safely surround each file name in
 double quotes.  Whether the application knows how to handle that or not
 is up to the application (although most do it correctly).  Remember that
 this is only necessary when passing names to the command shell.  The
 Windows APIs don't want the quotes.

I'm actually not passing file names.  I'm passing argument strings,
which may contain spaces, quotes, and other things.  For instance,

   myprogram --title=That's the game! says Mike Hammer Brotsky --file=...

myprogram is a Python program, and expects to get the whole --title
argument as a single element of sys.argv.

On Unix, I use

   arg = --title=%s % pipes.quote(title)

to achieve that effect.  What's the equivalent on Windows?  Since
cmd.exe also supports pipelines, I'd sort of expect it to do the right
thing on Windows, too.

Bill
___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32