Better errors on unexpected bencode data
Also fix indent.
This commit is contained in:
parent
1f3f36505e
commit
0fdde20654
@ -25,37 +25,41 @@ def vim_encode(data):
|
|||||||
raise TypeError("can't encode a " + type(data).__name__)
|
raise TypeError("can't encode a " + type(data).__name__)
|
||||||
|
|
||||||
def bdecode(f, char=None):
|
def bdecode(f, char=None):
|
||||||
if char == None:
|
if char == None:
|
||||||
|
char = f.read(1)
|
||||||
|
if char == 'l':
|
||||||
|
l = []
|
||||||
|
while True:
|
||||||
char = f.read(1)
|
char = f.read(1)
|
||||||
if char == 'l':
|
if char == 'e':
|
||||||
l = []
|
return l
|
||||||
while True:
|
l.append(bdecode(f, char))
|
||||||
char = f.read(1)
|
elif char == 'd':
|
||||||
if char == 'e':
|
d = {}
|
||||||
return l
|
while True:
|
||||||
l.append(bdecode(f, char))
|
char = f.read(1)
|
||||||
elif char == 'd':
|
if char == 'e':
|
||||||
d = {}
|
return d
|
||||||
while True:
|
key = bdecode(f, char)
|
||||||
char = f.read(1)
|
d[key] = bdecode(f)
|
||||||
if char == 'e':
|
elif char == 'i':
|
||||||
return d
|
i = 0
|
||||||
key = bdecode(f, char)
|
while True:
|
||||||
d[key] = bdecode(f)
|
char = f.read(1)
|
||||||
elif char == 'i':
|
if char == 'e':
|
||||||
i = 0
|
return i
|
||||||
while True:
|
i = 10 * i + int(char)
|
||||||
char = f.read(1)
|
elif char.isdigit():
|
||||||
if char == 'e':
|
i = int(char)
|
||||||
return i
|
while True:
|
||||||
i = 10 * i + int(char)
|
char = f.read(1)
|
||||||
else:
|
if char == ':':
|
||||||
i = int(char)
|
return f.read(i)
|
||||||
while True:
|
i = 10 * i + int(char)
|
||||||
char = f.read(1)
|
elif char == '':
|
||||||
if char == ':':
|
raise EOFError("unexpected end of bencode data")
|
||||||
return f.read(i)
|
else:
|
||||||
i = 10 * i + int(char)
|
raise TypeError("unexpected type "+char+"in bencode data")
|
||||||
|
|
||||||
|
|
||||||
class Connection:
|
class Connection:
|
||||||
|
Loading…
Reference in New Issue
Block a user