From 49b24d4cd08e8a52ada423608526888592092041 Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 5 Jan 2016 01:32:04 -0500 Subject: [PATCH] fix python bindings --- bindings/python/src/alert.cpp | 7 ++- bindings/python/src/session.cpp | 69 ++++++++++++++++++++++------ bindings/python/src/torrent_info.cpp | 4 +- bindings/python/test.py | 5 +- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index 3b5d8af8d..9de6a5082 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -182,9 +182,14 @@ dict dht_put_item(dht_put_alert const& alert) void bind_alert() { using boost::noncopyable; +#ifndef TORRENT_NO_DEPRECATE + typedef boost::shared_ptr alert_holder; +#else + typedef alert alert_holder; +#endif { - scope alert_scope = class_, noncopyable >("alert", no_init) + scope alert_scope = class_("alert", no_init) .def("message", &alert::message) .def("what", &alert::what) .def("category", &alert::category) diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index f86e80df8..3bf51bc38 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -98,9 +98,8 @@ namespace #endif // TORRENT_DISABLE_EXTENSIONS } - void session_set_settings(lt::session& ses, dict const& sett_dict) + void make_settings_pack(lt::settings_pack& p, dict const& sett_dict) { - settings_pack p; list iterkeys = (list)sett_dict.iterkeys(); for (int i = 0; i < boost::python::len(iterkeys); i++) { @@ -127,9 +126,20 @@ namespace } TORRENT_CATCH(...) {} } + } + boost::shared_ptr make_session(boost::python::dict sett, int flags) + { + settings_pack p; + make_settings_pack(p, sett); + return boost::make_shared(p, flags); + } + + void session_set_settings(lt::session& ses, dict const& sett_dict) + { + settings_pack p; + make_settings_pack(p, sett_dict); allow_threading_guard guard; - ses.apply_settings(p); } @@ -377,10 +387,21 @@ namespace } #endif - alert const* wait_for_alert(lt::session& s, int ms) +#ifndef TORRENT_NO_DEPRECATE + boost::shared_ptr +#else + alert const* +#endif + wait_for_alert(lt::session& s, int ms) { allow_threading_guard guard; - return s.wait_for_alert(milliseconds(ms)); + alert const* a = s.wait_for_alert(milliseconds(ms)); +#ifndef TORRENT_NO_DEPRECATE + if (a == NULL) return boost::shared_ptr(); + return boost::shared_ptr(a->clone().release()); +#else + return a; +#endif } list get_torrents(lt::session& s) @@ -472,7 +493,6 @@ namespace return object(boost::shared_ptr(a.release())); } -#endif list pop_alerts(lt::session& ses) { @@ -486,10 +506,28 @@ namespace for (std::vector::iterator i = alerts.begin() , end(alerts.end()); i != end; ++i) { - ret.append(boost::shared_ptr(*i)); + ret.append(boost::shared_ptr((*i)->clone().release())); } return ret; } +#else + list pop_alerts(lt::session& ses) + { + std::vector alerts; + { + allow_threading_guard guard; + ses.pop_alerts(&alerts); + } + + list ret; + for (std::vector::iterator i = alerts.begin() + , end(alerts.end()); i != end; ++i) + { + ret.append(boost::python::ptr(*i)); + } + return ret; + } +#endif void load_state(lt::session& ses, entry const& st) { @@ -694,10 +732,11 @@ void bind_session() ; class_("session", no_init) - .def( - init(( - arg("settings") - , arg("flags")=lt::session::start_default_features | lt::session::add_default_plugins)) + .def("__init__", boost::python::make_constructor(&make_session + , default_call_policies() + , (arg("settings") + , arg("flags")=lt::session::start_default_features + | lt::session::add_default_plugins)) ) #ifndef TORRENT_NO_DEPRECATE .def( @@ -762,7 +801,11 @@ void bind_session() .def("load_state", &load_state) .def("save_state", &save_state, (arg("entry"), arg("flags") = 0xffffffff)) .def("pop_alerts", &pop_alerts) - .def("wait_for_alert", &wait_for_alert, return_internal_reference<>()) + .def("wait_for_alert", &wait_for_alert +#ifdef TORRENT_NO_DEPRECATE + , return_internal_reference<>() +#endif + ) .def("add_extension", &add_extension) #ifndef TORRENT_NO_DEPRECATE .def("pop_alert", &pop_alert) @@ -869,8 +912,6 @@ void bind_session() ; #endif - register_ptr_to_python >(); - typedef void (*mem_preset2)(settings_pack& s); typedef void (*perf_preset2)(settings_pack& s); diff --git a/bindings/python/src/torrent_info.cpp b/bindings/python/src/torrent_info.cpp index 0fd3cf7fd..a7a1f476d 100644 --- a/bindings/python/src/torrent_info.cpp +++ b/bindings/python/src/torrent_info.cpp @@ -213,12 +213,12 @@ void bind_torrent_info() class_ >("torrent_info", no_init) .def(init((arg("info_hash"), arg("flags") = 0))) + .def("__init__", make_constructor(&bencoded_constructor0)) + .def("__init__", make_constructor(&bencoded_constructor1)) .def("__init__", make_constructor(&buffer_constructor0)) .def("__init__", make_constructor(&buffer_constructor1)) .def("__init__", make_constructor(&file_constructor0)) .def("__init__", make_constructor(&file_constructor1)) - .def("__init__", make_constructor(&bencoded_constructor0)) - .def("__init__", make_constructor(&bencoded_constructor1)) .def(init((arg("ti")))) #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE diff --git a/bindings/python/test.py b/bindings/python/test.py index 01fbd11fa..ecfcdd4e8 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -28,10 +28,7 @@ class test_alerts(unittest.TestCase): def test_alert(self): - ses = lt.session() - sett = lt.session_settings() - sett.alert_mask = 0xffffffff - ses.set_alert_mask(0xfffffff) + 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'); h = ses.add_torrent({'ti': ti, 'save_path': '.'})