From f1c3ecd06722c960b661975d5a9d6740f0341657 Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 31 Mar 2016 00:19:23 -0400 Subject: [PATCH] fix metadata() and hash_for_piece() in python3 binding --- bindings/python/src/bytes.hpp | 7 +++++++ bindings/python/src/torrent_info.cpp | 9 ++++----- bindings/python/test.py | 7 +++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/bindings/python/src/bytes.hpp b/bindings/python/src/bytes.hpp index 640366f36..d2690a62d 100644 --- a/bindings/python/src/bytes.hpp +++ b/bindings/python/src/bytes.hpp @@ -9,7 +9,14 @@ struct bytes { + bytes(char const* s, int len): arr(s, len) {} bytes(std::string const& s): arr(s) {} +#if __cplusplus >= 201103L + bytes(std::string&& s): arr(std::move(s)) {} + bytes(bytes const&) = default; + bytes(bytes&&) = default; + bytes& operator=(bytes&&) = default; +#endif bytes() {} std::string arr; }; diff --git a/bindings/python/src/torrent_info.cpp b/bindings/python/src/torrent_info.cpp index a7a1f476d..fcdec046d 100644 --- a/bindings/python/src/torrent_info.cpp +++ b/bindings/python/src/torrent_info.cpp @@ -90,15 +90,14 @@ namespace ti.set_merkle_tree(h); } - std::string hash_for_piece(torrent_info const& ti, int i) + bytes hash_for_piece(torrent_info const& ti, int i) { - return ti.hash_for_piece(i).to_string(); + return bytes(ti.hash_for_piece(i).to_string()); } - std::string metadata(torrent_info const& ti) + bytes metadata(torrent_info const& ti) { - std::string result(ti.metadata().get(), ti.metadata_size()); - return result; + return bytes(ti.metadata().get(), ti.metadata_size()); } list map_block(torrent_info& ti, int piece, boost::int64_t offset, int size) diff --git a/bindings/python/test.py b/bindings/python/test.py index c7d59640a..bc7f60659 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -36,6 +36,13 @@ class test_torrent_info(unittest.TestCase): self.assertEqual(f.file_size(0), 1234) self.assertEqual(info.total_size(), 1234) + def test_metadata(self): + shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'base.torrent'), '.') + ti = lt.torrent_info('base.torrent'); + + self.assertTrue(len(ti.metadata()) != 0) + self.assertTrue(len(ti.hash_for_piece(0)) != 0) + class test_alerts(unittest.TestCase): def test_alert(self):