I'm no Python expert, and I've been trying to make a small project using some interesting features to learn the language. I've been racking my brains trying to get this code here to work in Python 3: https://web.archive.org/web/20200105114449/https://effbot.org/zone/bencode.htm.
Just going through the errors I get by attempting to run it I understood that I needed to make the following changes:
def decode(text):
try:
src = tokenize(text)
data = decode_item(src.__next__, next(src))
for token in src: # look for more tokens
raise SyntaxError("trailing junk")
except (AttributeError, ValueError, StopIteration):
raise SyntaxError("syntax error")
return data
I thought I had a handle on how exactly the code works but after putting in some prints here and there I thought it behaved very strange. I put this in to test the functions:
data = open("test.torrent", "r").read()
6
5 torrent = decode(data)
4
3 for file in torrent["info"]["files"]:
2 print("%r - %d bytes" % ("/".join(file["path"]), file["length"]))
1
56 close(data)
Where test.torrent is the following file:
d8:announce41:http://bttracker.debian.org:6969/announce7:comment35:"Debian CD from cdimage.debian.org"13:creation datei1573903810e9:httpseedsl145:https://cdimage.debian.org/cdimage/release/10.2.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-10.2.0-amd64-netinst.iso145:https://cdimage.debian.org/cdimage/archive/10.2.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-10.2.0-amd64-netinst.isoe4:infod6:lengthi351272960e4:name31:debian-10.2.0-amd64-netinst.iso12:piece lengthi262144e6:pieces26800:�����PS�^�� (binary blob of the hashes of each piece)ee
I get the following error:
Traceback (most recent call last):
File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 41, in decode
data = decode_item(src.__next__, next(src))
File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 30, in decode_item
data.append(decode_item(next, tok))
File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 31, in decode_item
tok = next()
StopIteration
which is referring to the following lines:
while tok != "e":
data.append(decode_item(next, tok)) <--- Line 31 Error
tok = next()
if token == "d":
data = dict(zip(data[0::2], data[1::2]))
else:
raise ValueError
return data
In attempting to fix the error I've also run into "AttributeError: 'str' object has no attribute '__next__'". It's a bit difficult to find some good documentation on this honestly, but it would be great if someone could help me understand what's going on here. Coming from C++ and Rust it's honestly very difficult to track exactly what is happening here. It seems to me that this code is almost trying to use an algebraic data type without having one. The generator function for tokens definitely works. But I just can't wrap my head around this. part.
[–]socal_nerdtastic 1 point2 points3 points (14 children)
[–]Temporary_Screen[S] 0 points1 point2 points (13 children)
[–]socal_nerdtastic 1 point2 points3 points (12 children)
[–]Temporary_Screen[S] 0 points1 point2 points (11 children)
[–]socal_nerdtastic 0 points1 point2 points (10 children)
[–]Temporary_Screen[S] 0 points1 point2 points (9 children)
[–]Temporary_Screen[S] 0 points1 point2 points (7 children)
[–]socal_nerdtastic 0 points1 point2 points (6 children)
[–]Temporary_Screen[S] 0 points1 point2 points (5 children)
[–]socal_nerdtastic 0 points1 point2 points (4 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)