changeset 405:478d83448b0b

Refactor Python initialization into a common function * oct-py-init.cc, oct-py-init.h: New files defining a pytave::py_init function. * __py_struct_from_dict__.cc, pycall.cc, pyeval.cc, pyexec.cc: Use it instead of initializing Python differently in each entry point. * Makefile.am (COMMON_SOURCE_FILES): Include oct-py-init.cc in the list. (PYTAVE_HEADER_FILES): Include oct-py-init.h in the list.
author Mike Miller <mtmiller@octave.org>
date Sat, 29 Apr 2017 15:43:42 -0700
parents aef165ff92b0
children 16e79a1e96b8
files Makefile.am __py_struct_from_dict__.cc oct-py-init.cc oct-py-init.h pycall.cc pyeval.cc pyexec.cc
diffstat 7 files changed, 108 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.am	Fri Apr 28 16:21:39 2017 -0700
+++ b/Makefile.am	Sat Apr 29 15:43:42 2017 -0700
@@ -31,6 +31,7 @@
   exceptions.cc \
   octave_to_python.cc \
   oct-py-eval.cc \
+  oct-py-init.cc \
   oct-py-types.cc \
   oct-py-util.cc \
   python_to_octave.cc
@@ -68,6 +69,7 @@
   config.h \
   exceptions.h \
   oct-py-eval.h \
+  oct-py-init.h \
   oct-py-object.h \
   oct-py-types.h \
   oct-py-util.h \
--- a/__py_struct_from_dict__.cc	Fri Apr 28 16:21:39 2017 -0700
+++ b/__py_struct_from_dict__.cc	Sat Apr 29 15:43:42 2017 -0700
@@ -27,9 +27,8 @@
 #include <Python.h>
 #include <octave/oct.h>
 
-#define PYTAVE_DO_DECLARE_SYMBOL
-#include "arrayobjectdefs.h"
 #include "exceptions.h"
+#include "oct-py-init.h"
 #include "oct-py-object.h"
 #include "oct-py-types.h"
 #include "oct-py-util.h"
@@ -49,7 +48,7 @@
   if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
     error ("__py_class_name__: argument must be a valid Python object");
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
   std::string name = pytave::py_object_class_name (obj);
@@ -87,7 +86,7 @@
   if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
     error ("pyobject.int64: argument must be a Python object");
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
   if (! obj)
@@ -139,7 +138,7 @@
   if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
     error ("pyobject.uint64: argument must be a Python object");
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
   if (! obj)
@@ -187,7 +186,7 @@
   if (args.length () != 1)
     print_usage ();
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
 
