Jakub Dorňák 44a4c48
diff -up boost_1_55_0/libs/python/test/exec.cpp\~ boost_1_55_0/libs/python/test/exec.cpp
Jakub Dorňák 44a4c48
--- boost_1_55_0/libs/python/test/exec.cpp~	2010-07-05 00:38:38.000000000 +0200
Jakub Dorňák 44a4c48
+++ boost_1_55_0/libs/python/test/exec.cpp	2015-01-09 21:31:12.903218280 +0100
Jakub Dorňák 44a4c48
@@ -56,6 +56,20 @@ void eval_test()
Jakub Dorňák 44a4c48
   BOOST_TEST(value == "ABCDEFG");
Jakub Dorňák 44a4c48
 }
Jakub Dorňák 44a4c48
 
Jakub Dorňák 44a4c48
+struct PyCtx
Jakub Dorňák 44a4c48
+{
Jakub Dorňák 44a4c48
+  PyCtx() {
Jakub Dorňák 44a4c48
+    Py_Initialize();
Jakub Dorňák 44a4c48
+  }
Jakub Dorňák 44a4c48
+
Jakub Dorňák 44a4c48
+  ~PyCtx() {
Jakub Dorňák 44a4c48
+    // N.B. certain problems may arise when Py_Finalize is called when
Jakub Dorňák 44a4c48
+    // using Boost.Python.  However in this test suite it all seems to
Jakub Dorňák 44a4c48
+    // work fine.
Jakub Dorňák 44a4c48
+    Py_Finalize();
Jakub Dorňák 44a4c48
+  }
Jakub Dorňák 44a4c48
+};
Jakub Dorňák 44a4c48
+
Jakub Dorňák 44a4c48
 void exec_test()
Jakub Dorňák 44a4c48
 {
Jakub Dorňák 44a4c48
   // Register the module with the interpreter
Jakub Dorňák 44a4c48
@@ -68,6 +82,8 @@ void exec_test()
Jakub Dorňák 44a4c48
                              ) == -1) 
Jakub Dorňák 44a4c48
     throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
Jakub Dorňák 44a4c48
                  "builtin modules");
Jakub Dorňák 44a4c48
+
Jakub Dorňák 44a4c48
+  PyCtx ctx;
Jakub Dorňák 44a4c48
   // Retrieve the main module
Jakub Dorňák 44a4c48
   python::object main = python::import("__main__");
Jakub Dorňák 44a4c48
   
Jakub Dorňák 44a4c48
@@ -148,41 +164,43 @@ void check_pyerr(bool pyerr_expected=fal
Jakub Dorňák 44a4c48
   }
Jakub Dorňák 44a4c48
 }
Jakub Dorňák 44a4c48
 
Jakub Dorňák 44a4c48
+template <class Cb>
Jakub Dorňák 44a4c48
+bool
Jakub Dorňák 44a4c48
+run_and_handle_exception(Cb cb, bool pyerr_expected = false)
Jakub Dorňák 44a4c48
+{
Jakub Dorňák 44a4c48
+  PyCtx ctx;
Jakub Dorňák 44a4c48
+  if (python::handle_exception(cb)) {
Jakub Dorňák 44a4c48
+    check_pyerr(pyerr_expected);
Jakub Dorňák 44a4c48
+    return true;
Jakub Dorňák 44a4c48
+  } else {
Jakub Dorňák 44a4c48
+    return false;
Jakub Dorňák 44a4c48
+  }
Jakub Dorňák 44a4c48
+}
Jakub Dorňák 44a4c48
+
Jakub Dorňák 44a4c48
 int main(int argc, char **argv)
Jakub Dorňák 44a4c48
 {
Jakub Dorňák 44a4c48
   BOOST_TEST(argc == 2 || argc == 3);
Jakub Dorňák 44a4c48
   std::string script = argv[1];
Jakub Dorňák 44a4c48
-  // Initialize the interpreter
Jakub Dorňák 44a4c48
-  Py_Initialize();
Jakub Dorňák 44a4c48
 
Jakub Dorňák 44a4c48
-  if (python::handle_exception(eval_test)) {
Jakub Dorňák 44a4c48
-    check_pyerr();
Jakub Dorňák 44a4c48
-  }
Jakub Dorňák 44a4c48
-  else if(python::handle_exception(exec_test)) {
Jakub Dorňák 44a4c48
-    check_pyerr();
Jakub Dorňák 44a4c48
-  }
Jakub Dorňák 44a4c48
-  else if (python::handle_exception(boost::bind(exec_file_test, script))) {
Jakub Dorňák 44a4c48
+  // N.B. exec_test mustn't be called through run_and_handle_exception
Jakub Dorňák 44a4c48
+  // as it needs to handles the python context by itself.
Jakub Dorňák 44a4c48
+  if (run_and_handle_exception(eval_test)
Jakub Dorňák 44a4c48
+      || python::handle_exception(exec_test))
Jakub Dorňák 44a4c48
     check_pyerr();
Jakub Dorňák 44a4c48
-  }
Jakub Dorňák 44a4c48
-  
Jakub Dorňák 44a4c48
-  if (python::handle_exception(exec_test_error))
Jakub Dorňák 44a4c48
-  {
Jakub Dorňák 44a4c48
-    check_pyerr(/*pyerr_expected*/ true);
Jakub Dorňák 44a4c48
-  }
Jakub Dorňák 44a4c48
   else
Jakub Dorňák 44a4c48
-  {
Jakub Dorňák 44a4c48
+    run_and_handle_exception(boost::bind(exec_file_test, script));
Jakub Dorňák 44a4c48
+
Jakub Dorňák 44a4c48
+  if (!run_and_handle_exception(exec_test_error, true))
Jakub Dorňák 44a4c48
     BOOST_ERROR("Python exception expected, but not seen.");
Jakub Dorňák 44a4c48
-  }
Jakub Dorňák 44a4c48
 
Jakub Dorňák 44a4c48
   if (argc > 2) {
Jakub Dorňák 44a4c48
+    PyCtx ctx;
Jakub Dorňák 44a4c48
     // The main purpose is to test compilation. Since this test generates
Jakub Dorňák 44a4c48
     // a file and I (rwgk) am uncertain about the side-effects, run it only
Jakub Dorňák 44a4c48
     // if explicitly requested.
Jakub Dorňák 44a4c48
     exercise_embedding_html();
Jakub Dorňák 44a4c48
   }
Jakub Dorňák 44a4c48
 
Jakub Dorňák 44a4c48
-  // Boost.Python doesn't support Py_Finalize yet.
Jakub Dorňák 44a4c48
-  // Py_Finalize();
Jakub Dorňák 44a4c48
   return boost::report_errors();
Jakub Dorňák 44a4c48
 }
Jakub Dorňák 44a4c48
 
Jakub Dorňák 44a4c48
Jakub Dorňák 44a4c48
Diff finished.  Fri Jan  9 21:31:13 2015