Merge pull request #585 from arvidn/python-cleanup-1.1

cleanup python binding for torrent_handle::prioritize_pieces
This commit is contained in:
Arvid Norberg 2016-04-04 17:30:44 -04:00
commit e17409f996
1 changed files with 27 additions and 34 deletions

View File

@ -122,51 +122,44 @@ list get_peer_info(torrent_handle const& handle)
return result; return result;
} }
namespace
{
template <typename T>
T extract_fn(object o)
{
return boost::python::extract<T>(o);
}
}
void prioritize_pieces(torrent_handle& info, object o) void prioritize_pieces(torrent_handle& info, object o)
{ {
std::vector<int> result; stl_input_iterator<object> begin(o), end;
std::vector<std::pair<int, int> > piece_list; if (begin == end) return;
try
// determine which overload should be selected. the one taking a list of
// priorities or the one taking a list of piece -> priority mappings
bool const is_piece_list = extract<std::pair<int, int> >(*begin).check();
if (is_piece_list)
{ {
object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) )); std::vector<std::pair<int, int> > piece_list;
while( 1 ) std::transform(begin, end, std::back_inserter(piece_list)
{ , &extract_fn<std::pair<int, int> >);
#if PY_MAJOR_VERSION >= 3 info.prioritize_pieces(piece_list);
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())
{
result.push_back(val1);
continue;
}
extract<std::pair<int, int> > val2(obj);
if (val2.check())
{
piece_list.push_back(val2);
continue;
}
}
} }
catch( error_already_set ) else
{ {
PyErr_Clear(); std::vector<int> priority_vector;
if (result.size()) std::transform(begin, end, std::back_inserter(priority_vector)
info.prioritize_pieces(result); , &extract_fn<int>);
else info.prioritize_pieces(priority_vector);
info.prioritize_pieces(piece_list);
return;
} }
} }
void prioritize_files(torrent_handle& info, object o) void prioritize_files(torrent_handle& info, object o)
{ {
std::vector<int> result;
stl_input_iterator<int const> begin(o), end; stl_input_iterator<int const> begin(o), end;
result.insert(result.begin(), begin, end); info.prioritize_files(std::vector<int> (begin, end));
info.prioritize_files(result);
} }
list file_priorities(torrent_handle& handle) list file_priorities(torrent_handle& handle)