[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Using iGraph API in Python C module

From: Tamas Nepusz
Subject: Re: [igraph] Using iGraph API in Python C module
Date: Fri, 22 May 2015 09:49:40 +0200
User-agent: Mutt/1.5.23 (2014-03-12)

Dear Chris -

> I am trying to create a C extension module to provide a python-callable
> function that can operate on a graph created using python-graph. Everything
> is working except extracting the pointer to the underlying C graph inside of
> the PyObject graph.

This hasn't been tested too much so let me know if you run into any issues, but
the idea is roughly as follows:

- There is a header named igraphmodule_api.h in the source code of the Python
  interface. This header is installed into
  ${PREFIX}/include/pythonX.Y/python-igraph/igraphmodule_api.h where ${PREFIX}
  is your Python prefix (typically /usr if you are using a system Python on
  Linux or Mac) and X.Y is your Python version number.

- Including igraphmodule_api.h in your C code would give you a function named
  import_igraph() - this must be called before using any igraph-related call
  from your C code. The function will return 0 if everything is okay.

- After having called import_igraph, you can start calling PyIGraph_ToCGraph()
  from your C code.

I have followed the instructions on the page below to implement this in igraph:


Alternatively, you can use PyObject_CallMethod(py_graph, "_raw_pointer", "()")
to call the _raw_pointer method from C code -- this would return the raw memory
address as a Python integer (PyObject). You can then extract the memory address
using PyInt_AsLong and cast it into an (igraph_t*). (Don't forget to Py_DECREF
the Python integer if you don't need it any more).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]