cleanup and fixes to python bindings (#1317)

This commit is contained in:
Arvid Norberg 2016-11-13 20:52:56 -05:00 committed by GitHub
parent d92f9ecc50
commit a7dcdf30ef
3 changed files with 71 additions and 58 deletions

View File

@ -19,49 +19,6 @@ bytes get_buffer(read_piece_alert const& rpa)
: bytes();
}
tuple endpoint_to_tuple(tcp::endpoint const& ep)
{
return boost::python::make_tuple(ep.address().to_string(), ep.port());
}
tuple endpoint_to_tuple(udp::endpoint const& ep)
{
return boost::python::make_tuple(ep.address().to_string(), ep.port());
}
tuple peer_alert_ip(peer_alert const& pa)
{
return endpoint_to_tuple(pa.ip);
}
std::string peer_blocked_alert_ip(peer_blocked_alert const& pa)
{
error_code ec;
return pa.ip.to_string(ec);
}
std::string dht_announce_alert_ip(dht_announce_alert const& pa)
{
error_code ec;
return pa.ip.to_string(ec);
}
tuple incoming_connection_alert_ip(incoming_connection_alert const& ica)
{
return endpoint_to_tuple(ica.ip);
}
tuple dht_outgoing_get_peers_alert_ip(dht_outgoing_get_peers_alert const& a)
{
return endpoint_to_tuple(a.ip);
}
std::string external_ip_alert_ip(external_ip_alert const& eia)
{
return eia.external_address.to_string();
}
list stats_alert_transferred(stats_alert const& alert)
{
list result;
@ -197,13 +154,12 @@ dict session_stats_values(session_stats_alert const& alert)
list dht_get_peers_reply_alert_peers(dht_get_peers_reply_alert const& a)
{
list result;
std::vector<tcp::endpoint> v(a.peers());
for (std::vector<tcp::endpoint>::const_iterator i = v.begin();
i != v.end(); ++i)
{
result.append(endpoint_to_tuple(*i));
result.append(*i);
}
return result;
@ -221,6 +177,8 @@ void bind_alert()
typedef alert alert_holder;
#endif
typedef return_value_policy<return_by_value> by_value;
{
scope alert_scope = class_<alert, alert_holder, noncopyable >("alert", no_init)
.def("message", &alert::message)
@ -294,6 +252,7 @@ void bind_alert()
class_<read_piece_alert, bases<torrent_alert>, noncopyable>(
"read_piece_alert", 0, no_init)
.def_readonly("ec", &read_piece_alert::ec)
.add_property("buffer", get_buffer)
.def_readonly("piece", &read_piece_alert::piece)
.def_readonly("size", &read_piece_alert::size)
@ -301,7 +260,7 @@ void bind_alert()
class_<peer_alert, bases<torrent_alert>, noncopyable>(
"peer_alert", no_init)
.add_property("ip", &peer_alert_ip)
.add_property("ip", make_getter(&peer_alert::ip, by_value()))
.def_readonly("pid", &peer_alert::pid)
;
class_<tracker_error_alert, bases<tracker_alert>, noncopyable>(
@ -392,6 +351,8 @@ void bind_alert()
class_<storage_moved_failed_alert, bases<torrent_alert>, noncopyable>(
"storage_moved_failed_alert", no_init)
.def_readonly("error", &storage_moved_failed_alert::error)
.def("file_path", &storage_moved_failed_alert::file_path)
.def_readonly("operation", &storage_moved_failed_alert::operation)
;
class_<torrent_deleted_alert, bases<torrent_alert>, noncopyable>(
@ -411,7 +372,9 @@ void bind_alert()
.def_readonly("url", &url_seed_alert::url)
.def_readonly("msg", &url_seed_alert::msg)
#endif
.def_readonly("error", &url_seed_alert::error)
.def("server_url", &url_seed_alert::server_url)
.def("error_message", &url_seed_alert::error_message)
;
class_<file_error_alert, bases<torrent_alert>, noncopyable>(
@ -425,14 +388,16 @@ void bind_alert()
;
class_<metadata_failed_alert, bases<torrent_alert>, noncopyable>(
"metadata_failed_alert", no_init);
"metadata_failed_alert", no_init)
.def_readonly("error", &metadata_failed_alert::error)
;
class_<metadata_received_alert, bases<torrent_alert>, noncopyable>(
"metadata_received_alert", no_init);
class_<listen_failed_alert, bases<alert>, noncopyable>(
"listen_failed_alert", no_init)
.def_readonly("endpoint", &listen_failed_alert::endpoint)
.add_property("endpoint", make_getter(&listen_failed_alert::endpoint, by_value()))
.def("listen_interface", &listen_failed_alert::listen_interface)
.def_readonly("error", &listen_failed_alert::error)
.def_readonly("operation", &listen_failed_alert::operation)
@ -441,7 +406,7 @@ void bind_alert()
class_<listen_succeeded_alert, bases<alert>, noncopyable>(
"listen_succeeded_alert", no_init)
.def_readonly("endpoint", &listen_succeeded_alert::endpoint)
.add_property("endpoint", make_getter(&listen_succeeded_alert::endpoint, by_value()))
;
class_<portmap_error_alert, bases<alert>, noncopyable>(
@ -484,11 +449,13 @@ void bind_alert()
#ifndef TORRENT_NO_DEPRECATE
.def_readonly("msg", &fastresume_rejected_alert::msg)
#endif
.def("file_path", &fastresume_rejected_alert::file_path)
.def_readonly("operation", &fastresume_rejected_alert::operation)
;
class_<peer_blocked_alert, bases<alert>, noncopyable>(
"peer_blocked_alert", no_init)
.add_property("ip", &peer_blocked_alert_ip)
.add_property("ip", make_getter(&peer_blocked_alert::ip, by_value()))
;
class_<scrape_reply_alert, bases<tracker_alert>, noncopyable>(
@ -503,17 +470,18 @@ void bind_alert()
.def_readonly("msg", &scrape_failed_alert::msg)
#endif
.def("error_message", &scrape_failed_alert::error_message)
.def_readonly("error", &scrape_failed_alert::error)
;
class_<udp_error_alert, bases<alert>, noncopyable>(
"udp_error_alert", no_init)
.def_readonly("endpoint", &udp_error_alert::endpoint)
.add_property("endpoint", make_getter(&udp_error_alert::endpoint, by_value()))
.def_readonly("error", &udp_error_alert::error)
;
class_<external_ip_alert, bases<alert>, noncopyable>(
"external_ip_alert", no_init)
.add_property("external_address", &external_ip_alert_ip)
.add_property("external_address", make_getter(&external_ip_alert::external_address, by_value()))
;
class_<save_resume_data_alert, bases<torrent_alert>, noncopyable>(
@ -568,7 +536,7 @@ void bind_alert()
class_<dht_announce_alert, bases<alert>, noncopyable>(
"dht_announce_alert", no_init)
.add_property("ip", &dht_announce_alert_ip)
.add_property("ip", make_getter(&dht_announce_alert::ip, by_value()))
.def_readonly("port", &dht_announce_alert::port)
.def_readonly("info_hash", &dht_announce_alert::info_hash)
;
@ -623,6 +591,7 @@ void bind_alert()
.def_readonly("msg", &torrent_delete_failed_alert::msg)
#endif
.def_readonly("error", &torrent_delete_failed_alert::error)
.def_readonly("info_hash", &torrent_delete_failed_alert::info_hash)
;
class_<save_resume_data_failed_alert, bases<torrent_alert>, noncopyable>(
@ -690,7 +659,7 @@ void bind_alert()
class_<incoming_connection_alert, bases<alert>, noncopyable>(
"incoming_connection_alert", no_init)
.def_readonly("socket_type", &incoming_connection_alert::socket_type)
.add_property("ip", &incoming_connection_alert_ip)
.add_property("ip", make_getter(&incoming_connection_alert::ip, by_value()))
;
class_<torrent_need_cert_alert, bases<torrent_alert>, noncopyable>(
"torrent_need_cert_alert", no_init)
@ -713,7 +682,7 @@ void bind_alert()
"dht_outgoing_get_peers_alert", no_init)
.def_readonly("info_hash", &dht_outgoing_get_peers_alert::info_hash)
.def_readonly("obfuscated_info_hash", &dht_outgoing_get_peers_alert::obfuscated_info_hash)
.add_property("ip", &dht_outgoing_get_peers_alert_ip)
.add_property("ip", make_getter(&dht_outgoing_get_peers_alert::ip, by_value()))
;
#ifndef TORRENT_DISABLE_LOGGING
@ -754,18 +723,30 @@ void bind_alert()
class_<dht_immutable_item_alert, bases<alert>, noncopyable>(
"dht_immutable_item_alert", no_init)
.def_readonly("target", &dht_immutable_item_alert::target)
.add_property("item", &dht_immutable_item)
;
class_<dht_mutable_item_alert, bases<alert>, noncopyable>(
"dht_mutable_item_alert", no_init)
.def_readonly("key", &dht_mutable_item_alert::key)
.def_readonly("signature", &dht_mutable_item_alert::signature)
.def_readonly("seq", &dht_mutable_item_alert::seq)
.def_readonly("salt", &dht_mutable_item_alert::salt)
.add_property("item", &dht_mutable_item)
.def_readonly("authoritative", &dht_mutable_item_alert::authoritative)
;
class_<dht_put_alert, bases<alert>, noncopyable>(
"dht_put_alert", no_init)
.add_property("item", &dht_put_item)
.def_readonly("target", &dht_put_alert::target)
.def_readonly("public_key", &dht_put_alert::public_key)
.def_readonly("signature", &dht_put_alert::signature)
.def_readonly("salt", &dht_put_alert::salt)
.def_readonly("seq", &dht_put_alert::seq)
.def_readonly("num_success", &dht_put_alert::num_success)
;
class_<session_stats_alert, bases<alert>, noncopyable>(
"session_stats_alert", no_init)
.add_property("values", &session_stats_values)

View File

@ -3,15 +3,37 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "boost_python.hpp"
#include "libtorrent/address.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/error_code.hpp"
using namespace boost::python;
namespace bp = boost::python;
template<class T1, class T2>
struct pair_to_tuple
{
static PyObject* convert(const std::pair<T1, T2>& p)
{
return incref(make_tuple(p.first, p.second).ptr());
return incref(bp::make_tuple(p.first, p.second).ptr());
}
};
template <typename Endpoint>
struct endpoint_to_tuple
{
static PyObject* convert(Endpoint const& ep)
{
return incref(bp::object(bp::make_tuple(ep.address().to_string(), ep.port())).ptr());
}
};
struct address_to_tuple
{
static PyObject* convert(libtorrent::address const& addr)
{
libtorrent::error_code ec;
return incref(bp::object(addr.to_string(ec)).ptr());
}
};
@ -46,6 +68,10 @@ struct tuple_to_pair
void bind_converters()
{
namespace lt = libtorrent;
to_python_converter<std::pair<int, int>, pair_to_tuple<int, int> >();
to_python_converter<lt::tcp::endpoint, endpoint_to_tuple<lt::tcp::endpoint> >();
to_python_converter<lt::udp::endpoint, endpoint_to_tuple<lt::udp::endpoint> >();
to_python_converter<lt::address, address_to_tuple>();
tuple_to_pair<int, int>();
}

View File

@ -8,7 +8,6 @@ import os
import shutil
import binascii
class test_create_torrent(unittest.TestCase):
def test_from_torrent_info(self):
@ -101,6 +100,13 @@ class test_alerts(unittest.TestCase):
alerts = ses.pop_alerts()
for a in alerts:
print(a.message())
for field_name in dir(a):
if field_name.startswith('__'): continue
field = getattr(a, field_name)
if callable(field):
print(' ', field_name, ' = ', field())
else:
print(' ', field_name, ' = ', field)
print(st.next_announce)
self.assertEqual(st.name, 'temp')