0

I'm creating a hardening tool for popular versions of linux (RHEL, opensuse, ubuntu, etc.) I'm currently testing my code in an OpenSUSE environment using the zypper package manager.

I have this snippet of code:

message = subprocess.Popen(["/usr/bin/zypper","-n","install","vlock"],
  stdout=subprocess.PIPE,shell = False) 
details = message.stdout.readlines()
print message.poll()

This will install the package, in this case vlock, and then read in the output from the shell, and then print the success value which will either be a 0 or nonzero number. when I run this code in the python interpreter I can either print message.poll() or echo $? if I run the command outside of the interpreter and I get a 0 for success or a nonzero for failure as expected.

However when I actually run the full unit-test script from the shell:

sudo ./pkghelper.py  

I print out the message.poll() value which is supposed to hold the same returned value from the finished process as either printing message.poll() or echo $? however instead of the value being an integer of 0 or nonzero, I get a value of None. Does anyone know why the value in the python interpreter is an integer and the value when running the script is None?!?! this is actually a major hinderance in the continuation of the development of the program and I can find anything online

larsks
  • 277,717
  • 41
  • 399
  • 399
Derek Dub
  • 53
  • 2
  • 11

1 Answers1

1

The value is None when the process is running and it will be a 0 if it exited without error. Since you are calling it in the interpreter, the process has already finished and exited with a return code of 0. But, when you run it as a script, python keeps going faster than the call finishes(and it doesn't wait on the call), so it gets a None, signifying that the process is still running. The subprocess docs explain this.

underbar
  • 588
  • 3
  • 15