From e1e6c8bdc01f1dac5a56331e2ee1ca7255d36921 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 4 Apr 2015 22:37:11 +0000 Subject: [PATCH] fix python binding backwards compatibility --- bindings/python/src/create_torrent.cpp | 48 ++++++++++++++++++++++++-- bindings/python/src/torrent_info.cpp | 25 ++------------ include/libtorrent/bencode.hpp | 2 +- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/bindings/python/src/create_torrent.cpp b/bindings/python/src/create_torrent.cpp index dd22e9053..01917c5cc 100644 --- a/bindings/python/src/create_torrent.cpp +++ b/bindings/python/src/create_torrent.cpp @@ -59,6 +59,49 @@ namespace { ct.add_file(fe); } + + struct FileIter + { + typedef libtorrent::file_entry value_type; + typedef libtorrent::file_entry reference; + typedef libtorrent::file_entry* pointer; + typedef int difference_type; + typedef std::forward_iterator_tag iterator_category; + + FileIter(file_storage const& fs, int i) : m_fs(&fs), m_i(i) {} + FileIter(FileIter const& fi) : m_fs(fi.m_fs), m_i(fi.m_i) {} + FileIter() : m_fs(NULL), m_i(0) {} + libtorrent::file_entry operator*() const + { return m_fs->at(m_i); } + + FileIter operator++() { m_i++; return *this; } + FileIter operator++(int) { return FileIter(*m_fs, m_i++); } + + bool operator==(FileIter const& rhs) const + { return m_fs == rhs.m_fs && m_i == rhs.m_i; } + + int operator-(FileIter const& rhs) const + { + assert(rhs.m_fs == m_fs); + return m_i - rhs.m_i; + } + + FileIter& operator=(FileIter const& rhs) + { + m_fs = rhs.m_fs; + m_i = rhs.m_i; + return *this; + } + + file_storage const* m_fs; + int m_i; + }; + + FileIter begin_files(file_storage const& self) + { return FileIter(self, 0); } + + FileIter end_files(file_storage const& self) + { return FileIter(self, self.num_files()); } #endif char const* filestorage_name(file_storage const& fs) @@ -74,6 +117,7 @@ namespace { add_files(fs, file, boost::bind(&call_python_object2, cb, _1), flags); } + } void bind_create_torrent() @@ -120,11 +164,11 @@ void bind_create_torrent() #if !defined TORRENT_NO_DEPRECATE .def("at", at) .def("add_file", add_file, arg("entry")) + .def("__iter__", boost::python::range(&begin_files, &end_files)) + .def("__len__", &file_storage::num_files) #endif .def("hash", file_storage_hash) .def("symlink", file_storage_symlink, return_value_policy()) -// .def("file_base", &file_storage::file_base) -// .def("set_file_base", &file_storage::set_file_base) .def("file_path", file_storage_file_path, (arg("idx"), arg("save_path") = "")) .def("file_size", file_storage_file_size) .def("file_offset", file_storage_file_offset) diff --git a/bindings/python/src/torrent_info.cpp b/bindings/python/src/torrent_info.cpp index ebb03e774..20cf1c6ad 100644 --- a/bindings/python/src/torrent_info.cpp +++ b/bindings/python/src/torrent_info.cpp @@ -96,34 +96,13 @@ namespace } #endif -#if !defined TORRENT_NO_DEPRECATE - list files(torrent_info const& ti, bool storage) { - list result; - - for (int i = 0; i < ti.num_files(); ++i) - result.append(ti.files().at(i)); - - return result; - } - - list orig_files(torrent_info const& ti, bool storage) { - list result; - - file_storage const& st = ti.orig_files(); - - for (int i = 0; i < st.num_files(); ++i) - result.append(st.at(i)); - - return result; - } -#endif - std::string hash_for_piece(torrent_info const& ti, int i) { return ti.hash_for_piece(i).to_string(); } - std::string metadata(torrent_info const& ti) { + std::string metadata(torrent_info const& ti) + { std::string result(ti.metadata().get(), ti.metadata_size()); return result; } diff --git a/include/libtorrent/bencode.hpp b/include/libtorrent/bencode.hpp index 64b41db9c..c46076a5e 100644 --- a/include/libtorrent/bencode.hpp +++ b/include/libtorrent/bencode.hpp @@ -218,7 +218,7 @@ namespace libtorrent break; default: // trying to encode a structure with uninitialized values! - TORRENT_ASSERT_VAL(false, e.type()); +// TORRENT_ASSERT_VAL(false, e.type()); // do nothing break; }