From 1a4c017ec5dc726324aff51347d8a56c9c5515f0 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 25 Jul 2015 16:40:07 -0700 Subject: [PATCH] merged torrent_info constructor fix (python bindings) from RC_1_0 --- .travis.yml | 1 + bindings/python/src/create_torrent.cpp | 1 + bindings/python/src/torrent_info.cpp | 58 ++++++++++++++++++++------ bindings/python/test.py | 38 +++++++++++++++++ docs/python_binding.html | 3 +- docs/python_binding.rst | 3 +- include/libtorrent/torrent_info.hpp | 10 +---- 7 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 bindings/python/test.py diff --git a/.travis.yml b/.travis.yml index 795f62560..5775724a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,4 +47,5 @@ script: - bjam -j 3 variant=$variant warnings=off $CC - cd ../bindings/python - bjam -j 3 variant=$variant warnings=off $CC + - python test.py - ccache --show-stats diff --git a/bindings/python/src/create_torrent.cpp b/bindings/python/src/create_torrent.cpp index 9dd6697e6..8884c2995 100644 --- a/bindings/python/src/create_torrent.cpp +++ b/bindings/python/src/create_torrent.cpp @@ -154,6 +154,7 @@ void bind_create_torrent() file_entry (file_storage::*at)(int) const = &file_storage::at; #endif + // TODO: 3 move this to its own file class_("file_storage") .def("is_valid", &file_storage::is_valid) .def("add_file", add_file0, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = "")) diff --git a/bindings/python/src/torrent_info.cpp b/bindings/python/src/torrent_info.cpp index 20cf1c6ad..c81918be2 100644 --- a/bindings/python/src/torrent_info.cpp +++ b/bindings/python/src/torrent_info.cpp @@ -143,26 +143,62 @@ namespace } // namespace unnamed -boost::shared_ptr buffer_constructor(char const* buf, int len, int flags) +boost::shared_ptr buffer_constructor0(char const* buf, int len, int flags) { error_code ec; - boost::shared_ptr ret(new torrent_info(buf, len, ec, flags)); + boost::shared_ptr ret(boost::make_shared(buf, len, ec, flags)); #ifndef BOOST_NO_EXCEPTIONS if (ec) throw libtorrent_exception(ec); #endif return ret; } -boost::shared_ptr file_constructor(std::string const& filename, int flags) +boost::shared_ptr buffer_constructor1(char const* buf, int len) +{ + return buffer_constructor0(buf, len, 0); +} + +boost::shared_ptr file_constructor0(std::string const& filename, int flags) { error_code ec; - boost::shared_ptr ret(new torrent_info(filename, ec, flags)); + boost::shared_ptr ret(boost::make_shared(filename, ec, flags)); #ifndef BOOST_NO_EXCEPTIONS if (ec) throw libtorrent_exception(ec); #endif return ret; } +boost::shared_ptr file_constructor1(std::string const& filename) +{ + return file_constructor0(filename, 0); +} + +boost::shared_ptr bencoded_constructor0(entry const& ent, int flags) +{ + std::vector buf; + bencode(std::back_inserter(buf), ent); + + bdecode_node e; + error_code ec; + if (buf.size() == 0 || bdecode(&buf[0], &buf[0] + buf.size(), e, ec) != 0) + { +#ifndef BOOST_NO_EXCEPTIONS + throw invalid_torrent_file(ec); +#endif + } + + boost::shared_ptr ret(boost::make_shared(e, ec, flags)); +#ifndef BOOST_NO_EXCEPTIONS + if (ec) throw libtorrent_exception(ec); +#endif + return ret; +} + +boost::shared_ptr bencoded_constructor1(entry const& ent) +{ + return bencoded_constructor0(ent, 0); +} + void bind_torrent_info() { return_value_policy copy; @@ -179,15 +215,13 @@ void bind_torrent_info() ; class_ >("torrent_info", no_init) -#ifndef TORRENT_NO_DEPRECATE -#ifndef BOOST_NO_EXCEPTIONS - .def(init(arg("e"))) -#endif -#endif - .def(init((arg("info_hash"), arg("flags") = 0))) - .def("__init__", make_constructor(&buffer_constructor)) - .def("__init__", make_constructor(&file_constructor)) + .def("__init__", make_constructor(&buffer_constructor0)) + .def("__init__", make_constructor(&buffer_constructor1)) + .def("__init__", make_constructor(&file_constructor0)) + .def("__init__", make_constructor(&file_constructor1)) + .def("__init__", make_constructor(&bencoded_constructor0)) + .def("__init__", make_constructor(&bencoded_constructor1)) .def(init((arg("ti")))) #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE diff --git a/bindings/python/test.py b/bindings/python/test.py new file mode 100644 index 000000000..a3e4b5dbf --- /dev/null +++ b/bindings/python/test.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import libtorrent as lt + +import unittest + +# test torrent_info + +class test_torrent_info(unittest.TestCase): + + def test_bencoded_constructor(self): + info = lt.torrent_info({ 'info': {'name': 'test_torrent', 'length': 1234, + 'piece length': 16 * 1024, + 'pieces': 'aaaaaaaaaaaaaaaaaaaa'}}) + + self.assertEqual(info.num_files(), 1) + + f = info.files() + self.assertEqual(f.file_path(0), 'test_torrent') + self.assertEqual(f.file_size(0), 1234) + self.assertEqual(info.total_size(), 1234) + +class test_bencoder(unittest.TestCase): + + def test_bencode(self): + + encoded = lt.bencode({'a': 1, 'b': [1,2,3], 'c': 'foo'}) + self.assertEqual(encoded, 'd1:ai1e1:bli1ei2ei3ee1:c3:fooe') + + def test_bdecode(self): + + encoded = 'd1:ai1e1:bli1ei2ei3ee1:c3:fooe' + decoded = lt.bdecode(encoded) + self.assertEqual(decoded, {'a': 1, 'b': [1,2,3], 'c': 'foo'}) + +if __name__ == '__main__': + unittest.main() + diff --git a/docs/python_binding.html b/docs/python_binding.html index 19e572ed0..92c42ec24 100644 --- a/docs/python_binding.html +++ b/docs/python_binding.html @@ -156,7 +156,8 @@ params = { save_path: '.', \ ti: info } h = ses.add_torrent(params) -while (not h.is_seed()): +s = h.status() +while (not s.is_seeding): s = h.status() state_str = ['queued', 'checking', 'downloading metadata', \ diff --git a/docs/python_binding.rst b/docs/python_binding.rst index a3c12adb1..18bc1d6d8 100644 --- a/docs/python_binding.rst +++ b/docs/python_binding.rst @@ -127,7 +127,8 @@ A very simple example usage of the module would be something like this:: ti: info } h = ses.add_torrent(params) - while (not h.is_seed()): + s = h.status() + while (not s.is_seeding): s = h.status() state_str = ['queued', 'checking', 'downloading metadata', \ diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index 4d7c63227..d19be74ee 100644 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -303,14 +303,6 @@ namespace libtorrent torrent_info(bdecode_node const& torrent_file, int flags = 0); torrent_info(char const* buffer, int size, int flags = 0); torrent_info(std::string const& filename, int flags = 0); -#ifndef TORRENT_NO_DEPRECATE -#if TORRENT_USE_WSTRING - // all wstring APIs are deprecated since 0.16.11 instead, use the wchar - // -> utf8 conversion functions and pass in utf8 strings - TORRENT_DEPRECATED - torrent_info(std::wstring const& filename, int flags = 0); -#endif // TORRENT_USE_WSTRING -#endif // TORRENT_NO_DEPRECATE #endif // BOOST_NO_EXCEPTIONS torrent_info(torrent_info const& t); torrent_info(sha1_hash const& info_hash, int flags = 0); @@ -329,6 +321,8 @@ namespace libtorrent TORRENT_DEPRECATED torrent_info(std::wstring const& filename, error_code& ec , int flags = 0); + TORRENT_DEPRECATED + torrent_info(std::wstring const& filename, int flags = 0); #endif // TORRENT_USE_WSTRING #endif // TORRENT_NO_DEPRECATE