| 58 | # How to deal with a string containing bytes before handing it to the |
| 59 | # application through the 'normal' interface. |
| 60 | def _sanitize(string): |
| 61 | # Turn any escaped bytes into unicode 'unknown' char. If the escaped |
| 62 | # bytes happen to be utf-8 they will instead get decoded, even if they |
| 63 | # were invalid in the charset the source was supposed to be in. This |
| 64 | # seems like it is not a bad thing; a defect was still registered. |
| 65 | original_bytes = string.encode('utf-8', 'surrogateescape') |
| 66 | return original_bytes.decode('utf-8', 'replace') |
| 67 | |
| 68 | |
| 69 | |