@@ -236,7 +235,7 @@
 
   typestr = typestr.substr (3);
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   try
     {
@@ -272,7 +271,8 @@
   if (args.length () != 1)
     print_usage ();
 
-  Py_Initialize ();
+  pytave::py_init ();
+
   uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_del__: KEY must be an integer");
   pytave::py_objstore_del (key);
 
@@ -290,7 +290,8 @@
   if (args.length () != 1)
     print_usage ();
 
-  Py_Initialize ();
+  pytave::py_init ();
+
   uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_get__: KEY must be an integer");
   PyObject *obj = pytave::py_objstore_get (key);
 
@@ -313,9 +314,8 @@
   if (args.length () != 1)
     print_usage ();
 
-  Py_Initialize ();
-  boost::python::numeric::array::set_module_and_type ("numpy", "ndarray");
-  _import_array ();
+  pytave::py_init ();
+
   // FIXME: PyObject *obj = convert argument to Python (args(0));
   PyObject *obj = nullptr;
   try
@@ -352,7 +352,7 @@
   if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
     error ("pyobject.char: argument must be a valid Python object");
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
   if (! obj)
@@ -395,7 +395,7 @@
   if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
     error ("pyobject.struct: argument must be a Python object");
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   try
     {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/oct-py-init.cc	Sat Apr 29 15:43:42 2017 -0700
@@ -0,0 +1,51 @@
+/*
+
+Copyright (C) 2017 Mike Miller
+
+This file is part of Pytave.
+
+Pytave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+Pytave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Pytave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#if defined (HAVE_CONFIG_H)
+#  include <config.h>
+#endif
+
+#include <Python.h>
+
+#include "oct-py-init.h"
+
+// FIXME: the following are only needed for Boost.Python library and NumPy
+// library initialization
+#define PYTAVE_DO_DECLARE_SYMBOL
+#include <boost/python.hpp>
+#include "arrayobjectdefs.h"
+
+namespace pytave
+{
+
+  void
+  py_init ()
+  {
+    Py_Initialize ();
+
+    // FIXME: these are only needed for Boost.Python implicit conversion
+    // of Octave arrays to NumPy arrays
+    boost::python::numeric::array::set_module_and_type ("numpy", "ndarray");
+    _import_array ();
+  }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/oct-py-init.h	Sat Apr 29 15:43:42 2017 -0700
@@ -0,0 +1,35 @@
+/*
+
+Copyright (C) 2017 Mike Miller
+
+This file is part of Pytave.
+
+Pytave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+Pytave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Pytave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#if ! defined (pytave_oct_py_init_h)
+#define pytave_oct_py_init_h 1
+
+namespace pytave
+{
+
+  //! Initialize the Python interpreter and execution environment.
+  void
+  py_init ();
+
+}
+
+#endif
--- a/pycall.cc	Fri Apr 28 16:21:39 2017 -0700
+++ b/pycall.cc	Sat Apr 29 15:43:42 2017 -0700
@@ -30,10 +30,9 @@
 #include <octave/oct.h>
 #include <octave/parse.h>
 
-#define PYTAVE_DO_DECLARE_SYMBOL
-#include "arrayobjectdefs.h"
 #include "exceptions.h"
 #include "oct-py-eval.h"
+#include "oct-py-init.h"
 #include "oct-py-object.h"
 #include "oct-py-util.h"
 #include "octave_to_python.h"
@@ -94,10 +93,7 @@
                                   && args(0).class_name () == "pyobject")))
     error ("pycall: FUNC must be a string or a Python reference");
 
-  Py_Initialize ();
-
-  numeric::array::set_module_and_type ("numpy", "ndarray");
-  _import_array ();
+  pytave::py_init ();
 
   try
     {
--- a/pyeval.cc	Fri Apr 28 16:21:39 2017 -0700
+++ b/pyeval.cc	Sat Apr 29 15:43:42 2017 -0700
@@ -31,10 +31,9 @@
 #include <oct.h>
 #include <octave/parse.h>
 
-#define PYTAVE_DO_DECLARE_SYMBOL
-#include "arrayobjectdefs.h"
 #include "exceptions.h"
 #include "oct-py-eval.h"
+#include "oct-py-init.h"
 #include "oct-py-util.h"
 #include "python_to_octave.h"
 
@@ -77,7 +76,7 @@
 
   std::string code = args(0).string_value ();
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   PyObject *local_namespace = nullptr;
   if (nargin > 1)
--- a/pyexec.cc	Fri Apr 28 16:21:39 2017 -0700
+++ b/pyexec.cc	Sat Apr 29 15:43:42 2017 -0700
@@ -30,10 +30,9 @@
 
 #include <oct.h>
 
-#define PYTAVE_DO_DECLARE_SYMBOL
-#include "arrayobjectdefs.h"
 #include "exceptions.h"
 #include "oct-py-eval.h"
+#include "oct-py-init.h"
 #include "oct-py-util.h"
 #include "python_to_octave.h"
 
@@ -71,7 +70,7 @@
 
   std::string code = args(0).string_value ();
 
-  Py_Initialize ();
+  pytave::py_init ();
 
   PyObject *local_namespace = nullptr;
   if (nargin > 1)