xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #854: bencoder-python2-refix-v2.patch

File bencoder-python2-refix-v2.patch, 5.8 KB (added by Antoine Martin, 6 years ago)

all but one test fails..

  • xpra/net/bencode/bencode.py

     
    2525    #the values end up being ints..
    2626    def cv(x):
    2727        return ord(x)
     28    import codecs
     29    def b(x):
     30        if type(x)==bytes:
     31            return x
     32        return codecs.latin_1_encode(x)[0]
    2833else:
    2934    def strindex(s, c, start):
    3035        return s.index(c, start)
    3136    def cv(x):
    3237        return x
     38    def b(x):               #@DuplicatedSignature
     39        return x
    3340
    3441
    3542def decode_int(x, f):
     
    4047    except (OverflowError, ValueError):
    4148        n = long(x[f:newf])
    4249    if x[f] == cv('-'):
    43         if x[f + 1] == cv('0'):
     50        if x[f + 1] == '0':
    4451            raise ValueError
    4552    elif x[f] == cv('0') and newf != f+1:
    4653        raise ValueError
     
    8491        if not fn:
    8592            raise ValueError("invalid dict key: %s" % (x[f:]))
    8693        k, f = fn(x, f)
     94        #if lastkey is not None and lastkey >= k:
     95        #    raise ValueError("keys are not in ascending order!")
     96        #lastkey = k
    8797        fn = decode_func.get(x[f])
    8898        if not fn:
    8999            raise ValueError("invalid dict value: %s" % (x[f:]))
     
    90100        r[k], f = fn(x, f)
    91101    return (r, f + 1)
    92102
    93 
    94103decode_func = {}
    95104decode_func['l'] = decode_list
    96105decode_func['d'] = decode_dict
     
    100109        decode_func[c] = decode_string
    101110    else:
    102111        decode_func[c] = decode_py3kstring
     112    decode_func[c] = decode_string
    103113decode_func['u'] = decode_unicode
    104114#now as byte values:
    105115for k,v in dict(decode_func).items():
     
    108118
    109119def bdecode(x):
    110120    try:
    111         #v = x[0].encode("utf-8")
    112         xs = x.encode("utf8")
    113         #v = x[0]
     121        xs = b(x)
    114122        fn = decode_func.get(xs[0])
    115123        if not fn:
    116124            raise ValueError("invalid type identifier: %s" % (xs[0]))
    117125        r, l = fn(xs, 0)
    118     except (IndexError, KeyError) as e:
     126    except (IndexError, KeyError):
    119127        import traceback
    120128        traceback.print_exc()
    121         raise e
     129        raise ValueError
    122130    return r, l
    123131
    124132def encode_int(x, r):
     
    128136def encode_string(x, r):
    129137    r.extend((str(len(x)), ':', x))
    130138
    131 def encode_bytes(x, r):
     139def encode_unicode(x, r):
     140    x = x.encode("utf8")
    132141    encode_string(x, r)
    133142
    134 def encode_unicode(x, r):
    135     encode_string(x.encode("utf8"), r)
    136 
    137143def encode_list(x, r):
    138144    r.append('l')
    139145    for i in x:
     
    163169    encode_func[BooleanType] = encode_int
    164170else:
    165171    encode_func[int] = encode_int
    166     encode_func[str] = encode_unicode
     172    encode_func[str] = encode_string
    167173    encode_func[list] = encode_list
    168174    encode_func[tuple] = encode_list
    169175    encode_func[dict] = encode_dict
    170176    encode_func[bool] = encode_int
     177    def encode_bytes(x, r):
     178        encode_string(x.decode(), r)
    171179    encode_func[bytes] = encode_bytes
    172180
    173181def bencode(x):
    174182    r = []
    175183    encode_func[type(x)](x, r)
    176     def bytestostr(x):
    177         if type(x)==bytes:
    178             return x.decode("utf8")
    179         return x
    180     return ''.join(bytestostr(x) for x in r)
     184    return b''.join(b(v) for v in r)
  • xpra/net/bencode/cython_bencode.pyx

     
    2525    ListType    = list
    2626    TupleType   = tuple
    2727    BooleanType = bool
     28    import codecs
     29    def b(x):
     30        if type(x)==bytes:
     31            return x
     32        return codecs.latin_1_encode(x)[0]
    2833else:
    2934    from types import (StringType, UnicodeType, IntType, LongType, DictType, ListType,
    3035                       TupleType, BooleanType)
     36    def b(x):               #@DuplicatedSignature
     37        return x
    3138
    3239
    3340cdef int find(const char *p, char c, int start, size_t len):
     
    6370    assert colon>=0, "colon not found in string size header"
    6471    lenstr = x[f:colon]
    6572    try:
    66         slen = int(lenstr)
     73        slen = IntType(lenstr)
    6774    except (OverflowError, ValueError):
    6875        try:
    69             slen = long(lenstr)
     76            slen = LongType(lenstr)
    7077        except:
    7178            raise ValueError("cannot parse length '%s' (f=%s, colon=%s, string=%s)" % (lenstr, f, colon, x))
    7279    if x[f] == '0' and colon != f+1:
     
    7481    colon += 1
    7582    return (x[colon:colon+slen], colon+slen)
    7683
    77 cdef decode_py3kstring(const char *x, int f, int l):
    78     xs, fs = decode_string(x, f, l)
    79     return (xs.decode("utf8"), fs)
    80 
    8184cdef decode_unicode(const char *x, int f, int l):
    8285    xs, fs = decode_string(x, f+1, l)
    8386    return (xs.decode("utf8"), fs)
     
    117120    elif c=='i':
    118121        return decode_int(x, f, l)
    119122    elif c in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
    120         if sys.version_info[0]<3:
    121             return decode_string(x, f, l)
    122         else:
    123             return decode_py3kstring(x, f, l)
     123        return decode_string(x, f, l)
    124124    elif c=='u':
    125125        return decode_unicode(x, f, l)
    126126    else:
    127127        raise ValueError("invalid %s type identifier: %s at position %s" % (what, c, f))
    128128
    129 """
    130     cdef const char *s = x
    131     cdef size_t l = len(x)
    132 """
    133129def bdecode(x):
    134     xs = x.encode("utf8")
     130    xs = b(x)
    135131    cdef const char *s
    136132    cdef Py_ssize_t l
    137133    assert object_as_buffer(xs, <const void **> &s, &l)==0, "failed to convert %s to a buffer" % type(x)
     
    197193    r = []
    198194    try:
    199195        encode(x, r)
    200         def bytestostr(x):
    201             if type(x)==bytes:
    202                 return x.decode("utf8")
    203             return str(x)
    204         return ''.join(bytestostr(x) for x in r)
     196        return b''.join(b(v) for v in r)
    205197    except Exception as e:
    206198        import traceback
    207199        traceback.print_exc()