PyEval_GetLocals and unreferenced variables

Kasper Peeters kasper at phi-sci.com
Wed Nov 26 06:40:12 EST 2014


> To be honest, that's just made it even more weird :) You're creating
> something in a local namespace that the Python compiler isn't aware
> of.

Yes, I agree that retrieving the locals with PyEval_GetLocals and then
sticking something in there on the C side is weird. I wouldn't say that
the Python compiler is not aware of it though (I could ask my question
purely in Python: how do I check in the scope of fun2 whether 'q' is
available in the outer scope without actually using it).

> I wonder, would a 'nonlocal q' declaration inside fun2 affect things
> any?

Yes, that pulls in 'q', but I would like to do that 'nonlocal q' on the
C side. If I have to write 'nonlocal q' inside fun2 then I can also
pull in 'q' using any other reference to 'q'.

> Otherwise, maybe there's some completely different way to transfer
> information around. Using locals in this way seems fraught with peril.

There might very well be...

Cheers,
Kasper
 




More information about the Python-list mailing list