PyEval_GetLocals and unreferenced variables

Kasper Peeters kasper at phi-sci.com
Wed Nov 26 11:46:28 CET 2014


> >   def fun():
> >      q=3
> >      def fun2():
> >         cfun()
> >      fun2()
> >
> >   fun()
> >
> > and access 'q' inside the C-function cfun(). If I simply let it call
> > PyEval_GetLocals, then the result will again not contain "q". Is
> > there any way in which I can convince python to pull 'q' into the
> > local scope from within my C code?
> 
> Wouldn't this be a little surprising? Why not simply pass q as a
> parameter?

I agree that in this example that would be the natural thing to do. 
My case is more tricky though: I have something like

    def fun():
       cfun_that_creates_q_in_local_scope()
       def fun2():
           cfun_that_wants_to_see_if_q_is_available()

So the Python side actually doesn't see 'q' directly at all.

I am willing to elaborate on this if you want (I have fairly good
reasons to do things this way, mostly having to do with historical
constraints of an older C library that I cannot avoid), but it
requires more space.

Cheers,
Kasper



More information about the Python-list mailing list