Python Forum
which exception should be raised if ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which exception should be raised if ...
#1
which exception should be raised if a process being run produces output to a pipe normally connected to read its stdout and/or stderr when no output is expected from that process?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
BrokenPipeError

The mro:
BrokenPipeError.mro()
[BrokenPipeError, ConnectionError, OSError, Exception, BaseException, object]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Mar-27-2023, 05:17 PM)Skaperen Wrote: a pipe normally connected to read its stdout and/or stderr when no output is expected from that process?
If no output from that process is expected, why do you connect a pipe to receive output? It looks that you are expecting some output under abnormal circumstances and you want to detect if these special circumstances occur. This is very different from expecting no output. I think you should clarify your intention.
Reply
#4
(Mar-28-2023, 01:30 PM)Gribouillis Wrote:
(Mar-27-2023, 05:17 PM)Skaperen Wrote: a pipe normally connected to read its stdout and/or stderr when no output is expected from that process?
If no output from that process is expected, why do you connect a pipe to receive output? It looks that you are expecting some output under abnormal circumstances and you want to detect if these special circumstances occur. This is very different from expecting no output. I think you should clarify your intention.
the pipe is connected so that it can be determined if the process did produce output. that process also needs to not be interrupted by that situation. that process needs to go on running. when it is finally all done this function (which, in other cases, is expected to return data) will raise an exception as a way to pass error conditions (there are other). if the pipe is not connected, then it will be very difficult to detect if the process did produce any output.

the caller of this function, when specifying that no output is expected, may or may not consider that any output is an abnormal case. the function will not care. any output will be discarded just as if the process has been given a stdout open to write to file named in os.devnull.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Mar-29-2023, 02:19 AM)Skaperen Wrote: when it is finally all done this function (which, in other cases, is expected to return data) will raise an exception as a way to pass error conditions
I think it is best to create your own custom exception in this case
import subprocess as sp

def call_spam():
    proc = sp.run(['spam',], check=True, stdout=sp.PIPE, stderr=sp.STDOUT)
    if proc.stdout:
        raise SpamProducedOutput(proc.stdout)

class SpamProducedOutput(RuntimeError):
    pass
Skaperen likes this post
Reply
#6
i was thinking of just using RuntimeError but this makes more sense and the caller of my function can still expect RuntimeError if that makes sense to them.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020