[C++-sig] wrapping global variables

Dominique Devriese dominique.devriese at student.kuleuven.ac.be
Sun Oct 20 22:34:14 CEST 2002


Hi,
( please cc me as i'm not subscribed to the list)
I'm playing around a bit with embedding python in a C++ program.  I'm
using Boost.python for exposing the program's API to python, and the
normal python interface ( Py_Initialize, PyRun_SimpleString and
Py_Finalize ) for embedding.

So far this has proven surprisingly easy.  Especially Boost.Python has
amazed me with its ease of use, so congratulations to everyone here
who helped write it, as it most definitely rocks :)

But I have a question:  I would like to expose some global variables
to python.  E.g. if I have a variable curbuf, I would like to be able
to write ( in the embedded python interpreter, that is ) something
like "len = curbuf.length();".  curbuf is of class Buffer, and this
class has been exported very easily, but I don't seem to be able to
find how to expose a global variable.  Of course I can think of some
workarounds like exposing a free function, exposing a curbuf class
that has a singleton constructor, combining either of the above with
something like "PyRun_SimpleString( \"global a = freeFunOrClass();\"
);" ( If that is wrong python, that's because I'm more of a C++
programmer than a python programmer ;) etc., but I was wondering if
there wasn't a clean way to go about it.

I've browsed the c++-sig at python.org archives a bit ( but as there
doesn't seem to be a search function, I've only read the last five
months or so ), and all i've found was this thread:
http://mail.python.org/pipermail/c++-sig/2002-July/001768.html
but it's not very clear...

So, is there a way to do this, or are there any plans to implement it
any time soon or are there any easy alternatives I'm missing ?
Thanks in advance
domi

-- 
Dominique Devriese




More information about the Cplusplus-sig mailing list