diff options
| author | jvoisin | 2018-06-10 00:56:55 +0200 |
|---|---|---|
| committer | jvoisin | 2018-06-10 00:56:55 +0200 |
| commit | 87bdcd1a9501e398bccb03fce606cf6925a75d16 (patch) | |
| tree | 5b2687defbad65f8720b938fe7a658524228974c /libmat2 | |
| parent | 3c56fa32375ff8df087d15e8649b661682bbb150 (diff) | |
Improve a bit our coverage wrt. torrent files handling
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/torrent.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/libmat2/torrent.py b/libmat2/torrent.py index f5935e6..b598065 100644 --- a/libmat2/torrent.py +++ b/libmat2/torrent.py | |||
| @@ -1,4 +1,6 @@ | |||
| 1 | import logging | ||
| 1 | from typing import Union, Tuple, Dict | 2 | from typing import Union, Tuple, Dict |
| 3 | |||
| 2 | from . import abstract | 4 | from . import abstract |
| 3 | 5 | ||
| 4 | 6 | ||
| @@ -58,6 +60,8 @@ class _BencodeHandler(object): | |||
| 58 | def __decode_int(s: bytes) -> Tuple[int, bytes]: | 60 | def __decode_int(s: bytes) -> Tuple[int, bytes]: |
| 59 | s = s[1:] | 61 | s = s[1:] |
| 60 | next_idx = s.index(b'e') | 62 | next_idx = s.index(b'e') |
| 63 | if next_idx is None: | ||
| 64 | raise ValueError # missing suffix | ||
| 61 | if s.startswith(b'-0'): | 65 | if s.startswith(b'-0'): |
| 62 | raise ValueError # negative zero doesn't exist | 66 | raise ValueError # negative zero doesn't exist |
| 63 | elif s.startswith(b'0') and next_idx != 1: | 67 | elif s.startswith(b'0') and next_idx != 1: |
| @@ -67,6 +71,8 @@ class _BencodeHandler(object): | |||
| 67 | @staticmethod | 71 | @staticmethod |
| 68 | def __decode_string(s: bytes) -> Tuple[bytes, bytes]: | 72 | def __decode_string(s: bytes) -> Tuple[bytes, bytes]: |
| 69 | sep = s.index(b':') | 73 | sep = s.index(b':') |
| 74 | if set is None: | ||
| 75 | raise ValueError # missing suffix | ||
| 70 | str_len = int(s[:sep]) | 76 | str_len = int(s[:sep]) |
| 71 | if str_len < 0: | 77 | if str_len < 0: |
| 72 | raise ValueError | 78 | raise ValueError |
| @@ -119,9 +125,9 @@ class _BencodeHandler(object): | |||
| 119 | try: | 125 | try: |
| 120 | r, l = self.__decode_func[s[0]](s) | 126 | r, l = self.__decode_func[s[0]](s) |
| 121 | except (IndexError, KeyError, ValueError) as e: | 127 | except (IndexError, KeyError, ValueError) as e: |
| 122 | print("not a valid bencoded string: %s" % e) | 128 | logging.debug("Not a valid bencoded string: %s" % e) |
| 123 | return None | 129 | return None |
| 124 | if l != b'': | 130 | if l != b'': |
| 125 | print("invalid bencoded value (data after valid prefix)") | 131 | logging.debug("Invalid bencoded value (data after valid prefix)") |
| 126 | return None | 132 | return None |
| 127 | return r | 133 | return r |
