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
1=== modified file 'breezy/util/_bencode_py.py'
2--- breezy/util/_bencode_py.py 2017-06-10 22:09:37 +0000
3+++ breezy/util/_bencode_py.py 2017-06-16 23:20:42 +0000
4@@ -49,25 +49,24 @@
5 f += 1
6 newf = x.index(b'e', f)
7 n = int(x[f:newf])
8- if x[f] == b'-':
9- if x[f + 1] == b'0':
10- raise ValueError
11- elif x[f] == b'0' and newf != f+1:
12+ if x[f:f+2] == b'-0':
13+ raise ValueError
14+ elif x[f:f+1] == b'0' and newf != f+1:
15 raise ValueError
16 return (n, newf+1)
17
18 def decode_string(self, x, f):
19 colon = x.index(b':', f)
20 n = int(x[f:colon])
21- if x[f] == b'0' and colon != f+1:
22+ if x[f:f+1] == b'0' and colon != f+1:
23 raise ValueError
24 colon += 1
25 return (x[colon:colon+n], colon+n)
26
27 def decode_list(self, x, f):
28 r, f = [], f+1
29- while x[f] != b'e':
30- v, f = self.decode_func[x[f]](x, f)
31+ while x[f:f+1] != b'e':
32+ v, f = self.decode_func[x[f:f+1]](x, f)
33 r.append(v)
34 if self.yield_tuples:
35 r = tuple(r)
36@@ -76,12 +75,12 @@
37 def decode_dict(self, x, f):
38 r, f = {}, f+1
39 lastkey = None
40- while x[f] != b'e':
41+ while x[f:f+1] != b'e':
42 k, f = self.decode_string(x, f)
43- if lastkey >= k:
44+ if lastkey is not None and lastkey >= k:
45 raise ValueError
46 lastkey = k
47- r[k], f = self.decode_func[x[f]](x, f)
48+ r[k], f = self.decode_func[x[f:f+1]](x, f)
49 return (r, f + 1)
50
51 def bdecode(self, x):
52@@ -109,6 +108,7 @@
53 def __init__(self, s):
54 self.bencoded = s
55
56+
57 def encode_bencached(x,r):
58 r.append(x.bencoded)
59
60@@ -121,9 +121,6 @@
61 def encode_string(x, r):
62 r.extend((int_to_bytes(len(x)), b':', x))
63
64-def encode_unicode(x, r):
65- r.extend((int_to_bytes(len(x)), b':', x))
66-
67 def encode_list(x, r):
68 r.append(b'l')
69 for i in x:
70@@ -145,7 +142,8 @@
71 encode_func[long] = encode_int
72 int_to_bytes = str
73 else:
74- int_to_bytes = lambda n: b"%d" % n
75+ def int_to_bytes(n):
76+ return b'%d' % n
77 encode_func[bytes] = encode_string
78 encode_func[list] = encode_list
79 encode_func[tuple] = encode_list

Subscribers

People subscribed via source and target branches