Hi,
Mark Lodato wrote:
> The embedded mode is a really good idea, but I think it would make
> more sense as a separate script for several reasons:
> 1. If it is separate, Cython itself doesn't have to change.
> 2. The same Cython C output file be made into a module and also embedded.
> 3. Multiple Cython modules can be embedded into a single executable.
Yep, that's what I thought, too.
> I have included a first draft of such a script. It is a modification
> of the embedded mode that was checked into cython-devel. Notably, I
> use PyImport_ExtendInittab to register all the modules as built-ins,
1) does that work for modules in packages?
2) will this decref the modules in Py3?
> and I only import the first one listed (which would ideally be called
> "__main__"; see below). This seems to be a cleaner way to do things.
> This also means that any extra module listed on the command-line that
> is not imported by the first module will not be run.
When is the init code of the modules called, and in which order? The main
module init code should be able to import and use all other modules, even
before executing its own init code.
> Other (minor) changes:
> * I call Py_SetProgramName(), which seems like a good idea.
Yes.
> * I create a MODINIT() macro to create the module initialization name,
> but I don't know how portable the ## concatenation is.
Also, the functions return different things in Py2 and Py3, so they are not
easily interchangeable.
> Please realize this is a first draft, and thus I am simply printing to
> standard output. I hope this can be of use to someone.
Sure, thanks!
> An additional idea (not implemented) - it would be awesome to have the
> first module listed (that is, the one directly imported) to have
> __name__ == "__main__", so the normal Python idiom works. I
> researched this for the past few hours, but I could not find a way to
> do this without modifying the module's code. My best solution is the
> following:
> 1. For each module, mymod.pyx:
> 1a. Add a new externally-visible global variable:
> int __pyx_mymod_is___main__ = 0;
> 1b. When converting the code for __name__, if __pyx_mymod_is___main__,
> return "__main__", else the module name.
> 2. In the script below, set
> __pyx_mainmod_is___main__ = 1;
> right before the call to PyImport_ImportModule(), where "mainmod" is
> the main module.
>
> I tried the above by modifying the Cython C output by hand and it
> seemed to work OK.
I thought about special casing the
if __name__ == '__main__':
paradigm so that it generates a function with only global variables instead
that would be callable from the embedding main(). In that case, we'd have a
problem with code like this:
a = 1
if __name__ == '__main__':
a = 2
print a
So I think that we'd need something like you describe.
BTW, my impression is that this isn't ready for cython-devel yet, given the
amount of open questions and design choices.
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev