简体   繁体   中英

Check whether the interpreter is embedded

I'm creating Python binary module using Python C API. Also I'm creating a separate program which uses Python C API and runs some python scripts where the module might be used. The question is how to check (using Python C API) whether my module is loaded inside default python interpreter either inside my program ? I don't consider using global static variables in my module because of my program must not know about this module anything. Now I use Py_SetProgramName function in my program:

Py_SetProgramName("myprogram");

And check it inside my module:

bool isEmbedded = Py_GetProgramName() == std::string("myprogram");

It works fine, but I don't like this way as well. Are there any other approaches to solve this issue?

I wouldn't try to check. What I would do is have the embedding program either create a virtual module that contains the additional functionality, with the external module attempting to import this module and monkeypatching itself, or have it contain functions that the module will attempt to load and then call via function pointers. If the internal module fails to import or the functions fail to be loaded then the additional functionality will be unavailable and the module will run in a reduced-capability manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM