Ticket #759 (new enhancement)

Opened 17 months ago

Last modified 5 months ago

Runtime Compilation of a module [patch]

Reported by: bfredl Owned by: somebody
Priority: minor Milestone: wishlist
Component: Build System Keywords: inline, module
Cc: bfredl, scoder

Description

It would be useful if there was a function that took a string of cython code, compiled it as a .pyx file, and then returned a live cython module. Now, cython.inline sort of does this, but it also preprocesses the code under the assumption that it is a function-level chunk, and then excutes this chunck, automagically fetching unbound variables from the invoking envirionment.

While this is very handy in many applications, I would also find it useful at runtime compile many defs/cdefs/cpdefs at the same time, and manually control passing in/out data. The main use case would be using cython as a backend for dynamic code generation.

First I tried to improve cython.inline to make this easier, but it felt like constructing a better screwdriver in order to hammer in a nail. Instead I wrote cython.compile_module (mostly reusing cython.inline) which just compiles a string as if it was a .pyx file, returning the module object. Patch attached.

Attachments

compile_module.patch Download (8.3 KB) - added by bfredl 17 months ago.

Change History

Changed 17 months ago by bfredl

Changed 5 months ago by scoder

  • cc bfredl, scoder added

A couple of minor comments:

- I'm not a big fan of special casing NumPy? imports. - "f" isn't a good variable name for a code string (in Shadow.py). - There should eventually be one right way to compile a file, not several ones in Inline.py, pyximport, the test runner and maybe even other places. (Not really a comment on this patch, more of a general rant kind of thing.)

That being said, I think this feature is generally a nice thing to have. Github is a better place to provide patches, though, as it simplifies comments and discussions on pull requests.

Note: See TracTickets for help on using tickets.