Globals, Locals Dictionaries + Callbacks Question

F

Faisal Alquaddoomi

Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled. I've had some luck with PyRun_FileEx(), as you can
specify dictionaries to use for the globals and locals, but it seems
that you can't do the same for PyEval_CallObject() (which i use for
calling the callbacks previously registered by scripts run via
PyRun_FileEx()). Is there any way to substitute the current global/local
dictionaries, run the callback, then switch back to the default? It
would be just as good if I could switch between several sets of global
variable dictionaries, one for each script; unfortunately, the
documentation is less than informative on this point (there isn't even a
formal defintion for PyEval_CallObject()). Also, I'm aware that eval()
and exec() allow you to pass in global/local dictionaries, but I think
it'd be a bit wasteful to drop into Python, run exec(), then have that
call my callback, rather than just calling the callback directly from
the host program.

Thanks in advance
 
S

Steve Holden

Faisal said:
Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled.

[tales of woe ...]

Would it help to use PyNewInterpreter to create subinterpreters for each
of the different scripts?

regards
Steve
 
F

Faisal Alquaddoomi

Steve said:
Faisal said:
Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled.


[tales of woe ...]

Would it help to use PyNewInterpreter to create subinterpreters for each
of the different scripts?

regards
Steve

That sounds like a reasonable suggestion, but would it be inefficient to
spawn a whole new interpreter for each script, where what I really want
to do is just change the dictionaries used for globals and locals? I'll
try that out for now, but I'm still hoping there's some way to change
those dictionaries without having to do anything excessive.

Thanks for the advice,
Faisal
 
S

Steve Holden

Faisal said:
Steve said:
Faisal Alquaddoomi wrote:

Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled.


[tales of woe ...]

Would it help to use PyNewInterpreter to create subinterpreters for each
of the different scripts?

regards
Steve


That sounds like a reasonable suggestion, but would it be inefficient to
spawn a whole new interpreter for each script, where what I really want
to do is just change the dictionaries used for globals and locals? I'll
try that out for now, but I'm still hoping there's some way to change
those dictionaries without having to do anything excessive.
I guess only benchmarking can answer that question. However, given that
the code will be common to all instances you may find that it isn't too
resource-intensive. I agree it does seem much more heavyweight than
replacing execution context.

Good luck!

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top