So first the successes, and then I'll explain my current struggle...
So I've had more of a chance to mess around with things. I got things working using a modified version of cython_freeze against a set of modules in a directory. My modifications were just slicing up the existing cython_freeze so that instead of generating a main() function I instead generate an initialize, close, and optionally a main function (making use of the initialize and close routines). Resulting code ends up looking like this when ran against 2 modules (main_w and pysrc):
#include <Python.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
# define MODINIT(name) init ## name
PyMODINIT_FUNC MODINIT(main_w) (void);
PyMODINIT_FUNC MODINIT(pysrc) (void);
static struct _inittab inittab[] = {
{"main_w", MODINIT(main_w)},
{"pysrc", MODINIT(pysrc)},
{NULL, NULL}
};
extern int __pyx_module_is_main_main_w;
void Python_Initialize_Session() {
if (PyImport_ExtendInittab(inittab)) {
fprintf(stderr, "No memory\n");
exit(1);
}
Py_Initialize();
}
int Python_Close_Session() {
int r = 0;
if (PyErr_Occurred()) {
r = 1;
PyErr_Print(); /* This exits with the right code if SystemExit. */
if (Py_FlushLine()) PyErr_Clear();
}
Py_Finalize();
return r;
}
Again the intent is to have the ability to embed compiled python in a C project but allow more flexibility than just running a main file. In this case in C one could do something like this:
Python_Initialize_Session();
initpysrc(); //This is essentially the same as "import pysrc"
//Call whatever "cdef public" functions you want from pysrc and do procesing
Python_Close_Session(); // When all done
This setup seems like it would work quite well for a set of modules in a single directory, but doesn't really have the ability to deal with sub-directories containing packages (correct if I'm wrong). So now I'm pursuing more along the lines of Gabriel and looking to do some name mangling to get things to work with packages.
Gabriel, I've had a go at hacking up the schafer tool you referenced in an earlier post. I've got it pulling all desired files, name mangling, and putting resulting cpp files in a single build/<arch>/cython_src/ directory. What I'm struggling with is how to integrate the resulting <PKG>_glue.c file (with associated init<PKG>() function so that I can import and use that package elsewhere in my project.
I've tried many things all without success. First I tried calling the function prior to my Py_Initialize() call but that resulted in segfault issues since your making use of python API stuff in the function. I tried calling after Py_Initialize() without segfault, but can't seem to get any of my other code to be able to import any modules within the package.
I also tried hooking the init<PKG> call into my cython_freeze stuff so that the pkg would get mapped to the _glue init file when I did the extendInittab calls before Py_Initialize.
I'm thinking that the issues are either related to some misunderstanding I have about the __path__ stuff in the __init__.py of the packages, or that there something I don't have because I'm not using your patched python interpreter. On the __path__ stuff I've mirrored what you did your project. I'm still looking into your patches on the interpreter. You said before you only needed the patch to import.c when dealing with a statically compiled python. Right now I'm still doing dynamic linking, but perhaps there is still some need with something being done in the glue code.
I'll be working out a minimal example project to post, but wanted to see if anyone had any ideas.