Ticket #10923: boost-1.55.0-python-test-PyImport_AppendInittab.patch

File boost-1.55.0-python-test-PyImport_AppendInittab.patch, 2.6 KB (added by Petr Machata <pmachata@…>, 8 years ago)

A fix

  • boost_1_55_0/libs/python/test/exec.cpp

    diff -up boost_1_55_0/libs/python/test/exec.cpp\~ boost_1_55_0/libs/python/test/exec.cpp
    old new void eval_test()  
    5656  BOOST_TEST(value == "ABCDEFG");
    5757}
    5858
     59struct PyCtx
     60{
     61  PyCtx() {
     62    Py_Initialize();
     63  }
     64
     65  ~PyCtx() {
     66    // N.B. certain problems may arise when Py_Finalize is called when
     67    // using Boost.Python.  However in this test suite it all seems to
     68    // work fine.
     69    Py_Finalize();
     70  }
     71};
     72
    5973void exec_test()
    6074{
    6175  // Register the module with the interpreter
    void exec_test()  
    6882                             ) == -1)
    6983    throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
    7084                 "builtin modules");
     85
     86  PyCtx ctx;
    7187  // Retrieve the main module
    7288  python::object main = python::import("__main__");
    7389 
    void check_pyerr(bool pyerr_expected=fal  
    148164  }
    149165}
    150166
     167template <class Cb>
     168bool
     169run_and_handle_exception(Cb cb, bool pyerr_expected = false)
     170{
     171  PyCtx ctx;
     172  if (python::handle_exception(cb)) {
     173    check_pyerr(pyerr_expected);
     174    return true;
     175  } else {
     176    return false;
     177  }
     178}
     179
    151180int main(int argc, char **argv)
    152181{
    153182  BOOST_TEST(argc == 2 || argc == 3);
    154183  std::string script = argv[1];
    155   // Initialize the interpreter
    156   Py_Initialize();
    157184
    158   if (python::handle_exception(eval_test)) {
    159     check_pyerr();
    160   }
    161   else if(python::handle_exception(exec_test)) {
    162     check_pyerr();
    163   }
    164   else if (python::handle_exception(boost::bind(exec_file_test, script))) {
     185  // N.B. exec_test mustn't be called through run_and_handle_exception
     186  // as it needs to handles the python context by itself.
     187  if (run_and_handle_exception(eval_test)
     188      || python::handle_exception(exec_test))
    165189    check_pyerr();
    166   }
    167  
    168   if (python::handle_exception(exec_test_error))
    169   {
    170     check_pyerr(/*pyerr_expected*/ true);
    171   }
    172190  else
    173   {
     191    run_and_handle_exception(boost::bind(exec_file_test, script));
     192
     193  if (!run_and_handle_exception(exec_test_error, true))
    174194    BOOST_ERROR("Python exception expected, but not seen.");
    175   }
    176195
    177196  if (argc > 2) {
     197    PyCtx ctx;
    178198    // The main purpose is to test compilation. Since this test generates
    179199    // a file and I (rwgk) am uncertain about the side-effects, run it only
    180200    // if explicitly requested.
    181201    exercise_embedding_html();
    182202  }
    183203
    184   // Boost.Python doesn't support Py_Finalize yet.
    185   // Py_Finalize();
    186204  return boost::report_errors();
    187205}
    188206