[C++-sig] PyFinalize Safety

Dave Wolfe dwolfe at gforcetech.com
Sun Feb 18 16:04:24 CET 2007


Stefan Seefeld mentioned in a previous thread (see 'PyFinalize Safety'):

> (nit-picking: all the above could be written using only boost.python)

> You don't have to use the main dictionary. I believe you may create a
> new one, as long as you make sure that all the required builtin
> objects are there (copied from the main one, probably).

I have a somewhat related question.  I have a function to create python
objects that looks like this:

bpl::object CreatePythonCallable(const char* fileName,
                                 const char* className)
{
    try {
        bpl::object main = bpl::import("__main__");
        bpl::object global = main.attr("__dict__");
        bpl::exec_file(fileName, global, global);
        bpl::object ClassType = global[className];
        bpl::object retval = ClassType();
        return retval;
    }
    catch (bpl::error_already_set const&) {
        std::cerr << GetPythonException();
    }

    return bpl::object();  // <-- return 'None'
}

This works fine, except that test blocks in my code that look like this:

  if __name__ == '__main__':
      DoTests()

get executed when run from C++.  How do I arrange for the code to be
executed in another context, using only boost.python calls?





More information about the Cplusplus-sig mailing list