brendano (owner)

Revisions

gist: 100368 Download_button fork
public
Public Clone URL: git://gist.github.com/100368.git
Embed All Files: show embed
Python #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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