Image

Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Concise way to capture stderr output in an exception when calling a process from Python

+2
−0

I’d like to call an external executable from a Python script. If it exits with a nonzero code, I’d like an exception containing the return code and the stderr output. The exception will be caught and logged by the UncaughtExceptionHook. The subprocess.check_call(…) would be nice and concise, but it didn’t add the stderr output to the subprocess.CalledProcessError exception.

subprocess.check_call(
    ["wget.exe", 
        "--page-requisites", "--span-hosts", "--convert-links",
        sp_url.url],
    text=True)``

Here’s a snippet which explicitly generates subprocess.CalledProcessError exception with stderr. It works, but it’s more verbose.

with subprocess.Popen(
    ["wget.exe", "--page-requisites", "--span-hosts", "--convert-links",
        sp_url.url],
    stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
) as proc:
    try:
        (stdout, stderr) = proc.communicate(timeout=60)
    except (subprocess.TimeoutExpired, subprocess.CalledProcessError) as exc:
        proc.kill()
        raise   # Re-throw the exception.  The unhandled exception hook will log it, then sys.exit() the script.

if proc.returncode != 0:
    raise subprocess.CalledProcessError(proc.returncode, proc.args, output=stdout, stderr=stderr)     # the unhandled exception hook (catch-all) will log the exception and exit

Here’s my exception logging code.

def excepthook(self, exc_type, exc_value, exc_traceback):

    exc_attributes = ""
    for name, value in vars(exc_value).items():
        exc_attributes += f"{name}: {value}, "

    self._logger.exception(f"Uncaught exception hook.  {repr(exc_value)},  {exc_attributes} ", 
                           exc_info=(exc_type, exc_value, exc_traceback))
    sys.exit(1)

edit:

Come to think of it, I could create a helper function out of the verbose variant. It would be concise from the calling code's point of view.

In the event of success, do you need the stdout captured, or are the side effects of the process sufficient? [from comments]

Someday I may want to call a process and capture its stdout. That's a general case. For the processes which I'm actually calling at this time, if the process completes successfully, then I don't need the stdout. I only need files (side effects) created by the process.

Is it necessary from your perspective to impose a timeout, or is that just incidental to the fact that your current solution uses .communicate ? [from comments]

The timemout argument is just incidental because I'm calling .communicate.

History

1 comment thread

Exact requirements (2 comments)

Sign up to answer this question »