def load_content(self): """Reads the torrent file and decodes content. .. note:: bencode is supremely more efficient parser for torrents but extremely strict in the format of the file. A custom parser based on another implementation handles parsing that is more flexible but it is not as efficient. Therefore when the file is well formed bencode is used but if it fails then the custom parser is used. If the custom parser fails then a ParsingError is raised. """ with io.open(file=self.file, mode='rb') as handle: content = handle.read() try: return bencode.bdecode(content) except bencode.BTFailure as bterr: LOG.info('bencode.bdecode failed: (%s); trying alternate approach', bterr) return Bdecode.parse(content)
def test_roundtrip_decoded(self): for plain, encoded in self.knownValues: result = bencode.bencode(plain) self.assertEqual(plain, bencode.bdecode(result))
def test_bdecode_known_values(self): for plain, encoded in self.knownValues: result = bencode.bdecode(encoded) self.assertEqual(plain, result)