<p dir="ltr"><br>
On 26 Aug 2014 21:34, "MRAB" <<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>> wrote:<br>
><br>
> On 2014-08-26 03:11, Stephen J. Turnbull wrote:<br>
>><br>
>> Nick Coghlan writes:<br>
>><br>
>>   > "purge_surrogate_escapes" was the other term that occurred to me.<br>
>><br>
>> "purge" suggests removal, not replacement.  That may be useful too.<br>
>><br>
>> neutralize_surrogate_escapes(s, remove=False, replacement='\uFFFD')<br>
>><br>
> How about:<br>
><br>
>     replace_surrogate_escapes(s, replacement='\uFFFD')<br>
><br>
> If you want them removed, just pass an empty string as the replacement.</p>
<p dir="ltr">The current proposal on the issue tracker is to instead take advantage of the existing error handlers:</p>
<p dir="ltr">    def convert_surrogateescape(data, errors='replace'):<br>
        return data.encode('utf-8', 'surrogateescape').decode('utf-8', errors)</p>
<p dir="ltr">That code is short, but semantically dense - it took a few iterations to come up with that version. (Added bonus: once you're alerted to the possibility, it's trivial to write your own version for existing Python 3 versions. The standard name just makes it easier to look up when you come across it in a piece of code, and provides the option of optimising it later if it ever seems worth the extra work)</p>

<p dir="ltr">I also filed a separate RFE to make backslashreplace usable on input, since that allows the option of separating the replacement operation from the encoding operation.</p>
<p dir="ltr">Cheers,<br>
Nick.<br>
</p>