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)
17
for (entry::list_type::const_iterator i(l.begin()), e(l.end()); i != e; ++i)
25
static object convert(entry::dictionary_type const& d)
29
for (entry::dictionary_type::const_iterator i(d.begin()), e(d.end()); i != e; ++i)
30
result[i->first] = i->second;
35
static object convert0(entry const& e)
40
return object(e.integer());
42
return object(e.string());
44
return convert(e.list());
45
case entry::dictionary_t:
46
return convert(e.dict());
52
static PyObject* convert(entry const& e)
54
return incref(convert0(e).ptr());
58
struct entry_from_python
62
converter::registry::push_back(
63
&convertible, &construct, type_id<entry>()
67
static void* convertible(PyObject* 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)
85
extract<char const*>(items[i][0])()
86
, construct0(items[i][1])
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]));
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)());
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;
129
to_python_converter<entry, entry_to_python>();