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

Charsley, Mark Mark.Charsley at radioscape.com
Wed Dec 11 11:59:51 CET 2002


> 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.

Mark


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
postmaster at radioscape.com.

This footnote also confirms that this email message has been scanned
for the presence of computer viruses known at the time of sending.

www.radioscape.com
**********************************************************************





More information about the Cplusplus-sig mailing list