[Cython] Scope resolution in a Visitor

Romain Guillebert romain.py at gmail.com
Tue May 24 19:22:39 CEST 2011


On Tue, May 24, 2011 at 06:56:19AM +0200, Stefan Behnel wrote:
> Romain Guillebert, 23.05.2011 20:33:
> >I'm doing the PyPy backend for Cython Summer of Code project and I would
> >like to know if there is a way of getting the AST Node responsible for
> >the declaration of a variable.
> >
> >For example :
> >
> >def func():
> >     def func():
> >         pass
> >
> >     func()
> >
> >In the situation where I'm currently traversing the function call, I
> >would like to get the function declaration node.
> >Is there already something to do that ?
> 
> What for? What kind of information do you need from it?
> 
> In Cython, the metadata about a name is stored in its symbol table
> entry. The call to "func" is a SimpleCallNode that holds a NameNode
> which has an "entry" attribute. The entry knows the name, type,
> signature, etc. of the function that is being called.
> 
> Note, however, that you need to run both the "analyse declarations"
> step and the "analyse types/expression" step to figure out all
> information about the name. The declaration analysis will add the
> definition of "func" to the symbol table, so you can look it up
> ("env.lookup('func')") from that point on.
> 
> Does that help?
> 
> Stefan

I want to generate standard python function from "cdef extern from"
function, but I need to annotate these function as they may require a
special treatment (for passing value by reference), also, I may turn C
macros into a call to a C function returning the macro itself.

cdef extern from "stdio.h":
    cdef int BUFSIZ

Will be turned into :

def get_BUFSIZ():
    # whatever

So I need to turn every access to BUFSIZ into a function call.

Cheers
Romain


More information about the cython-devel mailing list