deepak
deepak

Reputation: 2075

Python ctypes: wraping c++ class with operators

I want to wrap a small test C++ class for use in python using ctypes. The class is called Edge and has a friend comparison (==) operator. I am having difficulty implementing the comparison feature in the wrapper python code.

A succinct Edge header is:

class Edge {
    private:
        int p1, p2;
    public:
        Edge(const int pp1, const int pp2);
        ~Edge(){};
        friend bool operator==(const Edge &e1, const Edge &e2);
};

The python wrapper I have written is:

from ctypes import *
lib = cdll.LoadLibrary('./libedge.so')

lib.Edge_new.argtypes = [c_int, c_int]
lib.Edge_new.restype = c_void_p

lib.compare_edge.argtypes = [c_void_p, c_void_p]
lib.compare_edge.restype  = c_bool


class Edge(object):
    def __init__(self, pp1, pp2):
        self.obj = lib.Edge_new(c_int(pp1), c_int(pp2))

    def __eq__(self, other):
        return lib.compare_edge(self.obj, c_void_p(other))

Where, Edge_new and compare_edge are C++ routines that are defined as:

#include "Edge.hpp"

extern "C" {
    Edge* Edge_new(const Int32 pp1, const Int32 pp2) { return new Edge(pp1, pp2); }

    bool compare_edge(Edge *e1, Edge *e2) {
        return *e1 == *e2;
    }
}

The constructor works fine. When I compare two edge objects e1 == e2 I get the following typeerror:

Traceback (most recent call last):   File "Edge.py", line 21, in <module>
    print e1 == e2   File "Edge.py", line 16, in __eq__
    return lib.compare_edge(self.obj, c_void_p(other)) TypeError: cannot be converted to pointer

I do understand what the error means, and most likely why something is going wrong, but I don't know how to fix it. I am compiling C++ code with gcc 4.7 and the python interpreter is 64 bit.

Upvotes: 2

Views: 1832

Answers (1)

Aya
Aya

Reputation: 42050

Problem is, you're trying to cast a Python object to a void *, rather than the void * you already attached to that object's obj attribute.

It should be as simple as changing...

def __eq__(self, other):
    return lib.compare_edge(self.obj, c_void_p(other))

...to...

def __eq__(self, other):
    return lib.compare_edge(self.obj, other.obj)

The explicit call to c_void_p should be unnecessary, since you already declared the types in the line...

lib.compare_edge.argtypes = [c_void_p, c_void_p]

Upvotes: 3

Related Questions