예제 #1
0
	static std::list<TYPE> extractFromList( api::object value ) {
		std::list<TYPE> retList;

		for( Py_ssize_t i = 0; i < PyList_Size( value.ptr() ); i++ ) {
			retList.push_back( convert( api::object( handle<>( borrowed( PyList_GetItem( value.ptr(), i )  ) ) ) ).as<TYPE>() );
		}

		return retList;
	}
예제 #2
0
inline extract<T>::extract(api::object const& o)
    : base(o.ptr())
{
}
예제 #3
0
파일: Value.cpp 프로젝트: jktjkt/deska
/** @short Convert a python object into the Deska::Db::Value */
Value Py_2_DeskaDbValue(const api::object &o)
{
    // None
    if (o == api::object())
        return Value();

    // string
    extract<std::string> get_str(o);
    if (get_str.check())
        return NonOptionalValue(get_str());

    // set<string>
    extract<std::set<std::string> > get_str_set(o);
    if (get_str_set.check())
        return NonOptionalValue(get_str_set());

    // bool
    // This check has to be performed using low-level Python functions to prevent boost::python from treating ints as bools and vice versa.
    // An alternative is a project-wide define, which is not exactly an etalon of elegance.
    // See http://boost.2283326.n4.nabble.com/Avoiding-implicit-boost-python-extract-lt-gt-conversions-td3433520.html for details
    if (PyBool_Check(o.ptr())) {
        extract<bool> get_bool(o);
        BOOST_ASSERT(get_bool.check());
        return NonOptionalValue(get_bool());
    }

    // int
    extract<int> get_int(o);
    if (get_int.check())
        return NonOptionalValue(get_int());

    // double
    extract<double> get_double(o);
    if (get_double.check())
        return NonOptionalValue(get_double());

    // IPv4 address
    extract<boost::asio::ip::address_v4> get_ipv4(o);
    if (get_ipv4.check())
        return NonOptionalValue(get_ipv4());

    // IPv6 address
    extract<boost::asio::ip::address_v6> get_ipv6(o);
    if (get_ipv6.check())
        return NonOptionalValue(get_ipv6());

    // MAC address
    extract<MacAddress> get_mac(o);
    if (get_mac.check())
        return NonOptionalValue(get_mac());

    // boost::posix_time::ptime
    extract<boost::posix_time::ptime> get_ptime(o);
    if (get_ptime.check())
        return NonOptionalValue(get_ptime());

    // boost::gregorian::date
    extract<boost::gregorian::date> get_date(o);
    if (get_date.check())
        return NonOptionalValue(get_date());

    throw std::runtime_error("Unsupported type of a python object");
}