code: def replace_surrogate_encode(mystring, exc):
      
      decoded = []
      for ch in mystring:
          # if PY3:
          #     code = ch
          # else:
          code = ord(ch)
  
          # The following magic comes from Py3.3's Python/codecs.c file:
          if not 0xD800 <= code <= 0xDCFF:
              # Not a surrogate. Fail with the original exception.
              raise exc
          # mybytes = [0xe0 | (code >> 12),
          #            0x80 | ((code >> 6) & 0x3f),
          #            0x80 | (code & 0x3f)]
          # Is this a good idea?
          if 0xDC00 <= code <= 0xDC7F:
              decoded.append(_unichr(code - 0xDC00))
          elif code <= 0xDCFF:
              decoded.append(_unichr(code - 0xDC00))
          else:
              raise NotASurrogateError
      return str().join(decoded)
docs: Returns a (unicode) string, not the more logical bytes, because the codecs
      register_error functionality expects this.