classification
Title: doc: Clarify difference between os.system/subprocess.call in section "Replacing os.system()"
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: AndiDog, docs@python, iritkatriel, martin.panter, miss-islington, orsenthil, uniocto
Priority: normal Keywords: easy, patch

Created on 2015-03-23 14:28 by AndiDog, last changed 2021-05-11 20:55 by orsenthil. This issue is now closed.

Files
File name Uploaded Description Edit
system-subprocess.patch martin.panter, 2015-06-22 06:32 review
Pull Requests
URL Status Linked Edit
PR 26016 merged uniocto, 2021-05-10 12:32
PR 26040 merged miss-islington, 2021-05-11 20:47
PR 26041 merged miss-islington, 2021-05-11 20:47
Messages (8)
msg239028 - (view) Author: Andreas Sommer (AndiDog) * Date: 2015-03-23 14:28
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.
msg239087 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-03-24 01:52
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]
msg245620 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-06-22 06:32
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.
msg392242 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-04-28 18:09
The patch needs to be converted to a github PR.
msg393381 - (view) Author: So Ukiyama (uniocto) * Date: 2021-05-10 12:36
I created a PR which apply Martin Panter's patch.

So If I have offended you with my rudeness, I hope you will forgive me for taking this down.
msg393469 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2021-05-11 18:30
Does anyone know what the return value 768 signify here?
msg393474 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2021-05-11 20:55
New changeset 6fc6f4366d02412e3424d2a6da43a28d8f479d7b by Miss Islington (bot) in branch '3.10':
bpo-23750: Document os-system, subprocess. Patch by Martin Panter. (GH-26016) (GH-26040)
https://github.com/python/cpython/commit/6fc6f4366d02412e3424d2a6da43a28d8f479d7b
msg393475 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2021-05-11 20:55
New changeset 390bfe044531a813722919933116ed37fe321861 by Miss Islington (bot) in branch '3.9':
bpo-23750: Document os-system, subprocess. Patch by Martin Panter. (GH-26016) (GH-26041)
https://github.com/python/cpython/commit/390bfe044531a813722919933116ed37fe321861
History
Date User Action Args
2021-05-11 20:55:39orsenthilsetstatus: open -> closed
stage: patch review -> resolved
2021-05-11 20:55:27orsenthilsetmessages: + msg393475
2021-05-11 20:55:21orsenthilsetmessages: + msg393474
2021-05-11 20:47:26miss-islingtonsetpull_requests: + pull_request24686
2021-05-11 20:47:21miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24685
2021-05-11 18:30:03orsenthilsetnosy: + orsenthil
messages: + msg393469
2021-05-10 12:36:04unioctosetmessages: + msg393381
2021-05-10 12:32:34unioctosetnosy: + uniocto
pull_requests: + pull_request24666
2021-04-28 18:09:29iritkatrielsetnosy: + iritkatriel
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.4, Python 3.5, Python 3.6
messages: + msg392242

keywords: + easy
title: Clarify difference between os.system/subprocess.call in section "Replacing os.system()" -> doc: Clarify difference between os.system/subprocess.call in section "Replacing os.system()"
2015-06-22 06:32:54martin.pantersetfiles: + system-subprocess.patch
versions: + Python 2.7, Python 3.4, Python 3.5, Python 3.6
messages: + msg245620

keywords: + patch
stage: patch review
2015-03-24 01:52:35martin.pantersetnosy: + martin.panter
messages: + msg239087
2015-03-23 14:28:11AndiDogcreate