# HG changeset patch # User Mike Miller # Date 1472152262 25200 # Node ID 1470ed26917afacd31582c1870d4bf44b8993f4d # Parent 445df7f96fbca8769d91ae819aac7b0be848e62f Use pyobject_unwrap_object when an existing PyObject is expected * pycall.cc (Fpycall): Use pyobject_unwrap_object when argument is an object. * pyeval.cc (Fpyeval): Use pyobject_unwrap_object on optional argument. Clean up imports no longer necessary. * pyeval.cc (Fpyeval): Likewise. * oct-py-util.cc, oct-py-util.h (pytave::get_builtins_module, pytave::get_object_from_python): Delete unused functions. Update copyright notice, file has been completely rewritten. diff -r 445df7f96fbc -r 1470ed26917a oct-py-util.cc --- a/oct-py-util.cc Thu Aug 25 11:39:45 2016 -0700 +++ b/oct-py-util.cc Thu Aug 25 12:11:02 2016 -0700 @@ -1,6 +1,6 @@ /* -Copyright (C) 2016 Abhinav Tripathi +Copyright (C) 2016 Mike Miller This file is part of Pytave. @@ -30,34 +30,12 @@ #include "oct-py-types.h" #include "oct-py-util.h" -using namespace boost::python; +// FIXME: only here for boost::python::error_already_set +#include namespace pytave { -void -get_builtins_module (boost::python::object& builtins_module) -{ -#if PY_VERSION_HEX >= 0x03000000 - builtins_module = import ("builtins"); -#else - builtins_module = import ("__builtin__"); -#endif -} - -void -get_object_from_python (const octave_value& oct_value, - boost::python::object& py_object) -{ - if (oct_value.is_object () && oct_value.class_name () == "pyobject") - { - PyObject *obj = pyobject_unwrap_object (oct_value); - py_object = boost::python::object (boost::python::handle<> (obj)); - } - else - py_object = boost::python::object (); // None -} - PyObject * py_builtins_module () { diff -r 445df7f96fbc -r 1470ed26917a oct-py-util.h --- a/oct-py-util.h Thu Aug 25 11:39:45 2016 -0700 +++ b/oct-py-util.h Thu Aug 25 12:11:02 2016 -0700 @@ -1,6 +1,6 @@ /* -Copyright (C) 2016 Abhinav Tripathi +Copyright (C) 2016 Mike Miller This file is part of Pytave. @@ -23,21 +23,15 @@ #if ! defined (pytave_oct_py_util_h) #define pytave_oct_py_util_h +#include +#include #include -#include class octave_value; namespace pytave { -void -get_builtins_module (boost::python::object& builtins_module); - -void -get_object_from_python (const octave_value& oct_value, - boost::python::object& py_object); - //! Return a reference to the builtins module. //! //! @return reference to the builtins module diff -r 445df7f96fbc -r 1470ed26917a pycall.cc --- a/pycall.cc Thu Aug 25 11:39:45 2016 -0700 +++ b/pycall.cc Thu Aug 25 12:11:02 2016 -0700 @@ -103,22 +103,19 @@ { PyObject *callable = 0; if (args(0).is_string ()) - callable = pytave::py_find_function (args(0).string_value ()); + { + callable = pytave::py_find_function (args(0).string_value ()); + if (! callable) + error ("pycall: no such Python function or callable: %s", + args(0).string_value ().c_str ()); + } else { - // FIXME: callable = get existing pyobject reference (args(0)) - boost::python::object obj; - pytave::get_object_from_python (args(0), obj); - if (obj.is_none ()) - error("pycall: FUNC must be a string or a Python reference"); - callable = obj.ptr (); - Py_INCREF (callable); + callable = pytave::pyobject_unwrap_object (args(0)); + if (! callable) + error("pycall: FUNC must be a valid Python reference"); } - if (! callable) - error ("pycall: no such Python function or callable: %s", - args(0).string_value ().c_str ()); - octave_value_list arglist = args.slice (1, nargin - 1); PyObject *result = pytave::py_call_function (callable, arglist); object res = object (handle (result)); diff -r 445df7f96fbc -r 1470ed26917a pyeval.cc --- a/pyeval.cc Thu Aug 25 11:39:45 2016 -0700 +++ b/pyeval.cc Thu Aug 25 12:11:02 2016 -0700 @@ -77,26 +77,19 @@ std::string code = args(0).string_value (); - std::string id; - Py_Initialize (); - object main_module = import ("__main__"); - object main_namespace = main_module.attr ("__dict__"); - object local_namespace; + PyObject *local_namespace = 0; if (nargin > 1) - { - pytave::get_object_from_python (args(1), local_namespace); - if (local_namespace.is_none ()) - error ("pyeval: NAMESPACE must be a string or a Python reference"); - } - else - local_namespace = main_namespace; + { + local_namespace = pytave::pyobject_unwrap_object (args(1)); + if (! local_namespace) + error ("pyeval: NAMESPACE must be a valid Python reference"); + } try { - PyObject *obj = pytave::py_eval_string (code, main_namespace.ptr (), - local_namespace.ptr ()); + PyObject *obj = pytave::py_eval_string (code, 0, local_namespace); boost::python::object res { boost::python::handle<> (obj) }; if (nargout > 0 || ! res.is_none ()) diff -r 445df7f96fbc -r 1470ed26917a pyexec.cc --- a/pyexec.cc Thu Aug 25 11:39:45 2016 -0700 +++ b/pyexec.cc Thu Aug 25 12:11:02 2016 -0700 @@ -73,24 +73,19 @@ Py_Initialize (); - object main_module = import ("__main__"); - object main_namespace = main_module.attr ("__dict__"); - object local_namespace; + PyObject *local_namespace = 0; if (nargin > 1) - { - pytave::get_object_from_python (args(1), local_namespace); - if (local_namespace.is_none ()) - error ("pyexec: NAMESPACE must be a string or a Python reference"); - } - else - local_namespace = main_namespace; + { + local_namespace = pytave::pyobject_unwrap_object (args(1)); + if (! local_namespace) + error ("pyexec: NAMESPACE must be a valid Python reference"); + } try { // FIXME: figure out exec return code: // http://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/exec.html - pytave::py_exec_string (code, main_namespace.ptr (), - local_namespace.ptr ()); + pytave::py_exec_string (code, 0, local_namespace); } catch (pytave::object_convert_exception const &) {