fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / bindings / python / src / entry.cpp
blobb24f6a2cfe970d008e8875d927f1f98768b44589
1 // Copyright Daniel Wallin 2006. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #include <libtorrent/session.hpp>
6 #include <boost/python.hpp>
8 using namespace boost::python;
9 using namespace libtorrent;
11 struct entry_to_python
13 static object convert(entry::list_type const& l)
15 list result;
17 for (entry::list_type::const_iterator i(l.begin()), e(l.end()); i != e; ++i)
19 result.append(*i);
22 return result;
25 static object convert(entry::dictionary_type const& d)
27 dict result;
29 for (entry::dictionary_type::const_iterator i(d.begin()), e(d.end()); i != e; ++i)
30 result[i->first] = i->second;
32 return result;
35 static object convert0(entry const& e)
37 switch (e.type())
39 case entry::int_t:
40 return object(e.integer());
41 case entry::string_t:
42 return object(e.string());
43 case entry::list_t:
44 return convert(e.list());
45 case entry::dictionary_t:
46 return convert(e.dict());
47 default:
48 return object();
52 static PyObject* convert(entry const& e)
54 return incref(convert0(e).ptr());
58 struct entry_from_python
60 entry_from_python()
62 converter::registry::push_back(
63 &convertible, &construct, type_id<entry>()
67 static void* convertible(PyObject* e)
69 return e;
72 static entry construct0(object e)
74 if (extract<dict>(e).check())
76 dict d = extract<dict>(e);
77 list items(d.items());
78 std::size_t length = extract<std::size_t>(items.attr("__len__")());
79 entry result(entry::dictionary_t);
81 for (std::size_t i = 0; i < length; ++i)
83 result.dict().insert(
84 std::make_pair(
85 extract<char const*>(items[i][0])()
86 , construct0(items[i][1])
91 return result;
93 else if (extract<list>(e).check())
95 list l = extract<list>(e);
97 std::size_t length = extract<std::size_t>(l.attr("__len__")());
98 entry result(entry::list_t);
100 for (std::size_t i = 0; i < length; ++i)
102 result.list().push_back(construct0(l[i]));
105 return result;
107 else if (extract<str>(e).check())
109 return entry(extract<std::string>(e)());
111 else if (extract<entry::integer_type>(e).check())
113 return entry(extract<entry::integer_type>(e)());
116 return entry();
119 static void construct(PyObject* e, converter::rvalue_from_python_stage1_data* data)
121 void* storage = ((converter::rvalue_from_python_storage<entry>*)data)->storage.bytes;
122 new (storage) entry(construct0(object(borrowed(e))));
123 data->convertible = storage;
127 void bind_entry()
129 to_python_converter<entry, entry_to_python>();
130 entry_from_python();