commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon
I'm running a command like

import commands
result = commands.getstatusoutput('somecommand')
print result[0]
3072


However, this exit code made no sense so I ran it manually from the 
command line in bash on my linux server and it gives the exit code as 
12, not this weird 3072 number.

So I tried os.system('somecommand') in the interactive python shell and 
it too returned the same result for the exit code as the unix shell, 12, 
but re-running the commands.getstatusoutput() with the exact same 
command still gave 3072.


Is commands.getstatusoutput() broken or something?


-h

-- 
Hari Sekhon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Steve Holden
Hari Sekhon wrote:
 I'm running a command like
 
 import commands
 result = commands.getstatusoutput('somecommand')
 print result[0]
 3072
 
 
 However, this exit code made no sense so I ran it manually from the 
 command line in bash on my linux server and it gives the exit code as 
 12, not this weird 3072 number.
 
 So I tried os.system('somecommand') in the interactive python shell and 
 it too returned the same result for the exit code as the unix shell, 12, 
 but re-running the commands.getstatusoutput() with the exact same 
 command still gave 3072.
 
 
 Is commands.getstatusoutput() broken or something?
 
 
 -h
 
No, it's just returning the error code in the top half of a sixteen-bit 
value. You will notice that 3072 == 2 * 256.

That's always been the way the Unix return code has been returned 
programattically, but the shell shifts it down to make it more usab;e.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon




I don't quite understand what you are saying here:

2 * 256 is 512,
2 ** 256 is some extremely large number.

2**12 is 4096.

So how does 3072 factor into this?

Could you explain what you mean by "the error in the top half of a
sixteen-bit value"?

This makes no sense to me at this moment.

-h

Hari Sekhon


Steve Holden wrote:

  Hari Sekhon wrote:
  
  
I'm running a command like

import commands
result = commands.getstatusoutput('somecommand')
print result[0]
3072


However, this exit code made no sense so I ran it manually from the 
command line in bash on my linux server and it gives the exit code as 
12, not this weird 3072 number.

So I tried os.system('somecommand') in the interactive python shell and 
it too returned the same result for the exit code as the unix shell, 12, 
but re-running the commands.getstatusoutput() with the exact same 
command still gave 3072.


Is commands.getstatusoutput() broken or something?


-h


  
  No, it's just returning the error code in the top half of a sixteen-bit 
value. You will notice that 3072 == 2 * 256.

That's always been the way the Unix return code has been returned 
programattically, but the shell shifts it down to make it more usab;e.

regards
  Steve
  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: *** Spam *** Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Steve Holden
A famous Holden typo - it should have been 12 * 256 == 3072, but 
really it shouldn't have been beyond you to perform a division of 3072 
by 12 (given that you already knew the number 12 was potentially involved).

Basically the value you want is shifted up 8 bits. Perhaps I should more 
understandably have said:

   12  8 == 3072

regards
  Steve

Hari Sekhon wrote:
 I don't quite understand what you are saying here:
 
 2 * 256 is 512,
 2 ** 256 is some extremely large number.
 
 2**12 is 4096.
 
 So how does 3072 factor into this?
 
 Could you explain what you mean by the error in the top half of a 
 sixteen-bit value?
 
 This makes no sense to me at this moment.
 
 -h
 
 Hari Sekhon
 
 
 
 Steve Holden wrote:
 
Hari Sekhon wrote:
  

I'm running a command like

import commands
result = commands.getstatusoutput('somecommand')
print result[0]
3072


However, this exit code made no sense so I ran it manually from the 
command line in bash on my linux server and it gives the exit code as 
12, not this weird 3072 number.

So I tried os.system('somecommand') in the interactive python shell and 
it too returned the same result for the exit code as the unix shell, 12, 
but re-running the commands.getstatusoutput() with the exact same 
command still gave 3072.


Is commands.getstatusoutput() broken or something?


-h



No, it's just returning the error code in the top half of a sixteen-bit 
value. You will notice that 3072 == 2 * 256.

That's always been the way the Unix return code has been returned 
programattically, but the shell shifts it down to make it more usab;e.

regards
  Steve
  



