# HG changeset patch # User Mike Miller # Date 1493505822 25200 # Node ID 478d83448b0be5fdbba5092bbbd6b4276ced78ef # Parent aef165ff92b0dd5ae2e20778961cbdb20e60cfad 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. diff -r aef165ff92b0 -r 478d83448b0b Makefile.am --- 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 \ diff -r aef165ff92b0 -r 478d83448b0b __py_struct_from_dict__.cc --- 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 #include -#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 { diff -r aef165ff92b0 -r 478d83448b0b oct-py-init.cc --- /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 +. + +*/ + +#if defined (HAVE_CONFIG_H) +# include +#endif + +#include + +#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 +#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 (); + } + +} diff -r aef165ff92b0 -r 478d83448b0b oct-py-init.h --- /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 +. + +*/ + +#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 diff -r aef165ff92b0 -r 478d83448b0b pycall.cc --- 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 #include -#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 { diff -r aef165ff92b0 -r 478d83448b0b pyeval.cc --- 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 #include -#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) diff -r aef165ff92b0 -r 478d83448b0b pyexec.cc --- 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 -#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)