mercurial/thirdparty/cbor/cbor2/compat.py
author Matt Harbison <matt_harbison@yahoo.com>
Wed, 09 Jan 2019 16:02:05 -0500
changeset 41180 69804c040a04
parent 37126 4bd73a955ab0
permissions -rw-r--r--
convert: don't drop commits that are empty in the source when using --filemap I ran into this when using `hg lfconvert --to-normal` (which uses the filemap class internally), and saw that commits with nothing but a branch change were dropped. We could put in an option that only lfconvert uses internally. But silently dropping anything other than a commit where all changes were excluded seems unintended. For example, there's a message in mercurial_sink.putcommit() if it drops an empty commit. (And the reason that isn't kicking in here is because lfconvert isn't passing --filemap, so the self.filemapmode conditional there is always False.) The naive change of `return not files` broke test-convert-filemap.t, so this is a little more elaborate than needed for converting from largefiles.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37126
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     1
from math import ldexp
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     2
import struct
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     3
import sys
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     4
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     5
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     6
if sys.version_info.major < 3:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     7
    from datetime import tzinfo, timedelta
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     8
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     9
    class timezone(tzinfo):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    10
        def __init__(self, offset):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    11
            self.offset = offset
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    12
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    13
        def utcoffset(self, dt):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    14
            return self.offset
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    15
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    16
        def dst(self, dt):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    17
            return timedelta(0)
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    18
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    19
        def tzname(self, dt):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    20
            return 'UTC+00:00'
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    21
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    22
    def as_unicode(string):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    23
        return string.decode('utf-8')
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    24
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    25
    def iteritems(self):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    26
        return self.iteritems()
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    27
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    28
    def bytes_from_list(values):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    29
        return bytes(bytearray(values))
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    30
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    31
    byte_as_integer = ord
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    32
    timezone.utc = timezone(timedelta(0))
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    33
    xrange = xrange  # noqa: F821
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    34
    long = long  # noqa: F821
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    35
    unicode = unicode  # noqa: F821
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    36
else:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    37
    from datetime import timezone
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    38
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    39
    def byte_as_integer(bytestr):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    40
        return bytestr[0]
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    41
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    42
    def as_unicode(string):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    43
        return string
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    44
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    45
    def iteritems(self):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    46
        return self.items()
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    47
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    48
    xrange = range
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    49
    long = int
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    50
    unicode = str
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    51
    bytes_from_list = bytes
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    52
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    53
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    54
if sys.version_info.major >= 3 and sys.version_info.minor >= 6:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    55
    # Python 3.6 added 16 bit floating point to struct
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    56
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    57
    def pack_float16(value):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    58
        try:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    59
            return struct.pack('>Be', 0xf9, value)
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    60
        except OverflowError:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    61
            return False
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    62
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    63
    def unpack_float16(payload):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    64
        return struct.unpack('>e', payload)[0]
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    65
else:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    66
    def pack_float16(value):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    67
        # Based on node-cbor by hildjj
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    68
        # which was based in turn on Carsten Borman's cn-cbor
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    69
        u32 = struct.pack('>f', value)
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    70
        u = struct.unpack('>I', u32)[0]
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    71
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    72
        if u & 0x1FFF != 0:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    73
            return False
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    74
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    75
        s16 = (u >> 16) & 0x8000
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    76
        exponent = (u >> 23) & 0xff
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    77
        mantissa = u & 0x7fffff
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    78
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    79
        if 113 <= exponent <= 142:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    80
            s16 += ((exponent - 112) << 10) + (mantissa >> 13)
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    81
        elif 103 <= exponent < 113:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    82
            if mantissa & ((1 << (126 - exponent)) - 1):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    83
                return False
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    84
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    85
            s16 += ((mantissa + 0x800000) >> (126 - exponent))
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    86
        else:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    87
            return False
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    88
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    89
        return struct.pack('>BH', 0xf9, s16)
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    90
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    91
    def unpack_float16(payload):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    92
        # Code adapted from RFC 7049, appendix D
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    93
        def decode_single(single):
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    94
            return struct.unpack("!f", struct.pack("!I", single))[0]
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    95
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    96
        payload = struct.unpack('>H', payload)[0]
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    97
        value = (payload & 0x7fff) << 13 | (payload & 0x8000) << 16
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    98
        if payload & 0x7c00 != 0x7c00:
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    99
            return ldexp(decode_single(value), 112)
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   100
4bd73a955ab0 thirdparty: vendor cbor2 python library
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   101
        return decode_single(value | 0x7f800000)