o ,º]Ñã@sÂdZddlZddlZddlmZdZdd„Zdd„Zejr$e Z d d „Z ne Z e Z d d „Z Gd d„deƒZdd„Zdd„Zdd„Zdd„ZdZedƒZedƒZe e¡jZdd„Zedkr_ dSdS)z¾ This is Victor Stinner's pure-Python implementation of PEP 383: the "surrogateescape" error handler of Python 3. Source: misc/python/surrogateescape.py in https://bitbucket.org/haypo/misc éN)ÚutilsÚsurrogateescapecCstjr|S| d¡S)NÚunicode_escape)rÚPY3Údecode)Útext©rú>/usr/lib/python3/dist-packages/future/utils/surrogateescape.pyÚus r cCstjr| d¡S|S)NÚlatin1)rrÚencode)Údatarrr Úbs rcCs t|fƒS©N)Úbytes)Úcoderrr Ú#s rcCs`|j|j|j…}zt|tƒrt|ƒ}n t|tƒrt|ƒ}n|‚Wn ty*|‚w||jfS)zý Pure Python implementation of the PEP 383: the "surrogateescape" error handler of Python 3. Undecodable bytes will be replaced by a Unicode character U+DCxx on decoding, and these are translated into the original bytes on encoding. ) ÚobjectÚstartÚendÚ isinstanceÚUnicodeDecodeErrorÚreplace_surrogate_decodeÚUnicodeEncodeErrorÚreplace_surrogate_encodeÚNotASurrogateError)ÚexcÚmystringÚdecodedrrr Úsurrogateescape_handler(s    € ÿ rc@s eZdZdS)rN)Ú__name__Ú __module__Ú __qualname__rrrr rCsrcCsˆg}|D]9}t|ƒ}d|krdkst‚t‚d|kr"dkr.nn | t|dƒ¡q|dkr<| t|dƒ¡qt‚tƒ |¡S)zƒ Returns a (unicode) string, not the more logical bytes, because the codecs register_error functionality expects this. éØéÿÜéÜiÜ)ÚordrÚappendÚ_unichrÚstrÚjoin)rrÚchrrrr rGsþ rcCsxg}|D]1}t|tƒr|}nt|ƒ}d|krdkr(nn | td|ƒ¡q|dkr4| t|ƒ¡qt‚tƒ |¡S)z$ Returns a (unicode) string é€éÿr%é)rÚintr&r'r(rr)r*)Úmybytesrr+rrrr rds  rcCs6tdkrDg}t|ƒD]3\}}t|ƒ}|dkrt|ƒ}nd|kr%dkr.nnt|dƒ}n tt|||ddƒ‚| |¡q tƒ |¡Stdkr•g}t|ƒD]@\}}t|ƒ}d |kr`d kr„nn"d|krldkrznn t|dƒ}| |¡qNtt|||dd ƒ‚| d¡}| |¡qNtƒ |¡S| tt ¡S) NÚasciir,i€Ür$r%ézordinal not in range(128)zutf-8r#iÿßzsurrogates not allowed) Ú FS_ENCODINGÚ enumerater&Ú bytes_chrrr'rr*r Ú FS_ERRORS)ÚfnÚencodedÚindexr+rÚch_utf8rrr Úencodefilename}s<  þ     þ    r;cCs | tt¡Sr)rr3r6)r7rrr Údecodefilename¤s r<r1u[abcÿ]u[abcí³¿]cCs<tjrdSzt t¡WdStyt tt¡YdSw)zH Registers the surrogateescape error handler on Python 2 (only) N)rrÚcodecsÚ lookup_errorr6Ú LookupErrorÚregister_errorrrrrr Úregister_surrogateescape±s ÿrAÚ__main__)Ú__doc__r=ÚsysÚfuturerr6r rrÚchrr(r5ÚunichrrÚ Exceptionrrrr;r<r3r7r8ÚlookupÚnamerAr rrrr Ús0   '  ÿ