Question

codes of python(which work)

import ldap,ldif

l = ldap.initialize('ldaps://RIO-PC:636')
l.set_option(ldap.OPT_TIMEOUT);

I want to extract the constant value of ldap.OPT_TIMEOUT, but how to?

#include <iostream>

#include <boost/python.hpp>

int main()
{
    namespace bp = boost::python;
    try{
        Py_Initialize();
        bp::object main_module = bp::import("__main__");
        bp::import("ldap");
        bp::import("ldif");
        bp::object main_namespace = main_module.attr("__dict__");

        bp::exec("import ldap,ldif\n"
                 "l = ldap.initialize('ldaps://RIO-PC')\n",
             main_namespace);

        boost::python::object ldap = boost::python::extract<boost::python::object>(main_namespace["l"]);
        ldap.attr("OPT_TIMEOUT"); //this line will throw exception
    }catch(boost::python::error_already_set const &){               
        PyErr_Print();
        PyErr_Clear();
    }
}

the error message is

AttributeError: SimpleLDAPObject has no attribute 'OPT_TIMEOUT'

environment

compiler : vc2008 boost version : 1.55_0 os : win7 64bits(x64)

Was it helpful?

Solution

Your C++ code is trying to extract l.OPT_TIMEOUT, not ldap.OPT_TIMEOUT. Try

    bp::object ldap_module = bp::import("ldap");
    ldap_module.attr("OPT_TIMEOUT");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top