6

I'm extending my library with Python (2.7) by wrapping interfaces with SWIG 2.0, and have a graph object in which I want to create a visitor. In C++, the interface looks like this:

    struct Visitor
    {
        virtual void OnStateBegin() = 0;
        virtual void OnNode(Node* n) = 0;
        virtual void OnStateEnd() = 0;
    };

I would like to define a class in Python that does the equivalent, all defined in python, that will allow for the definition of a visitor:

class GraphVisitor:
    def __init__(self, label):
        self._label = label
        print("__init__(self,{0})".format(self._label))
    def OnStateBegin(self):
        print("OnStateBegin()" + self._label)
    def OnNode(self, i_node):
        print("OnNode()" + self._label)
    def OnStateEnd(self):
        print("OnStateEnd()" + self._label)

And what I'm trying to do is create an instance of a GraphVisitor in python script, and call the methods OnStateBegin(), OnNode(), and OnStateEnd() for a given instance from C++. Here's what I'd like to do in Python:

#model is a SWIG wrapped class
mvis = GraphVisitor("This is a test")
model.Visit("mvis") # I'm not sure how to pass the instance 'mvis' to C++?

And in my C++ wrapped by Swig, I'm not sure how to get at the instance 'mvis'? I can call functions defined in Python no problem, but instances has me stumped!

4
  • 1
    mvis is just an instance of a python class. It has noting to do with your struct Visitor. In C/C++ you can only access it as a PyObject*.
    – Thomas
    Apr 28, 2015 at 20:28
  • I know that. I was trying to describe that I'm trying to define a visitor in python and thats the c++ counterpart. Apr 29, 2015 at 0:27
  • 1
    Have you seen my previous answer: stackoverflow.com/questions/9040669/… (you can skip the bits about embedding, but the bit about converting to a C++ interface from a PyObject does exactly what you want)
    – Flexo
    Apr 29, 2015 at 16:51
  • Flexo - thanks a ton! Your stuff really helped! May 2, 2015 at 13:24

2 Answers 2

1

In order to solve this problem, I retrieved the class from the module given it's module name and class name (the code below assumes the module hasn't already been loaded):

void Model::Visit(const char* mod_name, const char* class_name)
{
    PyErr_Clear();
    PyObject* mod_name_obj = PyString_FromString(mod_name);
    PyObject* class_name_obj = PyString_FromString(class_name);

    PyObject* py_module = PyImport_Import(mod_name_obj);
    PyObject* err_1 = PyErr_Occurred();
    if(err_1)
        PyErr_Print();

Once I had the module, I looked up the class from it's dictionary:

    if(py_module)
    {
        PyObject* py_module_dict = PyModule_GetDict(py_module);
        PyObject* py_class = PyDict_GetItem(py_module_dict, class_name_obj);

I simplified my problem a bit by instantiating the python class in C++, then created my visitor, and finally visited it:

        if(py_class && PyClass_Check(py_class) && PyCallable_Check(py_class))
        {
            PyObject* inst = PyInstance_New(py_class, 0, 0);

            if(inst && PyInstance_Check(inst))
            {
                IModel::IVisitorPtr py_visitor = new PyModelVisitor(inst);

                _model->Visit(py_visitor);
            }
        }
    }
}

The visitor had 3 functions OnStateBegin(), OnNode(), and OnStateEnd(). I added to my SWIG python binding generator an option to generate a header file for external access to the SWIG runtime with the -external-runtime option, so I could create a class in C++ (INode* below) and pass it to Python as the argument to the python OnNode() member function as follows (error checking removed for brevity):

VisitorCtrl OnNode(INode* node)
{
    Node* node_impl = new NodeImpl(node);
    PyObject* pynode = SWIG_NewPointerObj(node_impl, SWIG_TypeQuery("Node *"), 0);
    PyObject* result = PyObject_CallMethodObjArgs(_inst, PyString_FromString("OnNode"), pynode, 0);

    long rivis = PyInt_AsLong(result);

    return(static_cast<VisitorCtrl>(rivis));
}
0

I don't know if that's possible with SWIG, but you can do it with SIP.

sip_vector_test.h:

class EXPORT Node {
public:
    explicit Node(int n) : n_(n) {};
    int getN() const { return n_; }
private:
    int n_;
};
struct EXPORT NodeVisitor {
    virtual void OnNode(Node* n) = 0;
};
struct EXPORT Graph {
public:
    void addNode(int num);
    void accept(NodeVisitor *nv);
private:
    std::vector< std::shared_ptr<Node> > nodes_;
};

visitor.sip:

%Module pyvisit

%ModuleHeaderCode
#include "sip_visitor_test.h"
%End

class Node {
public:
    explicit Node(int n);
    int getN() const;
};
struct NodeVisitor {
    virtual void OnNode(Node* n) = 0;
};
struct Graph {
public:
    void addNode(int num);
    void accept(NodeVisitor *nv);
};

Using it from Python:

>>> import pyvisit
>>> g = pyvisit.Graph()
>>> g.addNode(3)
>>> g.addNode(5)
>>> class PyNodeVisitor(pyvisit.NodeVisitor):
>>>     def OnNode(self, node):
>>>         print(node.getN())
>>> pnv = PyNodeVisitor()
>>> g.accept(pnv)
3
5

I've put a zip file containing the source code of this test project on my homepage.

1
  • Thanks a lot for the answer! I'm not using SIP, and wanted to see if this could be done without adding a new type. Apr 29, 2015 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.