[C++-sig] Re: Presenting a Python embedding tutorial

Dirk Gerrits dirk at gerrits.homeip.net
Wed Dec 11 12:45:58 CET 2002


Charsley, Mark wrote:
>>A pseudo-implementation of the constructor and destructor 
>>could look like:
>>
>>     interpreter()
>>     {
>>         // Create the main interpreter
>>         if (interpreter_count == 0)
>>         {
>>             Py_Initialize();
>>             ...
>>         }
>>         // Create a sub-interpreter
>>         else
>>         {
>>             ...
>>         }
>>         interpreter_count++;
>>     }
>>     ~interpreter()
>>     {
>>         // Release the main interpreter
>>         if (interpreter_count == 1)
>>         {
>>             Py_Finalize();
>>             ...
>>         }
>>         // Release a sub-interpreter
>>         else
>>         {
>>             ...
>>         }
>>         interpreter_count--;
>>     }
> 
> 
> 
> That contains a dangerous flaw - it supposes that PyInitialize hasn't been
> called by anyone else, using code other than boost::python. Which means that
> code running in a boost::python extension can't create a sub-interpreter
> using boost::python. I strongly advise using Py_IsInitialized() instead of a
> static count in a C++ class.

Right. Good point. But how do you propose to fix the destructor?

Regards from pseudo-code-land,
Dirk Gerrits







More information about the Cplusplus-sig mailing list