What happens during and after boost::python::impor

2019-09-03 10:28发布

问题:

The Boost documentation does not specify this. When I use boost::python::import, where exactly is the specified module imported? My guess would be in '__main__' (just to make sure).

Also, what happens when the module object goes out of scope? Given the wording in Boost's docs — "Returns: An instance of object which holds a reference to the imported module" — I would guess only the reference goes out of scope, the module object is still found within '__main__' (or wherever it was imported).

回答1:

The underlying mechanism is PyImport_ImportModule, and no, it doesn't touch __main__. Successfully imported modules are inserted into sys.modules dict, so the object is not freed when you DECREF what the function returns — there is still at least that one reference alive.



标签: c++ python boost