Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it too hard to embed a Python interpreter into a Win32 program?

8 views
Skip to first unread message

Paul Pedriana

unread,
Apr 30, 1997, 3:00:00 AM4/30/97
to

I want to embed a Python interpreter into a Win32 program.

There is only one unusual thing about this Win32 app.
It cannot use the standard Windows GUI (e.g. HWNDS, window
controls, etc.) and it cannot use a console input/output.

The app would simply pass scripts to the Python interpreter
and the interpreter would run them.

Is this very hard? I read all the documentation and the FAQ,
but they never gets very far into the embedding topic.

Paul Pedriana
pped...@maxis.com

Fredrik Lundh

unread,
Apr 30, 1997, 3:00:00 AM4/30/97
to

> I want to embed a Python interpreter into a Win32 program.

> Is this very hard? I read all the documentation and the FAQ,

> but they never gets very far into the embedding topic.

Check http://www.python.org/doc/ext/node19.html and Demo/embed/demo.c;
you don't really need to know more to just get the interpreter running
under your application... basically, you have to do something like:

#include "Python.h"

Py_Initialize();
PySys_SetArgv(argc, argv); // optional

...

PyRun_SimpleString(python code);
or PyRun_SimpleFile(fp, filename);

...

Py_Exit(); // optional

and link your application with the python14.dll

Cheers /F (http://hem1.passagen.se/eff)

aaron watters

unread,
Apr 30, 1997, 3:00:00 AM4/30/97
to

Subject: Is it too hard to embed a Python interpreter into a Win32
program?

>I want to embed a Python interpreter into a Win32 program.
...
>Is this very hard?

No, it's easier than writing a C/C++ app from scratch for sure.
I suspect when you get going you'll want more interaction between
C and Python than you imagine right now, and there are some
tricks for win32. Look to
http://www.python.org/ftp/python/contrib/Web/
for grisha's nsapy_nt port of my nsapi embedding for a simple
but non-trivial example. Thanks again grisha!

Aaron Watters
=-=
Have you a brain?
I don't know, I've never looked.
From The Wizard of Oz (book)

Dirk Engelmann

unread,
May 1, 1997, 3:00:00 AM5/1/97
to

Fredrik Lundh wrote:
>
> > I want to embed a Python interpreter into a Win32 program.
>
> > Is this very hard? I read all the documentation and the FAQ,
> > but they never gets very far into the embedding topic.
>
> Check http://www.python.org/doc/ext/node19.html and Demo/embed/demo.c;
> you don't really need to know more to just get the interpreter running
> under your application... basically, you have to do something like:
>
> #include "Python.h"
>
> Py_Initialize();
> PySys_SetArgv(argc, argv); // optional
>
> ...
>
> PyRun_SimpleString(python code);
> or PyRun_SimpleFile(fp, filename);
>
> ...
>
> Py_Exit(); // optional
>

I'm wondering, if there is an (easy) way to access or set C-variables
from python-code called in this way ?


Cheers
Dirk Engelmann

Mark Lutz

unread,
May 3, 1997, 3:00:00 AM5/3/97
to

Dirk Engelmann <dirk.en...@iwr.uni-heidelberg.de> writes:

> > > I want to embed a Python interpreter into a Win32 program.
[...]

> > #include "Python.h"
> >
> > Py_Initialize();
> > PySys_SetArgv(argc, argv); // optional
> > ...
> > PyRun_SimpleString(python code);
> > or PyRun_SimpleFile(fp, filename);
> > ...
> > Py_Exit(); // optional
>
> I'm wondering, if there is an (easy) way to access or set C-variables
> from python-code called in this way ?

Depends on how you define easy :-). Two general approaches:

(1) C extension module...
Create a C extension module with get/set functions for each C
variable you want to expose, import this module in the embedded
Python code, and use its functions to access the C variables.

(2) Copy-in-copy-out...
Store the C variables as Python module-level variables before
the embedded code runs, and fetch their values out of the Python
module after the embedded code exits. The Python code can use
and change the module-level variables just like normal Python
global variables; the enclosing C layer is responsible for
initializing them and fetching their final values.

Plus other schemes I can't remember at the moment :-). For example,
passing C variable values to Python function arguments is similar
to method (2), but it's more difficult to copy final values out.
You can also work arbitrary magic with __getattr__ and __setattr__
(an exercise for the reader...).

Method (1) above is straightforward (a simple C extension module
will do, and could even be generated automatically). In method (2),
you'll need to call a few module attribute access API functions;
something along these lines:

pX = PyString_FromString(X); /* convert C=>Python */
pmod = PyImport_ImportModule("mymod"); /* import module in C */
PyObject_SetAttrString(pmod, "X", pX); /* assign to mymod.X */

[...run code-string or object in mymod.py...]
[...in the code, "X" is a copy of C's "X"...]

pX = PyObject_GetAttrString(pmod, "X"); /* get final mymod.X */
PyArg_Parse(pX, "s", &X); /* convert to C's X */

Chapter 15 in PP presents an API which automates some of this process
(if you can access a copy, see the use of the Set_Global/Get_Global
functions in the embedded order-validations example). And if you're
ambitious, some of the code above could be automatically generated too.

Probably the biggest drawback of method (2) is that the link to C
variables is really just a copy-in-copy-out: if the embedded code
calls out to C again before it exits, the C variables won't yet
reflect the values in their Python counterparts (probably not an
issue, unless they're globals in C too). The upside is that you
don't have to write or use a full-blown C extension module.

Hope it helps,
Mark L.

0 new messages