Skip to content

Instantly share code, notes, and snippets.

@brendano
Created April 23, 2009 07:09
Show Gist options
  • Save brendano/100368 to your computer and use it in GitHub Desktop.
Save brendano/100368 to your computer and use it in GitHub Desktop.
# from anyall.org/util.py
######### Make UTF-8 hurt less
# My rant about pre-py3k encoding handling
# They like to say, always use unicode internally, then decode/encode at I/O boundaries
# That's good once you've accomplished it, but it's impractical without the following shims
# Since Python has inconsistent policies for what encoding an arbitrary stream will be.
def fix_stdio(encoding='utf8', errors='strict', buffering=0):
""" forces utf8 at I/O boundaries, since it's ascii by default when using
pipes .. ugh .. Never call this multple times in the same process; horrible
things sometimes seem to happen."""
en,er,bu=encoding,errors,buffering
sys.stdout = codecs.open('/dev/stdout', 'w', encoding=en, errors=er, buffering=bu)
sys.stdout = ShutUpAboutBrokenPipe(sys.stdout)
sys.stdin = codecs.open('/dev/stdin', 'r', encoding=en, errors=er, buffering=bu)
sys.stderr = codecs.open('/dev/stderr', 'w', encoding=en, errors=er, buffering=0)
def unicodify(s, encoding='utf8', *args):
""" because {str,unicode}.{encode,decode} is anti-polymorphic, but sometimes
you can't control which you have. """
if isinstance(s,unicode): return s
return s.decode(encoding, *args)
def stringify(s, encoding='utf8', *args):
if isinstance(s,str): return s
return s.encode(encoding, *args)
class ShutUpAboutBrokenPipe:
"""i like to press ctrl-c; why is python yelling at me?"""
def __init__(self, fp):
self.fp = fp
def write(self,*a,**k):
try:
self.fp.write(*a,**k)
except IOError, e:
if e.errno == 32: # broken pipe
sys.exit(0)
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment