tnintemann / rpms / boost

Forked from rpms/boost 3 years ago
Clone

Blame SOURCES/boost-1.53.0-python-test-PyImport_AppendInittab.patch

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