Silently handle broken pipes
Two errors could occur when using a command that break the pipe on which
colout is supposed to write, like head or tail:
ls | colout . red | head -n 1
This fix set the SIGPIPE handler to kill the program instead of ending
in a write error when a broken pipe occurs and silently handle broken
pipes IOError exceptions.
This commit is contained in:
parent
f1bbd9eb16
commit
a404aabada
1 changed files with 16 additions and 2 deletions
|
|
@ -13,6 +13,12 @@ import glob
|
||||||
import math
|
import math
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
|
import signal
|
||||||
|
|
||||||
|
# set the SIGPIPE handler to kill the program instead of
|
||||||
|
# ending in a write error when a broken pipe occurs
|
||||||
|
signal.signal( signal.SIGPIPE, signal.SIG_DFL )
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Ressource parsing helpers
|
# Ressource parsing helpers
|
||||||
|
|
@ -524,8 +530,16 @@ def write(colored, stream = sys.stdout):
|
||||||
"""
|
"""
|
||||||
Write "colored" on sys.stdout, then flush.
|
Write "colored" on sys.stdout, then flush.
|
||||||
"""
|
"""
|
||||||
stream.write(colored)
|
try:
|
||||||
stream.flush()
|
stream.write(colored)
|
||||||
|
stream.flush()
|
||||||
|
|
||||||
|
# Silently handle broken pipes
|
||||||
|
except IOError:
|
||||||
|
try:
|
||||||
|
stream.close()
|
||||||
|
except IOError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def map_write( stream_in, stream_out, function, *args ):
|
def map_write( stream_in, stream_out, function, *args ):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue