[C++-sig] Reinitialization of interpreter with Boost.Python
prog at msobczak.com
Wed Apr 7 10:58:27 CEST 2004
I use Boost.Python (from 1.30.2 release) for interpreter embedding.
The idea is to run short, customized scripts from within the C++
application and have the scripts operate on application's data structures.
My use of Python interpreter is tightly scoped and what I need is the
possibility to *repeatedly* do the following:
1. initialize the interpreter
2. import module defined in the application's code
3. execute some Python script
4. finalize the interpreter
The problem is that the second time (after the interpreter is
re-initialized) my module is imported, the code that defines classes
cannot go past the registration procedure.
This is due to the fact that the slots for conversion functions cannot
be reused and the code stumbles either on assertion (in Debug build) or
the exception is thrown (in Release build).
The "offending" code is in the converter/registry.cpp file (of
void insert(to_python_function_t f, type_info source_t)
# ifdef BOOST_PYTHON_TRACE_REGISTRY
std::cout << "inserting to_python " << source_t << "\n";
to_python_function_t& slot = get(source_t)->m_to_python;
assert(slot == 0); // we have a problem otherwise
if (slot != 0)
"trying to register to_python_converter for a type which
already has a registered to_python_converter");
slot = f;
Considering the fact that what I need is the possibility to reinitialize
the interpreter in exactly the same manner, importing the same module
that defines the same classes - the second time the module is imported,
the converion function is exactly the same.
I have hacked the above function and removed both assertion and if, so
that the conversion function pointer is always written into the slot.
The second time my module is imported (after the interpreter is
finalized and re-initialized), the insert function above will just
overwrite the slot with the same value as before.
Interestingly, it started to work as expected, at least with simple tests.
What bothers me is the claim I found on Boost site, that
reinitialization of Python interpreter is *not* supported due to the
reference-counting problems affecting some global objects.
I do not experience any problems *at the moment*, but I would like to
know where the trolls may come from.
What is exactly the problem? What should I be aware of?
Thank you very much for your time,
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
More information about the Cplusplus-sig