Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a new subprocess.call which raises an error on non-zero rc #41218

Closed
ncw mannequin opened this issue Nov 23, 2004 · 5 comments
Closed

a new subprocess.call which raises an error on non-zero rc #41218

ncw mannequin opened this issue Nov 23, 2004 · 5 comments
Labels
stdlib Python modules in the Lib dir

Comments

@ncw
Copy link
Mannequin

ncw mannequin commented Nov 23, 2004

BPO 1071764
Files
  • xcall.patch: patch for subprocess to implement raising call
  • xcall.patch: Adapted patch against trunk
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2005-01-01.09:37:48.000>
    created_at = <Date 2004-11-23.14:45:22.000>
    labels = ['library']
    title = 'a new subprocess.call which raises an error on non-zero rc'
    updated_at = <Date 2005-01-01.09:37:48.000>
    user = 'https://bugs.python.org/ncw'

    bugs.python.org fields:

    activity = <Date 2005-01-01.09:37:48.000>
    actor = 'astrand'
    assignee = 'astrand'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2004-11-23.14:45:22.000>
    creator = 'ncw'
    dependencies = []
    files = ['6373', '6374']
    hgrepos = []
    issue_num = 1071764
    keywords = ['patch']
    message_count = 5.0
    messages = ['47326', '47327', '47328', '47329', '47330']
    nosy_count = 2.0
    nosy_names = ['astrand', 'ncw']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1071764'
    versions = ['Python 2.5']

    @ncw
    Copy link
    Mannequin Author

    ncw mannequin commented Nov 23, 2004

    The attached patch introduces a 3rd utility function -
    xcall() to the
    subprocess module. This function acts just like call
    but raises
    errors if the command had a non-zero return code.

    This saves writing

    if call(...):
        raise OSError(...)

    It is most useful for shell script replacement
    programming. Checking
    the return codes of commands called is often forgotten
    in shell
    programming.

    When you've moved up to python because shell is too
    limiting (usually
    about 10 lines of shell in my case ;-) you want to make
    sure that all
    your commands work so you write robust code.

    I consider raising an exception to be much more
    pythonic than checking
    a return code (ie "Errors should never pass silently"
    from Zen of
    Python)

    Eg

    # An easy to miss error

    >>> subprocess.call(["mkdir", "a/b"])
    mkdir: cannot create directory `a/b': No such file or
    directory
    1
    >>> # user forgot to check non-zero return code

    # becomes an impossible to miss exception

    >>> subprocess.xcall(["mkdir", "a/b"])
    mkdir: cannot create directory `a/b': No such file or
    directory
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "subprocess.py", line 462, in xcall
        raise CalledProcessError(rc, "Command %s returned
    non zero exit status" % args[0])
    subprocess.CalledProcessError: [Errno 1] Command
    ['mkdir', 'a/b'] returned non zero exit status
    >>> 

    See attached patch for more! Its been tested under
    python 2.3 on
    windows and linux.

    @ncw ncw mannequin closed this as completed Nov 23, 2004
    @ncw ncw mannequin assigned astrand Nov 23, 2004
    @ncw ncw mannequin added the stdlib Python modules in the Lib dir label Nov 23, 2004
    @ncw ncw mannequin closed this as completed Nov 23, 2004
    @ncw ncw mannequin assigned astrand Nov 23, 2004
    @ncw ncw mannequin added the stdlib Python modules in the Lib dir label Nov 23, 2004
    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Dec 1, 2004

    Logged In: YES
    user_id=344921

    Since this is a new feature, the patch will go into trunk,
    but not the 2.4 maint branch.

    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Dec 5, 2004

    Logged In: YES
    user_id=344921

    My suggested name is "check_call".

    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Dec 13, 2004

    Logged In: YES
    user_id=344921

    If there are no objections, I will commit the "Adapted patch
    against trunk".

    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Jan 1, 2005

    Logged In: YES
    user_id=344921

    Patch applied. New revisions:

    subprocess.py 1.12
    test_subprocess.py 1.17
    libsubprocess.tex 1.5

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants