From a7dcdf30ef97ed27360dd5d08ae4be439a83e5bc Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 13 Nov 2016 20:52:56 -0500 Subject: [PATCH] cleanup and fixes to python bindings (#1317) --- bindings/python/src/alert.cpp | 93 ++++++++++++------------------ bindings/python/src/converters.cpp | 28 ++++++++- bindings/python/test.py | 8 ++- 3 files changed, 71 insertions(+), 58 deletions(-) diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index 28b512257..7f4541775 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -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 v(a.peers()); for (std::vector::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 by_value; + { scope alert_scope = class_("alert", no_init) .def("message", &alert::message) @@ -294,6 +252,7 @@ void bind_alert() class_, 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_, 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_, noncopyable>( @@ -392,6 +351,8 @@ void bind_alert() class_, 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_, 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_, noncopyable>( @@ -425,14 +388,16 @@ void bind_alert() ; class_, noncopyable>( - "metadata_failed_alert", no_init); + "metadata_failed_alert", no_init) + .def_readonly("error", &metadata_failed_alert::error) + ; class_, noncopyable>( "metadata_received_alert", no_init); class_, 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_, 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_, 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_, 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_, 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_, 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_, 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_, noncopyable>( @@ -568,7 +536,7 @@ void bind_alert() class_, 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_, noncopyable>( @@ -690,7 +659,7 @@ void bind_alert() class_, 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_, 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_, noncopyable>( "dht_immutable_item_alert", no_init) + .def_readonly("target", &dht_immutable_item_alert::target) .add_property("item", &dht_immutable_item) ; class_, 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_, 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_, noncopyable>( "session_stats_alert", no_init) .add_property("values", &session_stats_values) diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index b7981ce7c..d69f52c92 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -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 struct pair_to_tuple { static PyObject* convert(const std::pair& p) { - return incref(make_tuple(p.first, p.second).ptr()); + return incref(bp::make_tuple(p.first, p.second).ptr()); + } +}; + +template +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, pair_to_tuple >(); + to_python_converter >(); + to_python_converter >(); + to_python_converter(); tuple_to_pair(); } diff --git a/bindings/python/test.py b/bindings/python/test.py index c3115e74d..9e0fa44c3 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -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')