I am writing an application that embeds a Python interpreter. The application also extends Python with a custom module. This is done along the lines of the "Extending and Embedding the Python Interpreter" part of the Python documentation. In the section "Extending Embedded Python" they describe how to "access to functionality from the application" and the example module emb
get access to argv
of the application's main
function by using a static variable numargs
.
Now, my question is, can I somehow pass data (like argv
) to my module in a more encapsulated way than using global state?
In contrast to the emb
example, I use multi-phase initialization and introduce module state in a struct via m_size
in my PyModuleDef
instance.
So I would have liked to somehow pass data to my Py_mod_exec
slot function to set it in my module state, but as the setup is made with raw C function pointers, I cannot see a way to pass data that way.
Alternatively, I've thought looking up the module after my Py_Initialize
call and pass the data via PyModule_GetState
, but I don't see how I can lookup my multi-phase initialized module.
So my idea now is to set some global state somewhere on the interpreter and pass the data that way. But I have not looked much into that yet, as I was hoping for a better solution.
Alternatively, I've thought looking up the module after my
Py_Initialize
call and pass the data viaPyModule_GetState
, but I don't see how I can lookup my multi-phase initialized module
I ended up with that solution and used PyImport_ImportModule
to get a reference to the module.
See discussion on https://discuss.python.org/t/access-application-data-when-extending-embedded-python.