Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81558 > unrolled thread

Re: PyEval_GetLocals and unreferenced variables

Started byKasper Peeters <kasper@phi-sci.com>
First post2014-11-26 10:46 +0000
Last post2014-11-30 00:34 +1300
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: PyEval_GetLocals and unreferenced variables Kasper Peeters <kasper@phi-sci.com> - 2014-11-26 10:46 +0000
    Re: PyEval_GetLocals and unreferenced variables Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-30 00:34 +1300

#81558 — Re: PyEval_GetLocals and unreferenced variables

FromKasper Peeters <kasper@phi-sci.com>
Date2014-11-26 10:46 +0000
SubjectRe: PyEval_GetLocals and unreferenced variables
Message-ID<mailman.16317.1416998820.18130.python-list@python.org>
> >   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

[toc] | [next] | [standalone]


#81718

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-11-30 00:34 +1300
Message-ID<cdtp9bFsmvpU1@mid.individual.net>
In reply to#81558
Kasper Peeters wrote:

>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 think you will need to elaborate. There are several things
about the way local scopes and references to outer scopes are
implemented in Python that makes what you are asking for
extremely difficult.

If you explain the problem you're actually trying to solve,
we'll probably be able to suggest a better solution.

-- 
Greg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web