<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Sat, Jun 30, 2018 at 9:02 PM Alfred Perlstein <<a href="mailto:alfred@freebsd.org">alfred@freebsd.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
On 6/30/18 4:20 PM, Greg Ewing wrote:<br>
> Alfred Perlstein wrote:<br>
>> I am asking if there's a way we can discourage the use of <br>
>> "signal(SIGPIPE, SIG_DFL)" unless the user really understands what <br>
>> they are doing.<br>
><br>
> Maybe there's some way that SIGPIPEs on stdout could be handled<br>
> differently by default, so that they exit silently instead of<br>
> producing an ugly message. That would remove the source of pain<br>
> that's leading people to do this.<br>
><br>
Thank you Greg, I can poke around into this, it would be a bit of a <br>
challenge as the descriptor which causes BrokenPipeError does not appear <br>
to be stored within the exception so differentiating it from other <br>
exceptions might be a bit tricky.<br>
<br>
I will look into this in the coming weeks.  Any tips on accomplishing <br>
this?  I was thinking of encoding the fd responsible for causing the <br>
error into the exception somehow and then checking to see if it was <br>
stdout, then not reporting on it.<br></blockquote></div><div><br></div><div>There's PyErr_SetFromErrnoWithFilenameObject(), which generates an OSError with a filename. We could have a similar call PyErr_SetFromErrnoWithFileDescriptor(), which generates an OSError (or a subclass like BrokenPipeError) with a file descriptor. Just pick a new attribute name to store the fd on, e.g. file_descriptor (I guess it would default to None). Then of course you will have to write the code that calls this instead of plain PyErr_SetFromErrno() for those syscalls where a file descriptor is present.</div><div><br></div><div>And when Python exits with a BrokenPipeError it could suppress printing the stack trace when the file_descriptor field equals 1.<br></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)</div></div>