Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 37 lines (33 sloc) 1.149 kb
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
#Copyright (c) 2011 Yahoo! Inc. All rights reserved. Licensed under the BSD License.
# See accompanying LICENSE file or http://www.opensource.org/licenses/BSD-3-Clause for the specific language governing permissions and limitations under the License.


__author__ = "BOSS Team"

def strfix(msg):
  """
Copies this recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523011
Constants out any characters that spew encoding errors
"""
  goodchars = {}
  try:
    return str(msg)
  except UnicodeEncodeError:
    res=''
    for i in list(msg):
      if i not in goodchars:
        try:
          str(i)
          goodchars[i] = i
        except UnicodeEncodeError:
          # format character as python string constant
          code = ord(i)
          t = None
          if code < 256:
            t ='\\x%02x' % code # 8-bit value
          elif code < 65536:
            t ='\\u%04x' % code # 16-bit value unicode
          else:
            t = '\\U%08x' % code # other values as 32-bit unicode
          goodchars[i] = t # or '.' for readability ;-)
      res += goodchars[i]
    return res

def write(msg):
  print strfix(msg)
Something went wrong with that request. Please try again.