I don't get it (Extending C/C++)

Michael Hudson mwh21 at cam.ac.uk
Fri Mar 9 18:48:43 CET 2001


"Moritz Voss" <thygrrr at gmx.net> writes:

> I'm somewhat stumped by all the weird function names in the Tutorial
> of the 2.0 Manual...

Do you really mean the tutorial, or the extending & embeddng manual?

> Is something like this possible:
> I call a Python function from my C/C++ program. I pass it two
> pointers, one to an array with data, and one to an empty space where
> it can put its results....

Yes, certainly.  Well, actually, not *quite* as you describe.  You
have to massage the data into a form Python can cope with, e.g. a list
of floats, using the Python C API calls and get a pointer to the
Python function, build the arguments you want to pass into the
function into a tuple, and call PyEval_CallObject.  You then get a
PyObject* back as a result, or NULL if an exception was thrown.

> I don't even remotely see a way how to do this in the Tutorial, it's
> just full of (to me) irrelevant stuff about callbacks etc...I feel
> stupid here, folks ;-)

This is one of those things that is really quite easy, once you know
it.  I agree learning how to do this stuff can be a bit intimidating.
If you have the time I recommend d/l-ing the python source and
spending an hour or two learning a sort of rough outline of how
CPython works (and how to write clean, readable C!).  After all,
CPython has to do these things that you're trying to do, e.g. calling
functions, handling errors.

I've been thinking about writing a Python-Internals-HOWTO describing
this sort of thing, but it's *hard*; everything depends on everything
else, which makes for poor pedagogy.

> Also, is it advisable to invoke the interpreter (do I even do that?
> what DO I do when I execute a python function?) several hundred
> times? (running about 500 instances of several, sometimes identical
> python scripts times would be likely for medium load of my
> application).

You call PyInitialize and PyFinalize (check spellings!) once each.
Then you can call the interprater through the PyRun_* or PyEval_* APIs
as much as you like (the interpreter itself uses the Py_Eval_* API).

> I'm sorry, I just don't /understand/ the Python concept...yet.

Hang in there.  We're here to help!

> It looks all so cool, but I don't have a clue about how to use it.
> (I plan to use it as a scripting language to customize application
> component behaviour)

Cool.

Do you have any more specific problems?  "Argh, it doesn't make any
sense!" is not the easiest problem to answer!

Cheers,
M.

-- 
  I hate leaving Windows95 boxes publically accessible, so shifting
  even to NT is a blessing in some ways.  At least I can reboot them
  remotely in a sane manner, rather than having to send them malformed
  packets.      -- http://bofhcam.org/journal/journal.html, 20/06/2000



More information about the Python-list mailing list