I just implemented forking codewriters. I'll use them for temporaries,
but they have other usecases as well (I used it to clean up
ModuleNode.generate_c_code as well, and I might want to add (in
addition) utility code registration in the code writer).
The concept is this: Whenever you have a codewriter, you can call fork()
on it:
declarations_code = code.fork()
and that will "drop off" a codewriter at the current location (there
shouldn't be any perforamance hit compared with what is in Cython today,
everything was buffered anyway and buffers are never copied). One can go
on writing to code, and once one is done, one can write to
declarations_code to insert something at the point it was forked off.
Example:
h_code = code.fork()
...
self.generate_filename_table(code)
self.generate_utility_functions(env, code, h_code)
...
f = open_new_file(result.c_file)
# automatically includes what was written to h_code at right location:
code.copyto(f)
f.close()
How does this handle state? There is no general rule, this depends on
the state being handled and the amounts of synchronization one is
willing to go to. For "local function state" (labels and soon
temporaries) I've added enter/exit_cfunc_scope which sets up a label and
temp context, and this context is not available to the forked code
writer, only the "main" one. This is enough as it allows me to leave off
a non-temp-using code writer for the function declarations, and you
cannot really synchronize the temps either.
For other state (such as perhaps utility code some day) it is easier to
make a global state, so that the root codewriter sets up a utility code
context which is shared by every forked codewriter.
Especially the AnnotationCodeWriter might need special attention in this
process -- it work ok now, but that might be because I haven't really
started forking yet.
--
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev