changeset 74:6517417e75b2

Fix crashes on exit().
author David Grundberg <individ@acc.umu.se>
date Fri, 03 Jul 2009 13:49:19 +0200
parents 077a44d23b54
children b0991511a16d f6e8abc9a8cf
files ChangeLog package/pytave.py pytave.cc
diffstat 3 files changed, 44 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Jul 01 14:37:29 2009 +0200
+++ b/ChangeLog	Fri Jul 03 13:49:19 2009 +0200
@@ -1,3 +1,10 @@
+2009-07-03  David Grundberg  <individ@acc.umu.se>
+
+	* pytave.cc: Sorted includes.
+	(atexit): New function. Call do_octave_atexit before Python
+	unloads shared libraries.
+	* package/pytave.py (_atexit): New function.
+
 2009-07-01  David Grundberg  <individ@acc.umu.se>
 
 	* pytave.cc [HAVE_USELOCALE] (init, func_eval, str_eval): Run
--- a/package/pytave.py	Wed Jul 01 14:37:29 2009 +0200
+++ b/package/pytave.py	Fri Jul 03 13:49:19 2009 +0200
@@ -20,11 +20,11 @@
 
 """Python to Octave bridge"""
 
+import UserDict
 import _pytave
-import UserDict
+import atexit
 import sys
 
-
 arg0 = sys.argv[0]
 interactive = sys.stdin.isatty() and (arg0 == '' or arg0 == '-')
 
@@ -44,6 +44,11 @@
 else:
     raise ImportError("Failed to import module: %s" % __modname__)
 
+def _atexit():
+	_pytave.atexit()
+
+atexit.register(_atexit)
+
 def feval(nargout, funcname, *arguments):
 
 	"""Executes an Octave function called funcname.
--- a/pytave.cc	Wed Jul 01 14:37:29 2009 +0200
+++ b/pytave.cc	Fri Jul 03 13:49:19 2009 +0200
@@ -24,27 +24,29 @@
 #include <boost/python/numeric.hpp>
 
 #undef HAVE_STAT /* Both boost.python and octave define HAVE_STAT... */
+#undef HAVE_FSTAT /* Both boost.python and octave define HAVE_FSTAT... */
 #include <octave/oct.h>
+
 #include <octave/oct-map.h>
 #include <octave/octave.h>
 #include <octave/ov.h>
 #include <octave/parse.h>
-#include <octave/utils.h>
 #include <octave/symtab.h>
 #include <octave/toplev.h>
+#include <octave/utils.h>
 
 #include <iostream>
-#include <sstream>
-#include <sys/types.h>
 #ifdef HAVE_USELOCALE
 #include <locale.h>
 #endif
+#include <sstream>
+#include <sys/types.h>
 
 #include "pytavedefs.h"
 
 #include "exceptions.h"
+#include "octave_to_python.h"
 #include "python_to_octave.h"
-#include "octave_to_python.h"
 
 using namespace boost::python;
 using namespace std;
@@ -341,6 +343,29 @@
          }
    }
 
+// Make sure Octave is correctly unloaded. We cannot depend on Octave running
+// at the (true) process atexit point, because at that time the Octave library
+// might have been unloaded.
+//
+// At least that is the hypothesis, since Octave (in certain circumstances)
+// cause a segmentation fault in do_octave_atexit called from the exit
+// function. (One Octave call that causes this is "sleep(0)".)
+   void atexit () {
+#ifdef HAVE_USELOCALE
+      // Set C locale
+      locale_t old_locale = uselocale(c_locale);
+#endif
+
+      Py_BEGIN_ALLOW_THREADS
+      do_octave_atexit();
+      Py_END_ALLOW_THREADS
+
+#ifdef HAVE_USELOCALE
+      // Reset locale
+      uselocale(old_locale);
+#endif
+   }
+
 } /* namespace pytave }}} */
 
 BOOST_PYTHON_MODULE(_pytave) { /* {{{ */
@@ -356,6 +381,7 @@
    def("delvar", pytave::delvar);
    def("push_scope", pytave::push_scope);
    def("pop_scope", pytave::pop_scope);
+   def("atexit", pytave::atexit);
    def("get_exceptions", pytave::get_exceptions);
 
    register_exception_translator<pytave::pytave_exception>(