-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon
I'm sorry, this may seem dense to you but I have to ask. What on earth 
are you talking about?

Why is it shifted 8 bits to the left? Why is there bitshifting at all? 
Why doesn't commands give the same exit value as os.system() and the 
unix cli?

When you choose to exit a program you give it a return value to exit 
with, so why would this change, I exit with the number 1 then expect 
that number to be the exit code, right? Where do these higher numbers 
come into the equation and why?

Please assume that I am not a mind reader and require explanation before 
I can understand.

Perhaps you aren't a mind reader either and don't know why the writers 
of the commands lib chose to do this insanity either? It seems the 
os.system() guys did the right thing, I wonder why 
commands.getstatusoutput doesn't...

Having just tested it manually with a shell script returning 2, 
commands.getstatusoutput did give the exit code as 512, so it does seem 
to generically shift the exit code 8 bits to the left or multiply it by 
256 for those of us who need some more straight talking...

ugg, perhaps it's time to stop using this thing and use a better lib module.

any explanations welcome...

-h

Hari Sekhon



Steve Holden wrote:
 A famous Holden typo - it should have been 12 * 256 == 3072, but 
 really it shouldn't have been beyond you to perform a division of 3072 
 by 12 (given that you already knew the number 12 was potentially 
 involved).

 Basically the value you want is shifted up 8 bits. Perhaps I should 
 more understandably have said:

   12  8 == 3072

 regards
  Steve

 Hari Sekhon wrote:
 I don't quite understand what you are saying here:

 2 * 256 is 512,
 2 ** 256 is some extremely large number.

 2**12 is 4096.

 So how does 3072 factor into this?

 Could you explain what you mean by the error in the top half of a 
 sixteen-bit value?

 This makes no sense to me at this moment.

 -h

 Hari Sekhon



 Steve Holden wrote:

 Hari Sekhon wrote:
  

 I'm running a command like

 import commands
 result = commands.getstatusoutput('somecommand')
 print result[0]
 3072


 However, this exit code made no sense so I ran it manually from the 
 command line in bash on my linux server and it gives the exit code 
 as 12, not this weird 3072 number.

 So I tried os.system('somecommand') in the interactive python shell 
 and it too returned the same result for the exit code as the unix 
 shell, 12, but re-running the commands.getstatusoutput() with the 
 exact same command still gave 3072.


 Is commands.getstatusoutput() broken or something?


 -h

   
 No, it's just returning the error code in the top half of a 
 sixteen-bit value. You will notice that 3072 == 2 * 256.

 That's always been the way the Unix return code has been returned 
 programattically, but the shell shifts it down to make it more usab;e.

 regards
  Steve
  



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Fredrik Lundh
Hari Sekhon wrote:

 I'm sorry, this may seem dense to you but I have to ask. What on earth 
 are you talking about?
 
 Why is it shifted 8 bits to the left? Why is there bitshifting at all? 
 Why doesn't commands give the same exit value as os.system() and the 
 unix cli?

because that's how Unix's wait() operation returns the status code (as 
mentioned in the commands section of the library reference).

you can use the os.WIFEXITED(status) and os.WEXITSTATUS(code) helpers to 
convert between wait return codes and signal numbers/exit codes.  see:

 http://docs.python.org/lib/os-process.html

or you can forget about the obsolete commands module, and use the new 
subprocess module instead; e.g.

def getstatusoutput(command):
 from subprocess import Popen, PIPE, STDOUT
 p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
 s = p.stdout.read()
 return p.wait(), s

print getstatusoutput(ls -l /bin/ls)
(0, '-rwxr-xr-x1 root root68660 Aug 12  2003 /bin/ls\n')

the subprocess module is highly flexible; see the library reference for 
details.

/F

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Martin v. Löwis
Hari Sekhon schrieb:
 I'm sorry, this may seem dense to you but I have to ask. What on earth
 are you talking about?

You may not be dense, but you are certainly fairly aggressive in your
postings. If you just want to complain, go ahead. If you want actual
help, you should reconsider your tone.

 Why is it shifted 8 bits to the left?

As Steve says, that what the system returns (from popen(3)), in this
case. It's the true exit status, not just the exit code.

 Why is there bitshifting at all?

Read some Unix book.

 Why doesn't commands give the same exit value as os.system() and the
 unix cli?

Read some Unix book.

 When you choose to exit a program you give it a return value to exit
 with, so why would this change, I exit with the number 1 then expect
 that number to be the exit code, right?

Read some Unix book. Hint: how do you know whether the program was
killed, and didn't actually invoke exit(3)?

 Where do these higher numbers
 come into the equation and why?

Read some Unix book. Hint: Read about WIFEXITED, WIFSIGNALED, and
WEXITSTATUS.

 Please assume that I am not a mind reader and require explanation before
 I can understand.

You are apparently not a documentation reader, as well. Nobody owes you
an explanation.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon




ok, I was thinking of shifting using subprocess, guess I'd better do
that and forget about this waste of time.

thanks

Hari Sekhon


Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
I'm sorry, this may seem dense to you but I have to ask. What on earth 
are you talking about?

  
   
  
  
Why is it shifted 8 bits to the left? Why is there bitshifting at all? 
Why doesn't commands give the same exit value as os.system() and the 
unix cli?

  
  
because that's how Unix's wait() operation returns the status code (as 
mentioned in the "commands" section of the library reference).

you can use the os.WIFEXITED(status) and os.WEXITSTATUS(code) helpers to 
convert between wait return codes and signal numbers/exit codes.  see:

 http://docs.python.org/lib/os-process.html

or you can forget about the obsolete "commands" module, and use the new 
subprocess module instead; e.g.

def getstatusoutput(command):
 from subprocess import Popen, PIPE, STDOUT
 p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
 s = p.stdout.read()
 return p.wait(), s

print getstatusoutput("ls -l /bin/ls")
(0, '-rwxr-xr-x1 root root68660 Aug 12  2003 /bin/ls\n')

the subprocess module is highly flexible; see the library reference for 
details.

/F

  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Scott David Daniels
Steve Holden wrote:
 Hari Sekhon wrote:
 I'm running a command like

 import commands
 result = commands.getstatusoutput('somecommand')
 print result[0]
 3072
...
 No, it's just returning the error code in the top half of a sixteen-bit 
 value. You will notice that 3072 == 2 * 256.
For the rest of us playing along at home, there is a typo there:
The preceding line should read:
  value. You will notice that 3072 == 12 * 256.

--Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon




yes already noted by Steve, thanks. 

I should have spotted that myself straight away but I was too wrapped
up in this whole "I didn't realise there were 2 sets of numbers" thing,
gotta go read some unix programming books it would seem this is a os
function that I am not aware of. 

I still reserve the right to be annoyed at commands for not hiding this
from me like everything else, but then /F is right (as always
it would seem) I should not be using such a deprecated thing like
commands, I will switch to subprocess...

I'm even more surprised since I do so much shell scripting and I've
never even heard of this thing before, I guess only the really
battle-scarred old skool ones may know of it.

-h
Hari Sekhon


Scott David Daniels wrote:

  Steve Holden wrote:
  
  
Hari Sekhon wrote:


  I'm running a command like

import commands
result = commands.getstatusoutput('somecommand')
print result[0]
3072
  

  
  ...
  
  
No, it's just returning the error code in the top half of a sixteen-bit 
value. You will notice that 3072 == 2 * 256.

  
  For the rest of us playing along at home, there is a typo there:
The preceding line should read:
  value. You will notice that 3072 == 12 * 256.

--Scott David Daniels
[EMAIL PROTECTED]
  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Donn Cave
In article [EMAIL PROTECTED],
 Steve Holden [EMAIL PROTECTED] wrote:

 Basically the value you want is shifted up 8 bits. Perhaps I should more 
 understandably have said:
 
12  8 == 3072

Or as already suggested in other followups, use os.WEXITSTATUS
(and os.WIFEXITED.)  Not only does this do more precisely the
right thing, it will work on any platform that supports a
POSIX wait -- which doesn't require that exit == status  8,
only that WEXITSTATUS be able to return that value.

   Donn Cave, [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list