From: jefflholle (jeff.holle_at_[hidden])
Date: 2002-01-05 01:19:06


I'm failing to understand examples of this in boost_1_24_0
Boost.Python.

Basically, my problem is in obtaining the PyObject * value needed to
make a callback.

The simplest example of this is abstract.cpp/test_abstract.py.
It is more complicated that what I'm after since it involves an
abstract C++ class that is interfaced to python.

I've modified this module to reflect what I'm really after and gotten
it to behave in the manner that my code is. It follows:
// Example by Ullrich Koethe
#pragma warning( disable: 4541)
#include "boost/python/class_builder.hpp"
#include <string>
#include <iostream>
struct Abstract
{
    virtual std::string test() = 0;
};

struct Abstract_callback
{
    Abstract_callback(PyObject *val)
    : m_self(val)
    {}

    std::string test()
    {
          std::cout << "Got here" << std::endl;
      return std::string
("ff");//boost::python::callback<std::string>::call_method
(m_self, "Beast");
    }

    PyObject * m_self;
};

BOOST_PYTHON_MODULE_INIT(abstract)
{
    boost::python::module_builder a("abstract");

    boost::python::class_builder<Abstract_callback> a_class
(a, "Abstract");
    a_class.def(boost::python::constructor<>()); // wrap a constructor
    a_class.def(&Abstract_callback::test, "test");
}

This module doesn't compile for want of a default constructor in
Abstract_callback. It doesn't exist because I want to make the
constructor of this class provide the needed PyObject, so a callback
can be done.

If I modify the "boost::python::constructor<>());"
to "boost::python::constructor<Python *>());" I run into conversion
problems. Actually, I've found that I run into such trouble passing
any pointer and a lot of complex things.

How do I do this?

One more thing. I've tried just adding another method to deliver the
self pointer of the class and get a runtime error saying that a
string or unicode object expected but an instance was found when
calling it from python.