[Cython] [ENH] Compile-time code execution (macros / templates)
Celelibi
celelibi at gmail.com
Thu Feb 25 21:21:27 EST 2021
Le Fri, Feb 26, 2021 at 01:32:34PM +1300, Greg Ewing a écrit :
> On 26/02/21 9:29 am, Celelibi wrote:
> > Maybe in the future if cython
> > support compiling closures to C?
>
> Your "twice" example already needs some closure functionality.
Yes, at the python level only. That's precisely the point.
> It relies on being able to manufacture a cdef function inside
> a Python function, with the expectation that it will have
> access to arguments of the Python function. With all or some
> of that happening at compile time rather than run time. I'm
> having trouble imagining how it would be implemented.
It relies on creating a python object representing the inner cdef
function during the compile-time execution. Which is, like, the trivial
part.
Just picture the `twice` example being converted like this for the
compile-time execution.
def twice(f):
fun = CdefFunction(... use f ...)
return fun
foo = CdefFunction(...)
ast.append(AstCdefFunction("foo", twice(foo)))
Where CdefFunction is not necessarily callable. It's just a class
representing a cdef function. And AstCdefFunction is an AST node
representing a cdef function definition.
Does that seem far fetched to you?
I think this might even be doable without having to even detect the
closure in cython. We'd just have to let python perform the name lookup.
Celelibi
More information about the cython-devel
mailing list