embedding help

Alex Martelli aleaxit at yahoo.com
Tue Dec 19 14:22:04 CET 2000


"Jp Calderone" <jcaldero at ic.sunysb.edu> wrote in message
news:3A3F0EFC.D2F2BAE9 at ic.sunysb.edu...
>
>
>   I'm trying to embed python in a much larger project (a MUD)
> as a scripting language, but I'm stuck, essentially right at
> the begining.  The docs at python.org on this section seem,
> ahh, incomplete (unless I'm missing something).  What I'm

They _are_ a bit incomplete -- some of the Python C API
docs just list relevant functions &c without any info
about them.

> trying to do ATM is something along the lines of let python
> code access and modify the values of C++ objects outside of
> the interpreter.  I believe the correct way to go about this
> is to create a new python object type that understands how
> to find the C++ objects in the C++ portion of the program,
> providing it with get and set attr methods.  However, I'm

Yes, that makes sense.  If you're _embedding_ Python,
rather than just _extending_ it, you'll still create a
Python module for the purpose, but 'pump it into Python'
just before or just after you start it, rather than
have it loaded when needed via import statements.

> at a loss as to how to actually do this.  I found the
> PyTypeObject struct, and I see it has a lot of fields for
> functions that do things, but no actual data storage, so
> I don't see any way to keep track of which python object
> instances points to which C++ object instances.  Can anyone
> point me to any examples of how to do this, or explain it?
> Or am I barking up the wrong tree entirely?

Since you're C++-bound anyway, I think that much the
easiest way for you to work is via the Boost Python
library, www.boost.org; once named py_cpp -- like all
of Boost, it does require a C++ compiler with halfway
decent templates (maybe CXX or other such projects
may still help you even with older C++ compilers...),
but gcc and Visual C++ 6, for example, work with it.

For C-level work, the ideal read would be "How to
write a Python extension",
http://starship.python.net:10080/crew/arcege/extwriting/pyext.html
except that accessing anything on Starship is always
a bother these days (I wonder if mirrors of this may
be found anywhere else...?).  Worst case, Google IS
fortunately caching it (bless it!)...: enter the
following very long URL on ONE single line...:
http://www.google.com/search?q=cache:starship.python.net:10080/crew/arcege/e
xtwriting/pyext.html

If you do use Google's cache, remember you have to force
its use manually for links in the page that also refer to
the Starship (assuming the Starship is still down, that is).


Anyway, to address your specific question, the C-level
structure may be something like...:

typedef struct oneVariable {
    const char* name;
    int* pValue;
} oneVariable;

typedef struct {
  PyObject_HEAD
  oneVariable variables[NUM_VARIABLES];
} myObject;

that is, your object are defined by _following_ the
'Python head' with your own data -- a sort of C level
emulation of C++'s simplest kind of inheritance (which
is part of why the C++ encapsulations of Python
interfacing are just SO much handier than the C level
ones... but, enough C++ haters lurk around here that
I'm sure to get flamed for this assertion!-).


Alex






More information about the Python-list mailing list