File: surrogateescape.py

package info (click to toggle)
python-libarchive-c 2.1-3.1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 836 kB
  • sloc: python: 808; makefile: 26; sh: 8
file content (49 lines) | stat: -rw-r--r-- 1,459 bytes parent folder | download | duplicates (4)
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
38
39
40
41
42
43
44
45
46
47
48
49
"""
This is a modified version of Victor Stinner's pure-Python implementation of
PEP 383: the "surrogateescape" error handler of Python 3.

This code is released under the Python license and the BSD 2-clause license

Source: misc/python/surrogateescape.py in https://bitbucket.org/haypo/misc
"""

from __future__ import division, print_function, unicode_literals

import codecs


chr = __builtins__.get('unichr', chr)


def surrogateescape(exc):
    if isinstance(exc, UnicodeDecodeError):
        decoded = []
        for code in exc.object[exc.start:exc.end]:
            if not isinstance(code, int):
                code = ord(code)
            if 0x80 <= code <= 0xFF:
                decoded.append(chr(0xDC00 + code))
            elif code <= 0x7F:
                decoded.append(chr(code))
            else:
                raise exc
        return (''.join(decoded), exc.end)
    elif isinstance(exc, UnicodeEncodeError):
        encoded = []
        for ch in exc.object[exc.start:exc.end]:
            code = ord(ch)
            if not 0xDC80 <= code <= 0xDCFF:
                raise exc
            encoded.append(chr(code - 0xDC00))
        return (''.join(encoded), exc.end)
    else:
        raise exc


def register():
    """Register the surrogateescape error handler if it doesn't exist
    """
    try:
        codecs.lookup_error('surrogateescape')
    except LookupError:
        codecs.register_error('surrogateescape', surrogateescape)