From 69f82c5274a9ca14376db61b1b80cf7672d830e2 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 27 Jul 2016 00:15:26 -0400 Subject: [PATCH] add support for entry::preformatted to python binding (#952) --- bindings/python/src/entry.cpp | 22 ++++++++++++++++++++++ bindings/python/test.py | 15 +++++++++++++++ test/Makefile.am | 1 + test/test_torrent_info.cpp | 1 + test/test_torrents/unordered.torrent | 1 + 5 files changed, 40 insertions(+) create mode 100644 test/test_torrents/unordered.torrent diff --git a/bindings/python/src/entry.cpp b/bindings/python/src/entry.cpp index bced76d65..5019920f1 100644 --- a/bindings/python/src/entry.cpp +++ b/bindings/python/src/entry.cpp @@ -45,6 +45,15 @@ struct entry_to_python return convert(e.list()); case entry::dictionary_t: return convert(e.dict()); + case entry::preformatted_t: + { + std::vector const& pre = e.preformatted(); + list l; + for (std::vector::const_iterator i = pre.begin() + , end(pre.end()); i != end; ++i) + l.append(*i); + return tuple(l); + } default: return object(); } @@ -136,6 +145,19 @@ struct entry_from_python { return entry(extract(e)()); } + else if (extract(e).check()) + { + tuple t = extract(e); + + std::size_t const length = extract(t.attr("__len__")()); + std::vector preformatted(length); + for (std::size_t i = 0; i < length; ++i) + { + preformatted[i] = extract(t[i]); + } + + return entry(preformatted); + } return entry(); } diff --git a/bindings/python/test.py b/bindings/python/test.py index 48c5a0f2a..d46e5b0ab 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -8,6 +8,20 @@ import os import shutil import binascii + +class test_create_torrent(unittest.TestCase): + + def test_from_torrent_info(self): + ti = lt.torrent_info('unordered.torrent') + ct = lt.create_torrent(ti) + entry = ct.generate() + content = lt.bencode(entry).strip() + with open('unordered.torrent', 'r') as f: + file_content = f.read().strip() + print file_content + print content + self.assertEqual(content, file_content) + class test_torrent_handle(unittest.TestCase): def test_torrent_handle(self): @@ -165,5 +179,6 @@ if __name__ == '__main__': print(lt.__version__) shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'url_seed_multi.torrent'), '.') shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'base.torrent'), '.') + shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'unordered.torrent'), '.') unittest.main() diff --git a/test/Makefile.am b/test/Makefile.am index ac8899f29..cb25fc037 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -94,6 +94,7 @@ EXTRA_DIST = Jamfile \ test_torrents/string.torrent \ test_torrents/symlink1.torrent \ test_torrents/unaligned_pieces.torrent \ + test_torrents/unordered.torrent \ test_torrents/url_list.torrent \ test_torrents/url_list2.torrent \ test_torrents/url_list3.torrent \ diff --git a/test/test_torrent_info.cpp b/test/test_torrent_info.cpp index 51f8ff471..87b092dc6 100644 --- a/test/test_torrent_info.cpp +++ b/test/test_torrent_info.cpp @@ -131,6 +131,7 @@ static test_torrent_t test_torrents[] = { "invalid_name2.torrent" }, { "invalid_name3.torrent" }, { "symlink1.torrent" }, + { "unordered.torrent" }, }; struct test_failing_torrent_t diff --git a/test/test_torrents/unordered.torrent b/test/test_torrents/unordered.torrent new file mode 100644 index 000000000..0d59f70dd --- /dev/null +++ b/test/test_torrents/unordered.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod4:name4:temp6:lengthi425e12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee