2011-06-25 22:11:31 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2011, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-08-14 00:49:49 +02:00
|
|
|
#include "boost_python.hpp"
|
2011-06-25 22:11:31 +02:00
|
|
|
#include <libtorrent/error_code.hpp>
|
2015-03-12 06:20:12 +01:00
|
|
|
#include <libtorrent/bdecode.hpp>
|
2014-02-02 04:05:55 +01:00
|
|
|
#include <libtorrent/upnp.hpp>
|
2014-02-02 05:07:36 +01:00
|
|
|
#include <libtorrent/socks5_stream.hpp>
|
2016-07-04 05:30:52 +02:00
|
|
|
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
// this fixe mysterious link error on msvc
|
2017-08-14 00:49:49 +02:00
|
|
|
template <>
|
|
|
|
inline boost::system::error_category const volatile*
|
|
|
|
get_pointer(class boost::system::error_category const volatile* p)
|
2016-07-04 05:30:52 +02:00
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 01:04:36 +01:00
|
|
|
#include <boost/asio/error.hpp>
|
|
|
|
#if defined TORRENT_USE_OPENSSL
|
2017-10-09 01:21:19 +02:00
|
|
|
#include <boost/asio/ssl.hpp>
|
|
|
|
#endif
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
#include <libtorrent/i2p_stream.hpp>
|
2017-02-22 01:04:36 +01:00
|
|
|
#endif
|
2011-06-25 22:11:31 +02:00
|
|
|
|
|
|
|
using namespace boost::python;
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2014-02-01 05:02:58 +01:00
|
|
|
using boost::system::error_category;
|
2011-06-25 22:11:31 +02:00
|
|
|
|
2017-02-22 01:04:36 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct ec_pickle_suite : boost::python::pickle_suite
|
|
|
|
{
|
|
|
|
static boost::python::tuple
|
2018-04-12 14:38:05 +02:00
|
|
|
getinitargs(error_code const&)
|
2017-02-22 01:04:36 +01:00
|
|
|
{
|
|
|
|
return boost::python::tuple();
|
|
|
|
}
|
|
|
|
|
|
|
|
static boost::python::tuple
|
|
|
|
getstate(error_code const& ec)
|
|
|
|
{
|
|
|
|
return boost::python::make_tuple(ec.value(), ec.category().name());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setstate(error_code& ec, boost::python::tuple state)
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
|
|
|
if (len(state) != 2)
|
|
|
|
{
|
|
|
|
PyErr_SetObject(PyExc_ValueError,
|
|
|
|
("expected 2-item tuple in call to __setstate__; got %s"
|
|
|
|
% state).ptr());
|
|
|
|
throw_error_already_set();
|
|
|
|
}
|
|
|
|
|
|
|
|
int const value = extract<int>(state[0]);
|
|
|
|
std::string const category = extract<std::string>(state[1]);
|
|
|
|
if (category == "system")
|
2017-04-12 20:05:53 +02:00
|
|
|
ec.assign(value, lt::system_category());
|
2017-02-22 01:04:36 +01:00
|
|
|
else if (category == "generic")
|
2017-04-12 20:05:53 +02:00
|
|
|
ec.assign(value, lt::generic_category());
|
2017-02-22 01:04:36 +01:00
|
|
|
else if (category == "libtorrent")
|
2017-04-12 20:05:53 +02:00
|
|
|
ec.assign(value, lt::libtorrent_category());
|
2017-02-22 01:04:36 +01:00
|
|
|
else if (category == "http error")
|
2017-04-12 20:05:53 +02:00
|
|
|
ec.assign(value, lt::http_category());
|
2017-02-22 01:04:36 +01:00
|
|
|
else if (category == "UPnP error")
|
2017-04-12 20:05:53 +02:00
|
|
|
ec.assign(value, lt::upnp_category());
|
2017-02-22 01:04:36 +01:00
|
|
|
else if (category == "bdecode error")
|
2017-04-12 20:05:53 +02:00
|
|
|
ec.assign(value, lt::bdecode_category());
|
2017-02-22 01:04:36 +01:00
|
|
|
else if (category == "asio.netdb")
|
|
|
|
ec.assign(value, boost::asio::error::get_netdb_category());
|
|
|
|
else if (category == "asio.addinfo")
|
|
|
|
ec.assign(value, boost::asio::error::get_addrinfo_category());
|
|
|
|
else if (category == "asio.misc")
|
|
|
|
ec.assign(value, boost::asio::error::get_misc_category());
|
|
|
|
else if (category == "asio.misc")
|
|
|
|
ec.assign(value, boost::asio::error::get_misc_category());
|
|
|
|
#if defined TORRENT_USE_OPENSSL
|
|
|
|
else if (category == "asio.ssl")
|
|
|
|
ec.assign(value, boost::asio::error::get_ssl_category());
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PyErr_SetObject(PyExc_ValueError,
|
|
|
|
("unexpected error_category passed to __setstate__; got '%s'"
|
|
|
|
% object(category)).ptr());
|
|
|
|
throw_error_already_set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-27 15:24:05 +02:00
|
|
|
struct category_holder
|
|
|
|
{
|
|
|
|
category_holder(boost::system::error_category const& cat) : m_cat(&cat) {}
|
|
|
|
char const* name() const { return m_cat->name(); }
|
|
|
|
std::string message(int const v) const { return m_cat->message(v); }
|
|
|
|
|
|
|
|
friend bool operator==(category_holder const lhs, category_holder const rhs)
|
|
|
|
{ return *lhs.m_cat == *rhs.m_cat; }
|
|
|
|
|
|
|
|
friend bool operator!=(category_holder const lhs, category_holder const rhs)
|
|
|
|
{ return *lhs.m_cat != *rhs.m_cat; }
|
|
|
|
|
|
|
|
friend bool operator<(category_holder const lhs, category_holder const rhs)
|
|
|
|
{ return *lhs.m_cat < *rhs.m_cat; }
|
|
|
|
|
|
|
|
boost::system::error_category const& ref() const { return *m_cat; }
|
|
|
|
operator boost::system::error_category const&() const { return *m_cat; }
|
|
|
|
private:
|
|
|
|
boost::system::error_category const* m_cat;
|
|
|
|
};
|
|
|
|
|
2018-11-16 16:19:12 +01:00
|
|
|
void error_code_assign(boost::system::error_code& me, int const v, category_holder const cat)
|
2011-06-25 22:11:31 +02:00
|
|
|
{
|
2018-11-16 16:19:12 +01:00
|
|
|
me.assign(v, cat.ref());
|
2018-10-27 15:24:05 +02:00
|
|
|
}
|
2016-07-04 05:30:52 +02:00
|
|
|
|
2018-11-16 16:19:12 +01:00
|
|
|
category_holder error_code_category(boost::system::error_code const& me)
|
2018-10-27 15:24:05 +02:00
|
|
|
{
|
2018-11-16 16:19:12 +01:00
|
|
|
return category_holder(me.category());
|
2018-10-27 15:24:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define WRAP_CAT(name) \
|
|
|
|
category_holder wrap_ ##name## _category() { return category_holder(name## _category()); }
|
|
|
|
|
|
|
|
WRAP_CAT(libtorrent)
|
|
|
|
WRAP_CAT(upnp)
|
|
|
|
WRAP_CAT(http)
|
|
|
|
WRAP_CAT(socks)
|
|
|
|
WRAP_CAT(bdecode)
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
WRAP_CAT(i2p)
|
|
|
|
#endif
|
|
|
|
WRAP_CAT(generic)
|
|
|
|
WRAP_CAT(system)
|
|
|
|
|
|
|
|
#undef WRAP_CAT
|
|
|
|
|
2011-06-25 22:11:31 +02:00
|
|
|
void bind_error_code()
|
|
|
|
{
|
2018-10-27 15:24:05 +02:00
|
|
|
class_<category_holder>("error_category", no_init)
|
|
|
|
.def("name", &category_holder::name)
|
|
|
|
.def("message", &category_holder::message)
|
2014-02-01 05:02:58 +01:00
|
|
|
.def(self == self)
|
|
|
|
.def(self < self)
|
|
|
|
.def(self != self)
|
|
|
|
;
|
|
|
|
|
2011-06-25 22:11:31 +02:00
|
|
|
class_<error_code>("error_code")
|
|
|
|
.def(init<>())
|
2018-10-27 15:24:05 +02:00
|
|
|
.def(init<int, category_holder>())
|
|
|
|
.def("message", static_cast<std::string (error_code::*)() const>(&error_code::message))
|
2011-06-25 22:11:31 +02:00
|
|
|
.def("value", &error_code::value)
|
|
|
|
.def("clear", &error_code::clear)
|
2018-10-27 15:24:05 +02:00
|
|
|
.def("category", &error_code_category)
|
|
|
|
.def("assign", &error_code_assign)
|
2017-02-22 01:04:36 +01:00
|
|
|
.def_pickle(ec_pickle_suite())
|
2011-06-25 22:11:31 +02:00
|
|
|
;
|
2014-02-01 05:02:58 +01:00
|
|
|
|
2018-10-27 15:24:05 +02:00
|
|
|
def("libtorrent_category", &wrap_libtorrent_category);
|
|
|
|
def("upnp_category", &wrap_upnp_category);
|
|
|
|
def("http_category", &wrap_http_category);
|
|
|
|
def("socks_category", &wrap_socks_category);
|
|
|
|
def("bdecode_category", &wrap_bdecode_category);
|
2014-02-02 05:07:36 +01:00
|
|
|
#if TORRENT_USE_I2P
|
2018-10-27 15:24:05 +02:00
|
|
|
def("i2p_category", &wrap_i2p_category);
|
2014-02-02 05:07:36 +01:00
|
|
|
#endif
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2018-10-27 15:24:05 +02:00
|
|
|
def("get_libtorrent_category", &wrap_libtorrent_category);
|
|
|
|
def("get_upnp_category", &wrap_upnp_category);
|
|
|
|
def("get_http_category", &wrap_http_category);
|
|
|
|
def("get_socks_category", &wrap_socks_category);
|
|
|
|
def("get_bdecode_category", &wrap_bdecode_category);
|
2016-10-02 21:27:50 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2018-10-27 15:24:05 +02:00
|
|
|
def("get_i2p_category", &wrap_i2p_category);
|
2016-10-02 21:27:50 +02:00
|
|
|
#endif
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2014-02-01 05:02:58 +01:00
|
|
|
|
2018-10-27 15:24:05 +02:00
|
|
|
def("generic_category", &wrap_generic_category);
|
2014-02-01 05:02:58 +01:00
|
|
|
|
2018-10-27 15:24:05 +02:00
|
|
|
def("system_category", &wrap_system_category);
|
2011-06-25 22:11:31 +02:00
|
|
|
}
|
|
|
|
|