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.patch

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

partial revert which at least gets python2 working again..

  • xpra/net/bencode/bencode.py

     
    3939        n = int(x[f:newf])
    4040    except (OverflowError, ValueError):
    4141        n = long(x[f:newf])
    42     if x[f] == cv('-'):
    43         if x[f + 1] == cv('0'):
     42    if x[f] == '-':
     43        if x[f + 1] == '0':
    4444            raise ValueError
    45     elif x[f] == cv('0') and newf != f+1:
     45    elif x[f] == '0' and newf != f+1:
    4646        raise ValueError
    4747    return (n, newf+1)
    4848
     
    5353        n = int(x[f:colon])
    5454    except (OverflowError, ValueError):
    5555        n = long(x[f:colon])
    56     if x[f] == cv('0') and colon != f+1:
     56    if x[f] == '0' and colon != f+1:
    5757        raise ValueError
    5858    colon += 1
    5959    return (x[colon:colon+n], colon+n)
    6060
    61 def decode_py3kstring(x, f):
    62     xs, fs = decode_string(x, f)
    63     return (xs, fs)
    64 
    6561def decode_unicode(x, f):
    6662    xs, fs = decode_string(x, f+1)
    6763    return (xs.decode("utf8"), fs)
     
    6864
    6965def decode_list(x, f):
    7066    r, f = [], f+1
    71     while x[f] != cv('e'):
     67    while x[f] != 'e':
    7268        fn = decode_func.get(x[f])
    7369        if not fn:
    7470            raise ValueError("invalid list entry: %s" % (x[f:]))
     
    7975def decode_dict(x, f):
    8076    r, f = {}, f+1
    8177    #lastkey = None
    82     while x[f] != cv('e'):
     78    while x[f] != 'e':
    8379        fn = decode_func.get(x[f])
    8480        if not fn:
    8581            raise ValueError("invalid dict key: %s" % (x[f:]))
    8682        k, f = fn(x, f)
     83        #if lastkey is not None and lastkey >= k:
     84        #    raise ValueError("keys are not in ascending order!")
     85        #lastkey = k
    8786        fn = decode_func.get(x[f])
    8887        if not fn:
    8988            raise ValueError("invalid dict value: %s" % (x[f:]))
     
    9089        r[k], f = fn(x, f)
    9190    return (r, f + 1)
    9291
    93 
    9492decode_func = {}
    9593decode_func['l'] = decode_list
    9694decode_func['d'] = decode_dict
    9795decode_func['i'] = decode_int
    9896for c in '0123456789':
    99     if sys.version_info[0]<3:
    100         decode_func[c] = decode_string
    101     else:
    102         decode_func[c] = decode_py3kstring
     97    decode_func[c] = decode_string
    10398decode_func['u'] = decode_unicode
    10499#now as byte values:
    105100for k,v in dict(decode_func).items():
    106     decode_func[ord(k)] = v
     101    decode_func[ord(k)] = lambda x,f : v(str(x), f)
    107102
    108 
    109103def bdecode(x):
    110104    try:
    111         #v = x[0].encode("utf-8")
    112         xs = x.encode("utf8")
    113         #v = x[0]
    114         fn = decode_func.get(xs[0])
     105        fn = decode_func.get(x[0])
    115106        if not fn:
    116             raise ValueError("invalid type identifier: %s" % (xs[0]))
    117         r, l = fn(xs, 0)
    118     except (IndexError, KeyError) as e:
     107            raise ValueError("invalid type identifier: %s" % (x[0]))
     108        r, l = fn(x, 0)
     109    except (IndexError, KeyError):
    119110        import traceback
    120111        traceback.print_exc()
    121         raise e
     112        raise ValueError
    122113    return r, l
    123114
    124115def encode_int(x, r):
     
    128119def encode_string(x, r):
    129120    r.extend((str(len(x)), ':', x))
    130121
    131 def encode_bytes(x, r):
     122def encode_unicode(x, r):
     123    x = x.encode("utf8")
    132124    encode_string(x, r)
    133125
    134 def encode_unicode(x, r):
    135     encode_string(x.encode("utf8"), r)
    136 
    137126def encode_list(x, r):
    138127    r.append('l')
    139128    for i in x:
     
    163152    encode_func[BooleanType] = encode_int
    164153else:
    165154    encode_func[int] = encode_int
    166     encode_func[str] = encode_unicode
     155    encode_func[str] = encode_string
    167156    encode_func[list] = encode_list
    168157    encode_func[tuple] = encode_list
    169158    encode_func[dict] = encode_dict
    170159    encode_func[bool] = encode_int
     160    def encode_bytes(x, r):
     161        encode_string(x.decode(), r)
    171162    encode_func[bytes] = encode_bytes
    172163
    173164def bencode(x):
    174165    r = []
    175166    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)
     167    return ''.join(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()