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):