# HG changeset patch # User Mike Miller # Date 1471386845 25200 # Node ID baff3b90dcb18d4df314e58e3b289f8dc5df45d4 # Parent 9d95f087e5aaec860c25c65a6c70b3d704a456a3 __py_struct_from_dict__: new private compiled conversion function * __py_struct_from_dict__.cc: New private internal compiled function to convert a Python dict to a scalar struct. * Makefile.am (OCT_FILES): Add __py_struct_from_dict__.oct to the list. diff -r 9d95f087e5aa -r baff3b90dcb1 Makefile.am --- a/Makefile.am Tue Aug 16 15:29:01 2016 -0700 +++ b/Makefile.am Tue Aug 16 15:34:05 2016 -0700 @@ -40,6 +40,7 @@ README.md OCT_FILES = \ + __py_struct_from_dict__.oct \ pycall.oct \ pyeval.oct \ pyexec.oct diff -r 9d95f087e5aa -r baff3b90dcb1 __py_struct_from_dict__.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/__py_struct_from_dict__.cc Tue Aug 16 15:34:05 2016 -0700 @@ -0,0 +1,85 @@ +/* + +Copyright (C) 2016 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 + +#define PYTAVE_DO_DECLARE_SYMBOL +#include "arrayobjectdefs.h" +#include "exceptions.h" +#include "oct-py-types.h" +#include "octave_to_python.h" + +DEFUN_DLD (__py_struct_from_dict__, args, nargout, + "-*- texinfo -*-\n\ +@deftypefn {} {} __py_struct_from_dict__ (@var{dict})\n\ +Extract a scalar struct from the Python dict @var{dict}.\n\ +\n\ +This is a private internal function not intended for direct use.\n\ +@end deftypefn") +{ + octave_value_list retval; + std::string id; + + int nargin = args.length (); + + if (nargin != 1) + { + print_usage (); + return retval; + } + + if (! (args(0).is_object () && args(0).class_name () == "pyobject")) + error ("pyobject.struct: argument must be a Python object"); + + Py_Initialize (); + + try + { + // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); + boost::python::object arg; + pytave::octvalue_to_pyobj (arg, args(0)); + PyObject *obj = arg.ptr (); + + retval(0) = pytave::extract_py_scalar_map (obj); + } + catch (pytave::object_convert_exception const &) + { + error ("pyobject.struct: error in return value type conversion"); + } + catch (boost::python::error_already_set const &) + { + std::string message = pytave::fetch_exception_message (); + error ("pyobject.struct: %s", message.c_str ()); + } + + return retval; +} + +/* +## No test needed for internal helper function. +%!assert (1) +*/