Merge pull request #571 from arvidn/fix-python3-1.1

fix python3 issue in binding
This commit is contained in:
Arvid Norberg 2016-03-31 08:29:33 -04:00
commit e3db774828
5 changed files with 43 additions and 6 deletions

View File

@ -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;
};

View File

@ -130,7 +130,11 @@ void prioritize_pieces(torrent_handle& info, object o)
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
extract<int const> val1(obj);
if (val1.check())
{
@ -164,7 +168,11 @@ void prioritize_files(torrent_handle& info, object o)
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 ));
}
}

View File

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

View File

@ -8,7 +8,19 @@ import os
import shutil
import binascii
# test torrent_info
class test_torrent_handle(unittest.TestCase):
def test_torrent_handle(self):
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');
h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()})
h.prioritize_files([0,1])
self.assertEqual(h.file_priorities(), [0,1])
h.prioritize_pieces([0])
self.assertEqual(h.piece_priorities(), [0])
class test_torrent_info(unittest.TestCase):
@ -24,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):
@ -75,6 +94,7 @@ class test_sha1hash(unittest.TestCase):
class test_session(unittest.TestCase):
def test_post_session_stats(self):
s = lt.session({'alert_mask': lt.alert.category_t.stats_notification})
s.post_session_stats()
@ -83,6 +103,7 @@ class test_session(unittest.TestCase):
self.assertTrue(isinstance(a.values, dict))
self.assertTrue(len(a.values) > 0)
if __name__ == '__main__':
print(lt.__version__)
unittest.main()

View File

@ -5493,6 +5493,8 @@ namespace libtorrent
TORRENT_ASSERT(num_have() >= m_picker->num_have_filtered());
}
update_gauge();
update_want_tick();
if (filter_updated)
{
// we need to save this new state