[Cython] Is it easy to do an "AST grep" of Cython code?

Jelle Zijlstra jelle.zijlstra at gmail.com
Mon Sep 14 18:36:55 CEST 2015


This seems doable in principle by using the
Cython.Compiler.Visitor.TreeVisitor class. You'll have to figure out
what Cython AST nodes the things you're looking for correspond to. As
a simple example, this will find all non-def functions in a Cython
file:

from Cython.Compiler import TreeFragment, Visitor

cython_code = unicode(open("filename.pyx").read())

class CdefFunFinder(Visitor.TreeVisitor):
    def visit_Node(self, node):
        self.visitchildren(node)

    def visit_CFuncDefNode(self, node):
        self.visitchildren(node)
        print self.dump_node(node)

CdefFunFinder().visit(TreeFragment.parse_from_strings('<test>',
unicode(cython_code)))

2015-09-14 5:33 GMT-07:00 Nathaniel Smith <njs at vorpus.org>:
> I have a few hundred files worth of Cython code, and I'd like to find
> all instances where a variable/expression has a certain cdef class
> type. (More specifically, I'd like to find all accesses to the cdef
> fields of a particular cdef class, but even just finding all 'cdef
> MYTYPE x" and "<MYTYPE>x" statements would probably get me pretty
> close.) Is there any easy way to do this?
>
> (The files are "every cython file on github or searchcode.com that
> mentions the word "ufunc"", and I'm trying to find code that does
> direct field access to the internals of the PyUFuncObject struct.)
>
> --
> Nathaniel J. Smith -- http://vorpus.org
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> https://mail.python.org/mailman/listinfo/cython-devel


More information about the cython-devel mailing list