[issue23750] Clarify difference between os.system/subprocess.call in section "Replacing os.system()"
New submission from Andreas Sommer:
Reading over the section "Replacing os.system()" (https://docs.python.org/2/library/subprocess.html#replacing-os-system), one might assume that the return value of os.system and subprocess.call are equivalent.
status = os.system("mycmd" + " myarg")
# becomes
status = subprocess.call("mycmd" + " myarg", shell=True)
However, they are not. Example:
import sys
import os
import subprocess
print subprocess.call("false")
print os.system("false")
gives 1 and 256, respectively. Maybe this could be rephrased for clarity, or a hint added.
----------
assignee: docs@python
components: Documentation
messages: 239028
nosy: Andreas Sommer, docs@python
priority: normal
severity: normal
status: open
title: Clarify difference between os.system/subprocess.call in section "Replacing os.system()"
type: enhancement
_______________________________________
Python tracker
Martin Panter added the comment:
Another difference is that (at least POSIX) system() blocks SIGINT and SIGQUIT while the child is running. Compare:
$ python3 -c 'print("Waiting"); import os; print("Returned", os.system("sleep 3 && exit 3"))'
Sleeping
Returned 768
$ python3 -c 'print("Sleeping"); import os; print("Returned", os.system("sleep 3 && exit 3"))'
Waiting
^CReturned 2 # Hit Ctrl+C during sleep command
$ python3 -c 'print("Sleeping"); import subprocess; print("Returned", subprocess.call("sleep 3 && exit 3", shell=True))'
Sleeping
Returned 3
$ python3 -c 'print("Sleeping"); import subprocess; print("Returned", subprocess.call("sleep 3 && exit 3", shell=True))'
Sleeping
^CTraceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.4/subprocess.py", line 539, in call
return p.wait(timeout=timeout)
File "/usr/lib/python3.4/subprocess.py", line 1566, in wait
(pid, sts) = self._try_wait(0)
File "/usr/lib/python3.4/subprocess.py", line 1514, in _try_wait
(pid, sts) = _eintr_retry_call(os.waitpid, self.pid, wait_flags)
File "/usr/lib/python3.4/subprocess.py", line 491, in _eintr_retry_call
return func(*args)
KeyboardInterrupt
[Exit 1]
----------
nosy: +vadmium
_______________________________________
Python tracker
Martin Panter added the comment:
Here is a patch:
* Use different return value variable names and point out that they are encoded differently
* Add another bullet point about signal handling
* Fix os.system() documentation of the return value. My understanding is it is the C standard that does not define the return value, and Posix essentially means Unix.
----------
keywords: +patch
stage: -> patch review
versions: +Python 2.7, Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file39767/system-subprocess.patch
_______________________________________
Python tracker
Irit Katriel
participants (3)
-
Andreas Sommer
-
Irit Katriel
-
Martin Panter