New GitHub issue #92490 from teichopsia:<br>

<hr>

<pre>
cpython 3.6.9/3.9.1 is swallowing BrokenPipeError in default TextIOWrapper. 

other issues like this are because the print is invoked without a catch block. I have one so...

$ lsb_release -d
Description:    Ubuntu 18.04.5 LTS
$ python3 -V
Python 3.6.9


## simple test case
$ python3 -c 'try: print("hi")                
except: pass'|false;echo ${PIPESTATUS[*]}                                                                     
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
120 1


## naive workaround seems to work... until i use it elsewhere
$ python3 -c 'try: print("hi",file=sys.stdout)
except: pass'|false;echo ${PIPESTATUS[*]}
0 1


## suggested 3.7 exit strategy
$ python3 -c 'try: print("hi")
except: os.dup2(os.open(os.devnull,os.O_WRONLY),sys.stdout.fileno());sys.exit(22)'|false;echo ${PIPESTATUS[*]}
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
120 1


# suggested 3.7 with file workaround
$ python3 -c 'try: print("hi",file=sys.stdout)
except: os.dup2(os.open(os.devnull,os.O_WRONLY),sys.stdout.fileno());sys.exit(22)'|false;echo ${PIPESTATUS[*]}
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'sys' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
NameError: name 'os' is not defined
1 1


## looks almost fixed! lets import os/sys to resolve and...

$ python3 -c 'import sys,os
> try: print("hi",file=sys.stdout)
except: os.dup2(os.open(os.devnull,os.O_WRONLY),sys.stdout.fileno());sys.exit(22)'|false;echo ${PIPESTATUS[*]}
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
120 1



so clearly at no point is my exception handler even getting a chance because it is swallowed and bailed.

how can I workaround this with some magic io change? sorry I only had those two versions to test with. I am too tired to grok why specifying a file argument sometimes works. I assume it is because it is racing the exit of the false and winning sometimes but haven't proven it.
</pre>

<hr>

<a href="https://github.com/python/cpython/issues/92490">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>