Embedding and threads ?

Martin v. Löwis loewis at informatik.hu-berlin.de
Thu May 30 13:27:18 EDT 2002


Bo Lorentsen <bl at netgroup.dk> writes:

> Is it a dumb question to ask, if I asked why the two dict's are there as
> parameters in the first place ? Do they overwrite the global ones ?

Python has three namespaces: locals, globals, and builtins. The
parameters allow you to specify the locals and globals, since that
allows you to execute your expression in a reasonably "fresh"
environment, and since Python could not obtain these dictionaries
otherwise (notice that there is a "global" namespace per module).

> I can see that normally Python has a single ThreadState (and its related
> Interpreter) that works as a singleton

That is not the case. There is one thread state per thread. All but
one thread is blocked at any point in time (unless the other threads
execute unrelated C code); those threads have their thread state
preserved in a local variable at the point where they last executed
BEGIN_ALLOW_THREADS.

> but is it not possible to "swap" these after getting the GIL, or am
> I totally off track here ?

It certainly is possible.

> Yes !!! This is what I need ! But this is designed to be used inside
> Python, but how about using something like this while embedding Python ?

There is no difference between "inside Python", and "from C". Write
down what you would do in Python, then translate that literally into C
(using PyObject_CallFunction etc). You may find it useful to make a
Python wrapper around rexec which customizes it to your need, and call
that wrapper from the C code.

> But, how about making Python point to an invalid "site" path, or
> something like it ? 

That won't help - one can modify sys.path to overcome that limitation.
The best way to not allow execution of dangerous code is not to
include such code in your binary in the first place.

> I think the rexec module sounds like the propper way to go. I also like
> to keep using a standart version of Python, as special versions of a
> major product often mess things up as the main product evolves.

Not having to edit the Python source is a desirable thing to
do. Having a custom build of Python is IMO reasonable in about any
embedding application.

> Again, thanks for your answers, I am already making my first embeded
> code tests, and it is wery easy to manage, and somewhat nice to look at

Good luck!

Martin





More information about the Python-list mailing list