Merge lp:~gz/brz/py3_bencode_really into lp:brz

Proposed by Martin Packman
Status: Merged
Approved by: Martin Packman
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~gz/brz/py3_bencode_really
Merge into: lp:brz
Diff against target: 79 lines (+12/-14)
1 file modified
breezy/util/_bencode_py.py (+12/-14)
To merge this branch: bzr merge lp:~gz/brz/py3_bencode_really
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+325880@code.launchpad.net

Commit message

Make bencode python implementation pass tests on Python 3

Description of the change

Thought I'd already done this, but apparently only enough so it didn't break other bits, not actually pass its own tests. The changes are pretty dumb, but the cython code can also be cleaned up a bunch for where we care about speed.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'breezy/util/_bencode_py.py'
--- breezy/util/_bencode_py.py 2017-06-10 22:09:37 +0000
+++ breezy/util/_bencode_py.py 2017-06-16 23:20:42 +0000
@@ -49,25 +49,24 @@
49 f += 149 f += 1
50 newf = x.index(b'e', f)50 newf = x.index(b'e', f)
51 n = int(x[f:newf])51 n = int(x[f:newf])
52 if x[f] == b'-':52 if x[f:f+2] == b'-0':
53 if x[f + 1] == b'0':53 raise ValueError
54 raise ValueError54 elif x[f:f+1] == b'0' and newf != f+1:
55 elif x[f] == b'0' and newf != f+1:
56 raise ValueError55 raise ValueError
57 return (n, newf+1)56 return (n, newf+1)
5857
59 def decode_string(self, x, f):58 def decode_string(self, x, f):
60 colon = x.index(b':', f)59 colon = x.index(b':', f)
61 n = int(x[f:colon])60 n = int(x[f:colon])
62 if x[f] == b'0' and colon != f+1:61 if x[f:f+1] == b'0' and colon != f+1:
63 raise ValueError62 raise ValueError
64 colon += 163 colon += 1
65 return (x[colon:colon+n], colon+n)64 return (x[colon:colon+n], colon+n)
6665
67 def decode_list(self, x, f):66 def decode_list(self, x, f):
68 r, f = [], f+167 r, f = [], f+1
69 while x[f] != b'e':68 while x[f:f+1] != b'e':
70 v, f = self.decode_func[x[f]](x, f)69 v, f = self.decode_func[x[f:f+1]](x, f)
71 r.append(v)70 r.append(v)
72 if self.yield_tuples:71 if self.yield_tuples:
73 r = tuple(r)72 r = tuple(r)
@@ -76,12 +75,12 @@
76 def decode_dict(self, x, f):75 def decode_dict(self, x, f):
77 r, f = {}, f+176 r, f = {}, f+1
78 lastkey = None77 lastkey = None
79 while x[f] != b'e':78 while x[f:f+1] != b'e':
80 k, f = self.decode_string(x, f)79 k, f = self.decode_string(x, f)
81 if lastkey >= k:80 if lastkey is not None and lastkey >= k:
82 raise ValueError81 raise ValueError
83 lastkey = k82 lastkey = k
84 r[k], f = self.decode_func[x[f]](x, f)83 r[k], f = self.decode_func[x[f:f+1]](x, f)
85 return (r, f + 1)84 return (r, f + 1)
8685
87 def bdecode(self, x):86 def bdecode(self, x):
@@ -109,6 +108,7 @@
109 def __init__(self, s):108 def __init__(self, s):
110 self.bencoded = s109 self.bencoded = s
111110
111
112def encode_bencached(x,r):112def encode_bencached(x,r):
113 r.append(x.bencoded)113 r.append(x.bencoded)
114114
@@ -121,9 +121,6 @@
121def encode_string(x, r):121def encode_string(x, r):
122 r.extend((int_to_bytes(len(x)), b':', x))122 r.extend((int_to_bytes(len(x)), b':', x))
123123
124def encode_unicode(x, r):
125 r.extend((int_to_bytes(len(x)), b':', x))
126
127def encode_list(x, r):124def encode_list(x, r):
128 r.append(b'l')125 r.append(b'l')
129 for i in x:126 for i in x:
@@ -145,7 +142,8 @@
145 encode_func[long] = encode_int142 encode_func[long] = encode_int
146 int_to_bytes = str143 int_to_bytes = str
147else:144else:
148 int_to_bytes = lambda n: b"%d" % n145 def int_to_bytes(n):
146 return b'%d' % n
149encode_func[bytes] = encode_string147encode_func[bytes] = encode_string
150encode_func[list] = encode_list148encode_func[list] = encode_list
151encode_func[tuple] = encode_list149encode_func[tuple] = encode_list

Subscribers

People subscribed via source and target branches