Merge pull request #576 from arvidn/python3-readpiece-1.1

fix python3 binding for read_piece::buffer
This commit is contained in:
Arvid Norberg 2016-04-02 08:46:58 -04:00
commit 55c35332ba
3 changed files with 11 additions and 26 deletions

View File

@ -8,14 +8,15 @@
#include <libtorrent/piece_picker.hpp> // for piece_block #include <libtorrent/piece_picker.hpp> // for piece_block
#include <libtorrent/session_stats.hpp> #include <libtorrent/session_stats.hpp>
#include <memory> #include <memory>
#include "bytes.hpp"
using namespace boost::python; using namespace boost::python;
using namespace libtorrent; using namespace libtorrent;
std::string get_buffer(read_piece_alert const& rpa) bytes get_buffer(read_piece_alert const& rpa)
{ {
return rpa.buffer ? std::string(rpa.buffer.get(), rpa.size) return rpa.buffer ? bytes(rpa.buffer.get(), rpa.size)
: std::string(); : bytes();
} }
tuple endpoint_to_tuple(tcp::endpoint const& ep) tuple endpoint_to_tuple(tcp::endpoint const& ep)

View File

@ -4,6 +4,7 @@
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/python/tuple.hpp> #include <boost/python/tuple.hpp>
#include <boost/python/stl_iterator.hpp>
#include <libtorrent/torrent_handle.hpp> #include <libtorrent/torrent_handle.hpp>
#include <libtorrent/torrent_info.hpp> #include <libtorrent/torrent_info.hpp>
#include <libtorrent/torrent_status.hpp> #include <libtorrent/torrent_status.hpp>
@ -163,25 +164,9 @@ void prioritize_pieces(torrent_handle& info, object o)
void prioritize_files(torrent_handle& info, object o) void prioritize_files(torrent_handle& info, object o)
{ {
std::vector<int> result; std::vector<int> result;
try stl_input_iterator<int const> begin(o), end;
{ result.insert(result.begin(), begin, end);
object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
while( 1 )
{
#if PY_MAJOR_VERSION >= 3
object obj = extract<object>( iter_obj.attr( "__next__" )() );
#else
object obj = extract<object>( iter_obj.attr( "next" )() );
#endif
result.push_back(extract<int const>( obj ));
}
}
catch( error_already_set )
{
PyErr_Clear();
info.prioritize_files(result); info.prioritize_files(result);
return;
}
} }
list file_priorities(torrent_handle& handle) list file_priorities(torrent_handle& handle)

View File

@ -12,7 +12,6 @@ class test_torrent_handle(unittest.TestCase):
def test_torrent_handle(self): def test_torrent_handle(self):
ses = lt.session({'alert_mask': lt.alert.category_t.all_categories}) ses = lt.session({'alert_mask': lt.alert.category_t.all_categories})
shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'url_seed_multi.torrent'), '.')
ti = lt.torrent_info('url_seed_multi.torrent'); ti = lt.torrent_info('url_seed_multi.torrent');
h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()}) h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()})
@ -37,7 +36,6 @@ class test_torrent_info(unittest.TestCase):
self.assertEqual(info.total_size(), 1234) self.assertEqual(info.total_size(), 1234)
def test_metadata(self): def test_metadata(self):
shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'base.torrent'), '.')
ti = lt.torrent_info('base.torrent'); ti = lt.torrent_info('base.torrent');
self.assertTrue(len(ti.metadata()) != 0) self.assertTrue(len(ti.metadata()) != 0)
@ -48,7 +46,6 @@ class test_alerts(unittest.TestCase):
def test_alert(self): def test_alert(self):
ses = lt.session({'alert_mask': lt.alert.category_t.all_categories}) ses = lt.session({'alert_mask': lt.alert.category_t.all_categories})
shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'base.torrent'), '.')
ti = lt.torrent_info('base.torrent'); ti = lt.torrent_info('base.torrent');
h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()}) h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()})
st = h.status() st = h.status()
@ -106,5 +103,7 @@ class test_session(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
print(lt.__version__) 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'), '.')
unittest.main() unittest.main()