Globals, Locals Dictionaries + Callbacks Question

Discussion in 'Python' started by Faisal Alquaddoomi, Dec 21, 2005.

  1. 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
     
    Faisal Alquaddoomi, Dec 21, 2005
    #1
    1. Advertising

  2. Faisal Alquaddoomi

    Steve Holden Guest

    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
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Dec 21, 2005
    #2
    1. Advertising

  3. Steve Holden wrote:
    > 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.

    Thanks for the advice,
    Faisal
     
    Faisal Alquaddoomi, Dec 21, 2005
    #3
  4. Faisal Alquaddoomi

    Steve Holden Guest

    Faisal Alquaddoomi wrote:
    > Steve Holden wrote:
    >
    >>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
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Dec 22, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giles Brown

    Question about "exec in globals, locals"

    Giles Brown, Jul 4, 2003, in forum: Python
    Replies:
    2
    Views:
    383
    Adrien Di Mascio
    Jul 4, 2003
  2. tedsuzman
    Replies:
    2
    Views:
    7,167
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    497
    Duncan Booth
    Jul 22, 2004
  4. Olivier Sessink
    Replies:
    0
    Views:
    419
    Olivier Sessink
    Feb 24, 2005
  5. Paolo Pantaleo

    locals() and globals()

    Paolo Pantaleo, Oct 14, 2006, in forum: Python
    Replies:
    1
    Views:
    596
    Kay Schluehr
    Oct 14, 2006
Loading...

Share This Page