# HG changeset patch # User Mike Miller # Date 1493413677 25200 # Node ID c4b78e449c626f41ad8e7ebf0035e697c7dbc871 # Parent 3a64a336d21440aa36fed4426767a9a0dd5dbb35 maint: indent functions declared in the pytave namespace * oct-py-eval.cc, oct-py-eval.h, oct-py-types.cc, oct-py-types.h, oct-py-util.cc, oct-py-util.h: Indent functions declared in the pytave namespace, adjust line wrap where necessary. diff -r 3a64a336d214 -r c4b78e449c62 oct-py-eval.cc --- a/oct-py-eval.cc Fri Apr 28 14:04:58 2017 -0700 +++ b/oct-py-eval.cc Fri Apr 28 14:07:57 2017 -0700 @@ -36,113 +36,113 @@ namespace pytave { -PyObject * -py_call_function (const std::string& func, const octave_value_list& args) -{ - PyObject *func_obj = py_find_function (func); - PyObject *retval = py_call_function (func_obj, args); - Py_DECREF (func_obj); - return retval; -} + PyObject * + py_call_function (const std::string& func, const octave_value_list& args) + { + PyObject *func_obj = py_find_function (func); + PyObject *retval = py_call_function (func_obj, args); + Py_DECREF (func_obj); + return retval; + } -PyObject * -py_call_function (const std::string& func, PyObject *args, PyObject *kwargs) -{ - PyObject *func_obj = py_find_function (func); - PyObject *retval = py_call_function (func_obj, args, kwargs); - Py_DECREF (func_obj); - return retval; -} + PyObject * + py_call_function (const std::string& func, PyObject *args, PyObject *kwargs) + { + PyObject *func_obj = py_find_function (func); + PyObject *retval = py_call_function (func_obj, args, kwargs); + Py_DECREF (func_obj); + return retval; + } -PyObject * -py_call_function (PyObject *callable, const octave_value_list& args) -{ - PyObject *kwargs = 0; - PyObject *args_list = PyList_New (0); - if (! args_list) - octave_throw_bad_alloc (); + PyObject * + py_call_function (PyObject *callable, const octave_value_list& args) + { + PyObject *kwargs = 0; + PyObject *args_list = PyList_New (0); + if (! args_list) + octave_throw_bad_alloc (); - for (int i = 0; i < args.length (); ++i) - { - boost::python::object arg; - pytave::octvalue_to_pyobj (arg, args(i)); - PyObject *obj = arg.ptr (); + for (int i = 0; i < args.length (); ++i) + { + boost::python::object arg; + pytave::octvalue_to_pyobj (arg, args(i)); + PyObject *obj = arg.ptr (); - if (pytave::is_py_kwargs_argument (obj)) - kwargs = pytave::update_py_dict (kwargs, obj); - else - { - Py_INCREF (obj); - PyList_Append (args_list, obj); - } - } + if (pytave::is_py_kwargs_argument (obj)) + kwargs = pytave::update_py_dict (kwargs, obj); + else + { + Py_INCREF (obj); + PyList_Append (args_list, obj); + } + } - PyObject *args_tuple = PyList_AsTuple (args_list); - Py_DECREF (args_list); + PyObject *args_tuple = PyList_AsTuple (args_list); + Py_DECREF (args_list); - PyObject *retval = py_call_function (callable, args_tuple, kwargs); - Py_DECREF (args_tuple); - Py_XDECREF (kwargs); + PyObject *retval = py_call_function (callable, args_tuple, kwargs); + Py_DECREF (args_tuple); + Py_XDECREF (kwargs); - return retval; -} + return retval; + } -PyObject * -py_call_function (PyObject *callable, PyObject *args, PyObject *kwargs) -{ - PyObject *retval = PyEval_CallObjectWithKeywords (callable, args, kwargs); - if (! retval) - throw boost::python::error_already_set (); + PyObject * + py_call_function (PyObject *callable, PyObject *args, PyObject *kwargs) + { + PyObject *retval = PyEval_CallObjectWithKeywords (callable, args, kwargs); + if (! retval) + throw boost::python::error_already_set (); - return retval; -} + return retval; + } -PyObject * -py_run_string_safe (const std::string& expr, int start, PyObject *globals, - PyObject *locals) -{ - bool alloc = false; + PyObject * + py_run_string_safe (const std::string& expr, int start, PyObject *globals, + PyObject *locals) + { + bool alloc = false; - if (! globals || (globals == Py_None)) - { - PyObject *main = py_import_module ("__main__"); - globals = PyModule_GetDict (main); - Py_DECREF (main); - if (! globals) - { - globals = PyDict_New (); - alloc = true; - } - } + if (! globals || (globals == Py_None)) + { + PyObject *main = py_import_module ("__main__"); + globals = PyModule_GetDict (main); + Py_DECREF (main); + if (! globals) + { + globals = PyDict_New (); + alloc = true; + } + } - if (! locals || (locals == Py_None)) - locals = globals; + if (! locals || (locals == Py_None)) + locals = globals; - // Evaluate all expressions under "from __future__ import print_function" - PyCompilerFlags flags { CO_FUTURE_PRINT_FUNCTION }; + // Evaluate all expressions under "from __future__ import print_function" + PyCompilerFlags flags { CO_FUTURE_PRINT_FUNCTION }; - PyObject *retval = PyRun_StringFlags (expr.c_str (), start, globals, locals, - &flags); + PyObject *retval = PyRun_StringFlags (expr.c_str (), start, globals, locals, + &flags); - if (alloc) - Py_DECREF (globals); + if (alloc) + Py_DECREF (globals); - if (! retval) - throw boost::python::error_already_set (); + if (! retval) + throw boost::python::error_already_set (); - return retval; -} + return retval; + } -PyObject * -py_eval_string (const std::string& expr, PyObject *globals, PyObject *locals) -{ - return py_run_string_safe (expr, Py_eval_input, globals, locals); -} + PyObject * + py_eval_string (const std::string& expr, PyObject *globals, PyObject *locals) + { + return py_run_string_safe (expr, Py_eval_input, globals, locals); + } -PyObject * -py_exec_string (const std::string& expr, PyObject *globals, PyObject *locals) -{ - return py_run_string_safe (expr, Py_file_input, globals, locals); -} + PyObject * + py_exec_string (const std::string& expr, PyObject *globals, PyObject *locals) + { + return py_run_string_safe (expr, Py_file_input, globals, locals); + } } diff -r 3a64a336d214 -r c4b78e449c62 oct-py-eval.h --- a/oct-py-eval.h Fri Apr 28 14:04:58 2017 -0700 +++ b/oct-py-eval.h Fri Apr 28 14:07:57 2017 -0700 @@ -31,57 +31,60 @@ namespace pytave { -//! Call a Python function by name with the given argument list. -//! -//! The @a func string may be the name of a builtin function or of a function -//! in a loadable module in the form @c module.function. -//! -//! If @a args contains one or more values created by the @c pyargs function, -//! they are automatically interpreted as keyword arguments. -//! -//! @param func name of a Python function -//! @param args Octave argument list to be converted and passed to @a func -//! @return return value of @a func -PyObject * -py_call_function (const std::string& func, const octave_value_list& args); + //! Call a Python function by name with the given argument list. + //! + //! The @a func string may be the name of a builtin function or of a function + //! in a loadable module in the form @c module.function. + //! + //! If @a args contains one or more values created by the @c pyargs function, + //! they are automatically interpreted as keyword arguments. + //! + //! @param func name of a Python function + //! @param args Octave argument list to be converted and passed to @a func + //! @return return value of @a func + PyObject * + py_call_function (const std::string& func, const octave_value_list& args); -//! Call a Python function by name with arguments and keyword arguments. -//! -//! The @a func string may be the name of a builtin function or of a function -//! in a loadable module in the form @c module.function. -//! -//! @param func name of a Python function -//! @param args tuple of positional arguments -//! @param kwargs dictionary of keyword arguments -//! @return return value of @a func -PyObject * -py_call_function (const std::string& func, PyObject *args, PyObject *kwargs = 0); + //! Call a Python function by name with arguments and keyword arguments. + //! + //! The @a func string may be the name of a builtin function or of a function + //! in a loadable module in the form @c module.function. + //! + //! @param func name of a Python function + //! @param args tuple of positional arguments + //! @param kwargs dictionary of keyword arguments + //! @return return value of @a func + PyObject * + py_call_function (const std::string& func, PyObject *args, + PyObject *kwargs = 0); -//! Call a Python function with the given argument list. -//! -//! If @a args contains one or more values created by the @c pyargs function, -//! they are automatically interpreted as keyword arguments. -//! -//! @param callable Python function or other callable object -//! @param args Octave argument list to be converted and passed to @a func -//! @return return value of @a func -PyObject * -py_call_function (PyObject *callable, const octave_value_list& args); + //! Call a Python function with the given argument list. + //! + //! If @a args contains one or more values created by the @c pyargs function, + //! they are automatically interpreted as keyword arguments. + //! + //! @param callable Python function or other callable object + //! @param args Octave argument list to be converted and passed to @a func + //! @return return value of @a func + PyObject * + py_call_function (PyObject *callable, const octave_value_list& args); -//! Call a Python function with arguments and keyword arguments. -//! -//! @param callable Python function or other callable object -//! @param args tuple of positional arguments -//! @param kwargs dictionary of keyword arguments -//! @return return value of @a func -PyObject * -py_call_function (PyObject *callable, PyObject *args, PyObject *kwargs = 0); + //! Call a Python function with arguments and keyword arguments. + //! + //! @param callable Python function or other callable object + //! @param args tuple of positional arguments + //! @param kwargs dictionary of keyword arguments + //! @return return value of @a func + PyObject * + py_call_function (PyObject *callable, PyObject *args, PyObject *kwargs = 0); -PyObject * -py_eval_string (const std::string& expr, PyObject *globals = 0, PyObject *locals = 0); + PyObject * + py_eval_string (const std::string& expr, PyObject *globals = 0, + PyObject *locals = 0); -PyObject * -py_exec_string (const std::string& expr, PyObject *globals = 0, PyObject *locals = 0); + PyObject * + py_exec_string (const std::string& expr, PyObject *globals = 0, + PyObject *locals = 0); } diff -r 3a64a336d214 -r c4b78e449c62 oct-py-types.cc --- a/oct-py-types.cc Fri Apr 28 14:04:58 2017 -0700 +++ b/oct-py-types.cc Fri Apr 28 14:07:57 2017 -0700 @@ -40,132 +40,133 @@ namespace pytave { -PyObject * -make_py_bool (bool value) -{ - if (value) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; -} + PyObject * + make_py_bool (bool value) + { + if (value) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } -PyObject * -make_py_complex (std::complex value) -{ - Py_complex& py_complex_value = reinterpret_cast (value); - return PyComplex_FromCComplex (py_complex_value); -} + PyObject * + make_py_complex (std::complex value) + { + Py_complex& py_complex_value = reinterpret_cast (value); + return PyComplex_FromCComplex (py_complex_value); + } -PyObject * -make_py_float (double value) -{ - return PyFloat_FromDouble (value); -} + PyObject * + make_py_float (double value) + { + return PyFloat_FromDouble (value); + } -bool -extract_py_bool (PyObject *obj) -{ - if (! obj) - throw object_convert_exception ("failed to extract boolean: null object"); + bool + extract_py_bool (PyObject *obj) + { + if (! obj) + throw object_convert_exception ("failed to extract boolean: null object"); - if (! PyBool_Check (obj)) - throw object_convert_exception ("failed to extract boolean: wrong type"); + if (! PyBool_Check (obj)) + throw object_convert_exception ("failed to extract boolean: wrong type"); - return (obj == Py_True); -} + return (obj == Py_True); + } -std::complex -extract_py_complex (PyObject *obj) -{ - if (! obj) - throw object_convert_exception ("failed to extract complex: null object"); + std::complex + extract_py_complex (PyObject *obj) + { + if (! obj) + throw object_convert_exception ("failed to extract complex: null object"); - if (! PyComplex_Check (obj)) - throw object_convert_exception ("failed to extract complex: wrong type"); + if (! PyComplex_Check (obj)) + throw object_convert_exception ("failed to extract complex: wrong type"); - Py_complex value = PyComplex_AsCComplex (obj); - return reinterpret_cast&> (value); -} + Py_complex value = PyComplex_AsCComplex (obj); + return reinterpret_cast&> (value); + } -double -extract_py_float (PyObject *obj) -{ - if (! obj) - throw object_convert_exception ("failed to extract float: null object"); + double + extract_py_float (PyObject *obj) + { + if (! obj) + throw object_convert_exception ("failed to extract float: null object"); - if (! PyFloat_Check (obj)) - throw object_convert_exception ("failed to extract float: wrong type"); + if (! PyFloat_Check (obj)) + throw object_convert_exception ("failed to extract float: wrong type"); - return PyFloat_AsDouble (obj); -} + return PyFloat_AsDouble (obj); + } -PyObject * -make_py_int (int32_t value) -{ + PyObject * + make_py_int (int32_t value) + { #if PY_VERSION_HEX >= 0x03000000 - return PyLong_FromLong (value); + return PyLong_FromLong (value); #else - return PyInt_FromLong (value); + return PyInt_FromLong (value); #endif -} + } -PyObject * -make_py_int (uint32_t value) -{ - return PyLong_FromUnsignedLong (value); -} + PyObject * + make_py_int (uint32_t value) + { + return PyLong_FromUnsignedLong (value); + } -PyObject * -make_py_int (int64_t value) -{ + PyObject * + make_py_int (int64_t value) + { #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG)) - return PyLong_FromLongLong (value); + return PyLong_FromLongLong (value); #else - return PyLong_FromLong (value); + return PyLong_FromLong (value); #endif -} + } -PyObject * -make_py_int (uint64_t value) -{ + PyObject * + make_py_int (uint64_t value) + { #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG)) - return PyLong_FromUnsignedLongLong (value); + return PyLong_FromUnsignedLongLong (value); #else - return PyLong_FromUnsignedLong (value); + return PyLong_FromUnsignedLong (value); #endif -} + } -PyObject * -make_py_array (const void *data, size_t len, char typecode) -{ - if (! typecode) - throw object_convert_exception ("unable to create array from Octave data"); + PyObject * + make_py_array (const void *data, size_t len, char typecode) + { + if (! typecode) + throw object_convert_exception + ("unable to create array from Octave data"); - std::string arg { typecode }; - PyObject *array = py_call_function ("array.array", ovl (arg)); + std::string arg { typecode }; + PyObject *array = py_call_function ("array.array", ovl (arg)); - if (len > 0) - { - // create a byte buffer containing a copy of the array binary data - const char *cdata = reinterpret_cast (data); - PyObject *buf = PyBytes_FromStringAndSize (cdata, len); - if (! buf) - octave_throw_bad_alloc (); + if (len > 0) + { + // create a byte buffer containing a copy of the array binary data + const char *cdata = reinterpret_cast (data); + PyObject *buf = PyBytes_FromStringAndSize (cdata, len); + if (! buf) + octave_throw_bad_alloc (); - PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ? - PyObject_GetAttrString (array, "frombytes") : - PyObject_GetAttrString (array, "fromstring")); - PyObject *args = PyTuple_Pack (1, buf); - py_call_function (frombytes, args); - Py_DECREF (args); - Py_DECREF (buf); - } + PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ? + PyObject_GetAttrString (array, "frombytes") : + PyObject_GetAttrString (array, "fromstring")); + PyObject *args = PyTuple_Pack (1, buf); + py_call_function (frombytes, args); + Py_DECREF (args); + Py_DECREF (buf); + } - return array; -} + return array; + } -// Prefer the 'q' and 'Q' typecodes if they are available (if Python 3 and -// built with support for long long integers) + // Prefer the 'q' and 'Q' typecodes if they are available (if Python 3 and + // built with support for long long integers) #if (PY_VERSION_HEX >= 0x03000000) && defined (HAVE_LONG_LONG) # define ARRAY_INT64_TYPECODE 'q' @@ -178,338 +179,341 @@ # define ARRAY_UINT64_TYPECODE 0 #endif -template -struct py_array_info { }; + template + struct py_array_info { }; -template <> -struct py_array_info { static const char typecode = 'b'; }; + template <> + struct py_array_info { static const char typecode = 'b'; }; -template <> -struct py_array_info { static const char typecode = 'h'; }; + template <> + struct py_array_info { static const char typecode = 'h'; }; -template <> -struct py_array_info { static const char typecode = 'i'; }; + template <> + struct py_array_info { static const char typecode = 'i'; }; -template <> -struct py_array_info -{ - static const char typecode = ARRAY_INT64_TYPECODE; -}; + template <> + struct py_array_info + { + static const char typecode = ARRAY_INT64_TYPECODE; + }; -template <> -struct py_array_info { static const char typecode = 'B'; }; + template <> + struct py_array_info { static const char typecode = 'B'; }; -template <> -struct py_array_info { static const char typecode = 'H'; }; + template <> + struct py_array_info { static const char typecode = 'H'; }; -template <> -struct py_array_info { static const char typecode = 'I'; }; + template <> + struct py_array_info { static const char typecode = 'I'; }; -template <> -struct py_array_info { - static const char typecode = ARRAY_UINT64_TYPECODE; -}; + template <> + struct py_array_info { + static const char typecode = ARRAY_UINT64_TYPECODE; + }; -PyObject * -make_py_array (const NDArray& nda) -{ - return make_py_array (nda.data (), nda.numel () * sizeof (double), 'd'); -} + PyObject * + make_py_array (const NDArray& nda) + { + return make_py_array (nda.data (), nda.numel () * sizeof (double), 'd'); + } -PyObject * -make_py_array (const FloatNDArray& nda) -{ - return make_py_array (nda.data (), nda.numel () * sizeof (float), 'f'); -} + PyObject * + make_py_array (const FloatNDArray& nda) + { + return make_py_array (nda.data (), nda.numel () * sizeof (float), 'f'); + } -template -PyObject * -make_py_array (const intNDArray& nda) -{ - return make_py_array (nda.data (), nda.numel () * sizeof (T), - py_array_info::typecode); -} + template + PyObject * + make_py_array (const intNDArray& nda) + { + return make_py_array (nda.data (), nda.numel () * sizeof (T), + py_array_info::typecode); + } -// Instantiate all possible integer array template functions needed + // Instantiate all possible integer array template functions needed -template PyObject * make_py_array (const int8NDArray&); -template PyObject * make_py_array (const int16NDArray&); -template PyObject * make_py_array (const int32NDArray&); -template PyObject * make_py_array (const int64NDArray&); -template PyObject * make_py_array (const uint8NDArray&); -template PyObject * make_py_array (const uint16NDArray&); -template PyObject * make_py_array (const uint32NDArray&); -template PyObject * make_py_array (const uint64NDArray&); + template PyObject * make_py_array (const int8NDArray&); + template PyObject * make_py_array (const int16NDArray&); + template PyObject * make_py_array (const int32NDArray&); + template PyObject * make_py_array (const int64NDArray&); + template PyObject * make_py_array (const uint8NDArray&); + template PyObject * make_py_array (const uint16NDArray&); + template PyObject * make_py_array (const uint32NDArray&); + template PyObject * make_py_array (const uint64NDArray&); -PyObject * -make_py_numeric_value (const octave_value& value) -{ - if (value.is_scalar_type ()) - { - if (value.is_bool_type ()) - return make_py_bool (value.bool_value ()); + PyObject * + make_py_numeric_value (const octave_value& value) + { + if (value.is_scalar_type ()) + { + if (value.is_bool_type ()) + return make_py_bool (value.bool_value ()); - else if (value.is_int8_type ()) - return make_py_int (value.int8_scalar_value ().value ()); - else if (value.is_int16_type ()) - return make_py_int (value.int16_scalar_value ().value ()); - else if (value.is_int32_type ()) - return make_py_int (value.int32_scalar_value ().value ()); - else if (value.is_int64_type ()) - return make_py_int (value.int64_scalar_value ().value ()); + else if (value.is_int8_type ()) + return make_py_int (value.int8_scalar_value ().value ()); + else if (value.is_int16_type ()) + return make_py_int (value.int16_scalar_value ().value ()); + else if (value.is_int32_type ()) + return make_py_int (value.int32_scalar_value ().value ()); + else if (value.is_int64_type ()) + return make_py_int (value.int64_scalar_value ().value ()); - else if (value.is_uint8_type ()) - return make_py_int (value.uint8_scalar_value ().value ()); - else if (value.is_uint16_type ()) - return make_py_int (value.uint16_scalar_value ().value ()); - else if (value.is_uint32_type ()) - return make_py_int (value.uint32_scalar_value ().value ()); - else if (value.is_uint64_type ()) - return make_py_int (value.uint64_scalar_value ().value ()); + else if (value.is_uint8_type ()) + return make_py_int (value.uint8_scalar_value ().value ()); + else if (value.is_uint16_type ()) + return make_py_int (value.uint16_scalar_value ().value ()); + else if (value.is_uint32_type ()) + return make_py_int (value.uint32_scalar_value ().value ()); + else if (value.is_uint64_type ()) + return make_py_int (value.uint64_scalar_value ().value ()); - else if (value.is_complex_type ()) - return make_py_complex (value.complex_value ()); - else if (value.is_float_type ()) - return make_py_float (value.double_value ()); - } + else if (value.is_complex_type ()) + return make_py_complex (value.complex_value ()); + else if (value.is_float_type ()) + return make_py_float (value.double_value ()); + } - throw value_convert_exception ("unhandled scalar type"); - return 0; -} + throw value_convert_exception ("unhandled scalar type"); + return 0; + } -PyObject * -make_py_array (const octave_value& value) -{ - if (value.is_numeric_type () && ! value.is_complex_type () - && value.ndims () == 2 && (value.columns () <= 1 || value.rows () <= 1)) - { - if (value.is_double_type ()) - return make_py_array (value.array_value ()); - else if (value.is_single_type ()) - return make_py_array (value.float_array_value ()); + PyObject * + make_py_array (const octave_value& value) + { + if (value.is_numeric_type () && ! value.is_complex_type () + && value.ndims () == 2 && (value.columns () <= 1 || value.rows () <= 1)) + { + if (value.is_double_type ()) + return make_py_array (value.array_value ()); + else if (value.is_single_type ()) + return make_py_array (value.float_array_value ()); - else if (value.is_int8_type ()) - return make_py_array (value.int8_array_value ()); - else if (value.is_int16_type ()) - return make_py_array (value.int16_array_value ()); - else if (value.is_int32_type ()) - return make_py_array (value.int32_array_value ()); - else if (value.is_int64_type ()) - return make_py_array (value.int64_array_value ()); + else if (value.is_int8_type ()) + return make_py_array (value.int8_array_value ()); + else if (value.is_int16_type ()) + return make_py_array (value.int16_array_value ()); + else if (value.is_int32_type ()) + return make_py_array (value.int32_array_value ()); + else if (value.is_int64_type ()) + return make_py_array (value.int64_array_value ()); - else if (value.is_uint8_type ()) - return make_py_array (value.uint8_array_value ()); - else if (value.is_uint16_type ()) - return make_py_array (value.uint16_array_value ()); - else if (value.is_uint32_type ()) - return make_py_array (value.uint32_array_value ()); - else if (value.is_uint64_type ()) - return make_py_array (value.uint64_array_value ()); - } + else if (value.is_uint8_type ()) + return make_py_array (value.uint8_array_value ()); + else if (value.is_uint16_type ()) + return make_py_array (value.uint16_array_value ()); + else if (value.is_uint32_type ()) + return make_py_array (value.uint32_array_value ()); + else if (value.is_uint64_type ()) + return make_py_array (value.uint64_array_value ()); + } - throw value_convert_exception ("unhandled Octave numeric vector type"); - return 0; -} + throw value_convert_exception ("unhandled Octave numeric vector type"); + return 0; + } -inline PyObject * -wrap_octvalue_to_pyobj (const octave_value& value) -{ - boost::python::object obj; - octvalue_to_pyobj (obj, value); - PyObject *ptr = obj.ptr (); - Py_INCREF (ptr); - return ptr; -} + inline PyObject * + wrap_octvalue_to_pyobj (const octave_value& value) + { + boost::python::object obj; + octvalue_to_pyobj (obj, value); + PyObject *ptr = obj.ptr (); + Py_INCREF (ptr); + return ptr; + } -inline octave_value -wrap_pyobj_to_octvalue (PyObject *obj) -{ - boost::python::object objref { boost::python::handle<> (boost::python::borrowed (obj)) }; - octave_value value; - pyobj_to_octvalue (value, objref); - return value; -} + inline octave_value + wrap_pyobj_to_octvalue (PyObject *obj) + { + boost::python::object objref + { boost::python::handle<> (boost::python::borrowed (obj)) }; + octave_value value; + pyobj_to_octvalue (value, objref); + return value; + } -octave_scalar_map -extract_py_scalar_map (PyObject *obj) -{ - if (! obj) - throw object_convert_exception ("failed to extract map: null object"); + octave_scalar_map + extract_py_scalar_map (PyObject *obj) + { + if (! obj) + throw object_convert_exception ("failed to extract map: null object"); - if (! PyDict_Check (obj)) - throw object_convert_exception ("failed to extract map: wrong type"); + if (! PyDict_Check (obj)) + throw object_convert_exception ("failed to extract map: wrong type"); - octave_scalar_map map; + octave_scalar_map map; - Py_ssize_t pos = 0; - PyObject *py_key = 0; - PyObject *py_value = 0; + Py_ssize_t pos = 0; + PyObject *py_key = 0; + PyObject *py_value = 0; - while (PyDict_Next (obj, &pos, &py_key, &py_value)) - { - if (! PyBytes_Check (py_key) && ! PyUnicode_Check (py_key)) - throw object_convert_exception ("failed to extract map: bad key type"); + while (PyDict_Next (obj, &pos, &py_key, &py_value)) + { + if (! PyBytes_Check (py_key) && ! PyUnicode_Check (py_key)) + throw object_convert_exception + ("failed to extract map: bad key type"); - std::string key = extract_py_str (py_key); - octave_value value = wrap_pyobj_to_octvalue (py_value); - map.setfield (key, value); - } + std::string key = extract_py_str (py_key); + octave_value value = wrap_pyobj_to_octvalue (py_value); + map.setfield (key, value); + } - return map; -} + return map; + } -PyObject * -make_py_dict (const octave_scalar_map& map) -{ - PyObject *dict = PyDict_New (); - if (! dict) - octave_throw_bad_alloc (); + PyObject * + make_py_dict (const octave_scalar_map& map) + { + PyObject *dict = PyDict_New (); + if (! dict) + octave_throw_bad_alloc (); - for (auto p = map.begin (); p != map.end (); ++p) - { - PyObject *key = make_py_str (map.key (p)); - if (! key) - octave_throw_bad_alloc (); + for (auto p = map.begin (); p != map.end (); ++p) + { + PyObject *key = make_py_str (map.key (p)); + if (! key) + octave_throw_bad_alloc (); - PyObject *item = wrap_octvalue_to_pyobj (map.contents (p)); + PyObject *item = wrap_octvalue_to_pyobj (map.contents (p)); - if (PyDict_SetItem (dict, key, item) < 0) - throw boost::python::error_already_set (); - } + if (PyDict_SetItem (dict, key, item) < 0) + throw boost::python::error_already_set (); + } - return dict; -} + return dict; + } -int64_t -extract_py_int64 (PyObject *obj) -{ - if (! obj) - throw object_convert_exception ("failed to extract integer: null object"); + int64_t + extract_py_int64 (PyObject *obj) + { + if (! obj) + throw object_convert_exception ("failed to extract integer: null object"); - if (PyLong_Check (obj)) - { - int overflow = 0; + if (PyLong_Check (obj)) + { + int overflow = 0; #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8)) - PY_LONG_LONG value = PyLong_AsLongLongAndOverflow (obj, &overflow); + PY_LONG_LONG value = PyLong_AsLongLongAndOverflow (obj, &overflow); #else - long value = PyLong_AsLongAndOverflow (obj, &overflow); + long value = PyLong_AsLongAndOverflow (obj, &overflow); #endif - if (overflow) - if (overflow > 0) - value = std::numeric_limits::max (); - else - value = std::numeric_limits::min (); - return static_cast (value); - } + if (overflow) + if (overflow > 0) + value = std::numeric_limits::max (); + else + value = std::numeric_limits::min (); + return static_cast (value); + } #if PY_VERSION_HEX < 0x03000000 - else if (PyInt_Check (obj)) - return PyInt_AsLong (obj); + else if (PyInt_Check (obj)) + return PyInt_AsLong (obj); #endif - else - throw object_convert_exception ("failed to extract integer: wrong type"); + else + throw object_convert_exception ("failed to extract integer: wrong type"); - return 0; -} + return 0; + } -uint64_t -extract_py_uint64 (PyObject *obj) -{ - if (! obj) - throw object_convert_exception ("failed to extract integer: null object"); + uint64_t + extract_py_uint64 (PyObject *obj) + { + if (! obj) + throw object_convert_exception ("failed to extract integer: null object"); - if (PyLong_Check (obj)) - { - // FIXME: if (value < 0), may be very implementation dependent - if (Py_SIZE (obj) < 0) - return 0; + if (PyLong_Check (obj)) + { + // FIXME: if (value < 0), may be very implementation dependent + if (Py_SIZE (obj) < 0) + return 0; #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8)) - unsigned PY_LONG_LONG value = PyLong_AsUnsignedLongLong (obj); - bool overflow = (value == static_cast (-1)); + unsigned PY_LONG_LONG value = PyLong_AsUnsignedLongLong (obj); + bool overflow = (value == static_cast (-1)); #else - unsigned long value = PyLong_AsUnsignedLong (obj); - bool overflow = (value == static_cast (-1)); + unsigned long value = PyLong_AsUnsignedLong (obj); + bool overflow = (value == static_cast (-1)); #endif - if (overflow) - { - value = std::numeric_limits::max (); - PyErr_Clear (); - } + if (overflow) + { + value = std::numeric_limits::max (); + PyErr_Clear (); + } - return static_cast (value); - } + return static_cast (value); + } #if PY_VERSION_HEX < 0x03000000 - else if (PyInt_Check (obj)) - return static_cast (PyInt_AsLong (obj)); + else if (PyInt_Check (obj)) + return static_cast (PyInt_AsLong (obj)); #endif - else - throw object_convert_exception ("failed to extract integer: wrong type"); + else + throw object_convert_exception ("failed to extract integer: wrong type"); - return 0; -} + return 0; + } -PyObject * -make_py_tuple (const Cell& cell) -{ - if (! (cell.is_empty () || cell.is_vector ())) - throw value_convert_exception ( - "unable to convert multidimensional cell array into Python tuple"); + PyObject * + make_py_tuple (const Cell& cell) + { + if (! (cell.is_empty () || cell.is_vector ())) + throw value_convert_exception ( + "unable to convert multidimensional cell array into Python tuple"); - octave_idx_type size = cell.numel (); - PyObject *tuple = PyTuple_New (size); - if (! tuple) - octave_throw_bad_alloc (); + octave_idx_type size = cell.numel (); + PyObject *tuple = PyTuple_New (size); + if (! tuple) + octave_throw_bad_alloc (); - for (octave_idx_type i = 0; i < size; ++i) - { - PyObject *item = wrap_octvalue_to_pyobj (cell.xelem (i)); - PyTuple_SET_ITEM (tuple, i, item); - } + for (octave_idx_type i = 0; i < size; ++i) + { + PyObject *item = wrap_octvalue_to_pyobj (cell.xelem (i)); + PyTuple_SET_ITEM (tuple, i, item); + } - return tuple; -} + return tuple; + } -std::string -extract_py_str (PyObject *obj) -{ - std::string retval; + std::string + extract_py_str (PyObject *obj) + { + std::string retval; - if (! obj) - throw object_convert_exception ("failed to extract string: null object"); - if (PyBytes_Check (obj)) - { - retval.assign (PyBytes_AsString (obj), PyBytes_Size (obj)); - } - else if (PyUnicode_Check (obj)) - { - bool ok = false; - PyObject *enc = PyUnicode_AsUTF8String (obj); - if (enc) - { - if (PyBytes_Check (enc)) - { - ok = true; - retval.assign (PyBytes_AsString (enc), PyBytes_Size (enc)); - } - Py_DECREF (enc); - } - if (! ok) - throw object_convert_exception ("failed to extract string: UTF-8 error"); - } - else - throw object_convert_exception ("failed to extract string: wrong type"); + if (! obj) + throw object_convert_exception ("failed to extract string: null object"); + if (PyBytes_Check (obj)) + { + retval.assign (PyBytes_AsString (obj), PyBytes_Size (obj)); + } + else if (PyUnicode_Check (obj)) + { + bool ok = false; + PyObject *enc = PyUnicode_AsUTF8String (obj); + if (enc) + { + if (PyBytes_Check (enc)) + { + ok = true; + retval.assign (PyBytes_AsString (enc), PyBytes_Size (enc)); + } + Py_DECREF (enc); + } + if (! ok) + throw object_convert_exception + ("failed to extract string: UTF-8 error"); + } + else + throw object_convert_exception ("failed to extract string: wrong type"); - return retval; -} + return retval; + } -PyObject * -make_py_str (const std::string& str) -{ + PyObject * + make_py_str (const std::string& str) + { #if PY_VERSION_HEX >= 0x03000000 - return PyUnicode_FromStringAndSize (str.data (), str.size ()); + return PyUnicode_FromStringAndSize (str.data (), str.size ()); #else - return PyString_FromStringAndSize (str.data (), str.size ()); + return PyString_FromStringAndSize (str.data (), str.size ()); #endif -} + } } diff -r 3a64a336d214 -r c4b78e449c62 oct-py-types.h --- a/oct-py-types.h Fri Apr 28 14:04:58 2017 -0700 +++ b/oct-py-types.h Fri Apr 28 14:07:57 2017 -0700 @@ -37,183 +37,185 @@ namespace pytave { -//! Extract the integer value of the given Python bool object. -//! -//! @param obj Python bool object -//! @return @c true or @c false value of @a obj -bool -extract_py_bool (PyObject *obj); + //! Extract the integer value of the given Python bool object. + //! + //! @param obj Python bool object + //! @return @c true or @c false value of @a obj + bool + extract_py_bool (PyObject *obj); -//! Create a Python bool object with the value of the given @c bool value. -//! -//! @param value @c true or @c false value -//! @return Python bool object -PyObject * -make_py_bool (bool value); + //! Create a Python bool object with the value of the given @c bool value. + //! + //! @param value @c true or @c false value + //! @return Python bool object + PyObject * + make_py_bool (bool value); -//! Extract the complex value of the given Python complex object. -//! -//! @param obj Python complex object -//! @return complex value of @a obj -std::complex -extract_py_complex (PyObject *obj); + //! Extract the complex value of the given Python complex object. + //! + //! @param obj Python complex object + //! @return complex value of @a obj + std::complex + extract_py_complex (PyObject *obj); -//! Create a Python complex object with the value of the given @c complex value. -//! -//! @param value complex value -//! @return Python complex object -PyObject * -make_py_complex (std::complex value); + //! Create a Python complex object with the value of the given @c complex + //! value. + //! + //! @param value complex value + //! @return Python complex object + PyObject * + make_py_complex (std::complex value); -//! Extract the floating point value of the given Python float object. -//! -//! @param obj Python float object -//! @return floating point value of @a obj -double -extract_py_float (PyObject *obj); + //! Extract the floating point value of the given Python float object. + //! + //! @param obj Python float object + //! @return floating point value of @a obj + double + extract_py_float (PyObject *obj); -//! Create a Python float object with the value of the given @c double value. -//! -//! @param value floating point value -//! @return Python float object -PyObject * -make_py_float (double value); + //! Create a Python float object with the value of the given @c double value. + //! + //! @param value floating point value + //! @return Python float object + PyObject * + make_py_float (double value); -//! Extract an Octave scalar map from the given Python dict object. -//! -//! @param obj Python dict object -//! @return Octave scalar map containing the items of @a obj -octave_scalar_map -extract_py_scalar_map (PyObject *obj); + //! Extract an Octave scalar map from the given Python dict object. + //! + //! @param obj Python dict object + //! @return Octave scalar map containing the items of @a obj + octave_scalar_map + extract_py_scalar_map (PyObject *obj); -//! Create a Python dict object from the given Octave scalar map value. -//! -//! The values contained in the map are recursively converted to appropriate -//! Python values. -//! -//! @param map Octave scalar map -//! @return Python dict object -PyObject * -make_py_dict (const octave_scalar_map& map); + //! Create a Python dict object from the given Octave scalar map value. + //! + //! The values contained in the map are recursively converted to appropriate + //! Python values. + //! + //! @param map Octave scalar map + //! @return Python dict object + PyObject * + make_py_dict (const octave_scalar_map& map); -//! Extract the integer value of the given Python int or long object. -//! -//! @param obj Python int or long object -//! @return integer value of @a obj -int64_t -extract_py_int64 (PyObject *obj); + //! Extract the integer value of the given Python int or long object. + //! + //! @param obj Python int or long object + //! @return integer value of @a obj + int64_t + extract_py_int64 (PyObject *obj); -//! Extract the integer value of the given Python int or long object. -//! -//! @param obj Python int or long object -//! @return integer value of @a obj -uint64_t -extract_py_uint64 (PyObject *obj); + //! Extract the integer value of the given Python int or long object. + //! + //! @param obj Python int or long object + //! @return integer value of @a obj + uint64_t + extract_py_uint64 (PyObject *obj); -//! Create a Python int object with the value of the given @c int32_t value. -//! -//! @param value integer value -//! @return Python int or long object -PyObject * -make_py_int (int32_t value); + //! Create a Python int object with the value of the given @c int32_t value. + //! + //! @param value integer value + //! @return Python int or long object + PyObject * + make_py_int (int32_t value); -//! Create a Python int object with the value of the given @c uint32_t value. -//! -//! @param value integer value -//! @return Python int or long object -PyObject * -make_py_int (uint32_t value); + //! Create a Python int object with the value of the given @c uint32_t value. + //! + //! @param value integer value + //! @return Python int or long object + PyObject * + make_py_int (uint32_t value); -//! Create a Python int object with the value of the given @c int64_t value. -//! -//! @param value integer value -//! @return Python int or long object -PyObject * -make_py_int (int64_t value); + //! Create a Python int object with the value of the given @c int64_t value. + //! + //! @param value integer value + //! @return Python int or long object + PyObject * + make_py_int (int64_t value); -//! Create a Python int object with the value of the given @c uint64_t value. -//! -//! @param value integer value -//! @return Python int or long object -PyObject * -make_py_int (uint64_t value); + //! Create a Python int object with the value of the given @c uint64_t value. + //! + //! @param value integer value + //! @return Python int or long object + PyObject * + make_py_int (uint64_t value); -//! Create a Python array object with the value of the given Octave array. -//! -//! @param nda array value -//! @return Python array object -PyObject * -make_py_array (const NDArray& nda); + //! Create a Python array object with the value of the given Octave array. + //! + //! @param nda array value + //! @return Python array object + PyObject * + make_py_array (const NDArray& nda); -//! Create a Python array object with the value of the given Octave array. -//! -//! @param nda array value -//! @return Python array object -PyObject * -make_py_array (const FloatNDArray& nda); + //! Create a Python array object with the value of the given Octave array. + //! + //! @param nda array value + //! @return Python array object + PyObject * + make_py_array (const FloatNDArray& nda); -//! Create a Python array object with the value of the given Octave array. -//! -//! @param nda array value -//! @return Python array object -template -PyObject * -make_py_array (const intNDArray& nda); + //! Create a Python array object with the value of the given Octave array. + //! + //! @param nda array value + //! @return Python array object + template + PyObject * + make_py_array (const intNDArray& nda); -//! Create a Python array object from the given Octave numeric vector. -//! -//! All Octave real floating point and integer values are converted to -//! corresponding Python array types by this function. -//! -//! @warning Depending on the version of Python and how it is configured, -//! @c int64 and @c uint64 vectors may not be supported. -//! -//! @param value Octave numeric or boolean scalar value -//! @return Python array object -PyObject * -make_py_array (const octave_value& value); + //! Create a Python array object from the given Octave numeric vector. + //! + //! All Octave real floating point and integer values are converted to + //! corresponding Python array types by this function. + //! + //! @warning Depending on the version of Python and how it is configured, + //! @c int64 and @c uint64 vectors may not be supported. + //! + //! @param value Octave numeric or boolean scalar value + //! @return Python array object + PyObject * + make_py_array (const octave_value& value); -//! Create a Python tuple object from the given Octave cell array value. -//! -//! The values contained in the cell array are recursively converted to -//! appropriate Python values. -//! -//! @param cell Octave cell array -//! @return Python tuple object -PyObject * -make_py_tuple (const Cell& cell); + //! Create a Python tuple object from the given Octave cell array value. + //! + //! The values contained in the cell array are recursively converted to + //! appropriate Python values. + //! + //! @param cell Octave cell array + //! @return Python tuple object + PyObject * + make_py_tuple (const Cell& cell); -//! Create a Python numeric object from the given Octave numeric or boolean -//! scalar value. -//! -//! The following implicit type conversions are implemented by this function: -//! -//! @arg @c bool from Octave logical scalar, -//! @arg @c complex from Octave double or single precision complex scalar, -//! @arg @c float from Octave double or single precision scalar, -//! @arg @c int from any Octave integer-valued scalar, -//! @arg @c long from any Octave @c uint32, @c int64, or @c uint64, and only -//! if running against Python 2. -//! -//! @param value Octave numeric or boolean scalar value -//! @return Python numeric object (@c bool, @c int, @c long, @c float, or -//! @c complex) -PyObject * -make_py_numeric_value (const octave_value& value); + //! Create a Python numeric object from the given Octave numeric or boolean + //! scalar value. + //! + //! The following implicit type conversions are implemented by this function: + //! + //! @arg @c bool from Octave logical scalar, + //! @arg @c complex from Octave double or single precision complex scalar, + //! @arg @c float from Octave double or single precision scalar, + //! @arg @c int from any Octave integer-valued scalar, + //! @arg @c long from any Octave @c uint32, @c int64, or @c uint64, and only + //! if running against Python 2. + //! + //! @param value Octave numeric or boolean scalar value + //! @return Python numeric object (@c bool, @c int, @c long, @c float, or + //! @c complex) + PyObject * + make_py_numeric_value (const octave_value& value); -//! Extract the string value of the given Python str, bytes, or unicode object. -//! -//! @param obj Python str, bytes, or unicode object -//! @return string value of @a obj -std::string -extract_py_str (PyObject *obj); + //! Extract the string value of the given Python str, bytes, or unicode + //! object. + //! + //! @param obj Python str, bytes, or unicode object + //! @return string value of @a obj + std::string + extract_py_str (PyObject *obj); -//! Create a Python str object from the given @c string value. -//! -//! @param str string value -//! @return Python str object -PyObject * -make_py_str (const std::string& str); + //! Create a Python str object from the given @c string value. + //! + //! @param str string value + //! @return Python str object + PyObject * + make_py_str (const std::string& str); } diff -r 3a64a336d214 -r c4b78e449c62 oct-py-util.cc --- a/oct-py-util.cc Fri Apr 28 14:04:58 2017 -0700 +++ b/oct-py-util.cc Fri Apr 28 14:07:57 2017 -0700 @@ -37,224 +37,224 @@ namespace pytave { -inline std::string -py_builtins_module_name () -{ + inline std::string + py_builtins_module_name () + { #if PY_VERSION_HEX >= 0x03000000 - return "builtins"; + return "builtins"; #else - return "__builtin__"; + return "__builtin__"; #endif -} + } -PyObject * -py_builtins_module () -{ - return py_import_module (py_builtins_module_name ()); -} + PyObject * + py_builtins_module () + { + return py_import_module (py_builtins_module_name ()); + } -PyObject * -py_find_function (PyObject *module, const std::string& name) -{ - if (module && PyModule_Check (module)) - { - PyObject *obj = PyObject_GetAttrString (module, name.c_str ()); - if (obj && ! PyCallable_Check (obj)) - { - Py_CLEAR (obj); - } + PyObject * + py_find_function (PyObject *module, const std::string& name) + { + if (module && PyModule_Check (module)) + { + PyObject *obj = PyObject_GetAttrString (module, name.c_str ()); + if (obj && ! PyCallable_Check (obj)) + { + Py_CLEAR (obj); + } - return obj; - } + return obj; + } - return 0; -} + return 0; + } -PyObject * -py_find_function (const std::string& module, const std::string& name) -{ - python_object mod = py_import_module (module); - PyObject *func = py_find_function (mod, name); - return func; -} + PyObject * + py_find_function (const std::string& module, const std::string& name) + { + python_object mod = py_import_module (module); + PyObject *func = py_find_function (mod, name); + return func; + } -PyObject * -py_find_function (const std::string& name) -{ - std::string::size_type idx = name.rfind ("."); - if (idx == std::string::npos) - { - PyObject *func = py_find_function ("__main__", name); - if (! func) - func = py_find_function (py_builtins_module (), name); - return func; - } - else - { - std::string module = name.substr (0, idx); - std::string function = name.substr (idx + 1); - return py_find_function (module, function); - } -} + PyObject * + py_find_function (const std::string& name) + { + std::string::size_type idx = name.rfind ("."); + if (idx == std::string::npos) + { + PyObject *func = py_find_function ("__main__", name); + if (! func) + func = py_find_function (py_builtins_module (), name); + return func; + } + else + { + std::string module = name.substr (0, idx); + std::string function = name.substr (idx + 1); + return py_find_function (module, function); + } + } -PyObject * -py_find_type (const std::string& name) -{ - python_object obj = py_find_function (name); - if (obj && PyType_Check (obj)) - return obj.release (); + PyObject * + py_find_type (const std::string& name) + { + python_object obj = py_find_function (name); + if (obj && PyType_Check (obj)) + return obj.release (); - return 0; -} + return 0; + } -PyObject * -py_import_module (const std::string& name) -{ - return PyImport_ImportModule (name.c_str ()); -} + PyObject * + py_import_module (const std::string& name) + { + return PyImport_ImportModule (name.c_str ()); + } -bool -py_isinstance (PyObject *obj, PyObject *type) -{ - if (obj && type) - return static_cast (PyObject_IsInstance (obj, type)); + bool + py_isinstance (PyObject *obj, PyObject *type) + { + if (obj && type) + return static_cast (PyObject_IsInstance (obj, type)); - return false; -} + return false; + } -std::string -py_object_class_name (PyObject *obj) -{ - std::string retval; + std::string + py_object_class_name (PyObject *obj) + { + std::string retval; - python_object type = obj ? PyObject_GetAttrString (obj, "__class__") : 0; - if (type) - { - python_object mod = PyObject_GetAttrString (type, "__module__"); + python_object type = obj ? PyObject_GetAttrString (obj, "__class__") : 0; + if (type) + { + python_object mod = PyObject_GetAttrString (type, "__module__"); - python_object name; - if (PyObject_HasAttrString (type, "__qualname__")) - name = PyObject_GetAttrString (type, "__qualname__"); - else - name = PyObject_GetAttrString (type, "__name__"); + python_object name; + if (PyObject_HasAttrString (type, "__qualname__")) + name = PyObject_GetAttrString (type, "__qualname__"); + else + name = PyObject_GetAttrString (type, "__name__"); - std::string mod_str = !mod.is_none () ? extract_py_str (mod) : ""; - std::string name_str = name ? extract_py_str (name) : ""; + std::string mod_str = !mod.is_none () ? extract_py_str (mod) : ""; + std::string name_str = name ? extract_py_str (name) : ""; - if (mod_str.empty () || mod_str == py_builtins_module_name ()) - retval = name_str; - else - retval = mod_str + "." + name_str; - } + if (mod_str.empty () || mod_str == py_builtins_module_name ()) + retval = name_str; + else + retval = mod_str + "." + name_str; + } - return retval; -} + return retval; + } -// FIXME: could make this into a class/singleton wrapper a la Octave core -PyObject *objstore = 0; + // FIXME: could make this into a class/singleton wrapper a la Octave core + PyObject *objstore = 0; -inline PyObject * -py_objstore () -{ - if (! objstore) - { - python_object main = py_import_module ("__main__"); - python_object ns = main ? PyObject_GetAttrString (main, "__dict__") : 0; - PyObject *dict = ns ? PyDict_GetItemString (ns, "_in_octave") : 0; + inline PyObject * + py_objstore () + { + if (! objstore) + { + python_object main = py_import_module ("__main__"); + python_object ns = main ? PyObject_GetAttrString (main, "__dict__") : 0; + PyObject *dict = ns ? PyDict_GetItemString (ns, "_in_octave") : 0; - if (dict) - Py_INCREF (dict); + if (dict) + Py_INCREF (dict); - if (! dict) - { - dict = PyDict_New (); - if (dict && ns) - PyDict_SetItemString (ns, "_in_octave", dict); - } + if (! dict) + { + dict = PyDict_New (); + if (dict && ns) + PyDict_SetItemString (ns, "_in_octave", dict); + } - if (! dict) - throw boost::python::error_already_set (); + if (! dict) + throw boost::python::error_already_set (); - objstore = dict; - } - return objstore; -} + objstore = dict; + } + return objstore; + } -void -py_objstore_del (uint64_t key) -{ - python_object store = py_objstore (); - python_object key_obj = make_py_int (key); - python_object key_fmt = PyNumber_ToBase (key_obj, 16); - PyDict_DelItem (store, key_fmt); - store.release (); -} + void + py_objstore_del (uint64_t key) + { + python_object store = py_objstore (); + python_object key_obj = make_py_int (key); + python_object key_fmt = PyNumber_ToBase (key_obj, 16); + PyDict_DelItem (store, key_fmt); + store.release (); + } -PyObject * -py_objstore_get (uint64_t key) -{ - python_object store = py_objstore (); - python_object key_obj = make_py_int (key); - python_object key_fmt = PyNumber_ToBase (key_obj, 16); - PyObject *obj = PyDict_GetItem (store, key_fmt); - store.release (); - if (obj) - Py_INCREF (obj); - return obj; -} + PyObject * + py_objstore_get (uint64_t key) + { + python_object store = py_objstore (); + python_object key_obj = make_py_int (key); + python_object key_fmt = PyNumber_ToBase (key_obj, 16); + PyObject *obj = PyDict_GetItem (store, key_fmt); + store.release (); + if (obj) + Py_INCREF (obj); + return obj; + } -uint64_t -py_objstore_put (PyObject *obj) -{ - python_object store = py_objstore (); - uint64_t key = reinterpret_cast (obj); - python_object key_obj = make_py_int (key); - python_object key_fmt = PyNumber_ToBase (key_obj, 16); - PyDict_SetItem (store, key_fmt, obj); - store.release (); - return key; -} + uint64_t + py_objstore_put (PyObject *obj) + { + python_object store = py_objstore (); + uint64_t key = reinterpret_cast (obj); + python_object key_obj = make_py_int (key); + python_object key_fmt = PyNumber_ToBase (key_obj, 16); + PyDict_SetItem (store, key_fmt, obj); + store.release (); + return key; + } -octave_value -pyobject_wrap_object (PyObject *obj) -{ - uint64_t key = py_objstore_put (obj); - octave_value_list out = feval ("pyobject", ovl (0, octave_uint64 (key)), 1); - return out(0); -} + octave_value + pyobject_wrap_object (PyObject *obj) + { + uint64_t key = py_objstore_put (obj); + octave_value_list out = feval ("pyobject", ovl (0, octave_uint64 (key)), 1); + return out(0); + } -PyObject * -pyobject_unwrap_object (const octave_value& value) -{ - if (value.is_object () && value.class_name () == "pyobject") - { - octave_value_list out = feval ("id", ovl (value), 1); - uint64_t key = out(0).uint64_scalar_value (); - return py_objstore_get (key); - } + PyObject * + pyobject_unwrap_object (const octave_value& value) + { + if (value.is_object () && value.class_name () == "pyobject") + { + octave_value_list out = feval ("id", ovl (value), 1); + uint64_t key = out(0).uint64_scalar_value (); + return py_objstore_get (key); + } - return 0; -} + return 0; + } -bool -is_py_kwargs_argument (PyObject *obj) -{ - if (obj && py_object_class_name (obj) == "__main__._OctaveKwargs" - && PyObject_HasAttrString (obj, "is_kwargs_argument")) - { - PyObject *flag = PyObject_GetAttrString (obj, "is_kwargs_argument"); - if (flag && PyBool_Check (flag) && PyObject_IsTrue (flag)) - return true; - } - return false; -} + bool + is_py_kwargs_argument (PyObject *obj) + { + if (obj && py_object_class_name (obj) == "__main__._OctaveKwargs" + && PyObject_HasAttrString (obj, "is_kwargs_argument")) + { + PyObject *flag = PyObject_GetAttrString (obj, "is_kwargs_argument"); + if (flag && PyBool_Check (flag) && PyObject_IsTrue (flag)) + return true; + } + return false; + } -PyObject * -update_py_dict (PyObject *dict_orig, PyObject *dict_new) -{ - PyObject *dict = dict_orig ? dict_orig : PyDict_New (); - PyDict_Update (dict, dict_new); - return dict; -} + PyObject * + update_py_dict (PyObject *dict_orig, PyObject *dict_new) + { + PyObject *dict = dict_orig ? dict_orig : PyDict_New (); + PyDict_Update (dict, dict_new); + return dict; + } } diff -r 3a64a336d214 -r c4b78e449c62 oct-py-util.h --- a/oct-py-util.h Fri Apr 28 14:04:58 2017 -0700 +++ b/oct-py-util.h Fri Apr 28 14:07:57 2017 -0700 @@ -21,7 +21,7 @@ */ #if ! defined (pytave_oct_py_util_h) -#define pytave_oct_py_util_h +#define pytave_oct_py_util_h 1 #include #include @@ -32,80 +32,80 @@ namespace pytave { -//! Return a reference to the builtins module. -//! -//! @return reference to the builtins module -PyObject * -py_builtins_module (); + //! Return a reference to the builtins module. + //! + //! @return reference to the builtins module + PyObject * + py_builtins_module (); -//! Return a reference to the named function in the given module. -//! -//! @param module module to find the function in -//! @param name name of the function -//! @return a reference to the function, or a null pointer -PyObject * -py_find_function (PyObject *module, const std::string& name); + //! Return a reference to the named function in the given module. + //! + //! @param module module to find the function in + //! @param name name of the function + //! @return a reference to the function, or a null pointer + PyObject * + py_find_function (PyObject *module, const std::string& name); -//! Return a reference to the named function in the given module. -//! -//! @param module name of the module to find the function in -//! @param name name of the function -//! @return a reference to the function, or a null pointer -PyObject * -py_find_function (const std::string& module, const std::string& name); + //! Return a reference to the named function in the given module. + //! + //! @param module name of the module to find the function in + //! @param name name of the function + //! @return a reference to the function, or a null pointer + PyObject * + py_find_function (const std::string& module, const std::string& name); -//! Return a reference to the fully-qualified function name. -//! -//! @param name fully-qualified name of the function -//! @return a reference to the function, or a null pointer -PyObject * -py_find_function (const std::string& name); + //! Return a reference to the fully-qualified function name. + //! + //! @param name fully-qualified name of the function + //! @return a reference to the function, or a null pointer + PyObject * + py_find_function (const std::string& name); -//! Return a reference to the fully-qualified type name. -//! -//! @param name fully-qualified name of the type -//! @return a reference to the type, or a null pointer -PyObject * -py_find_type (const std::string& name); + //! Return a reference to the fully-qualified type name. + //! + //! @param name fully-qualified name of the type + //! @return a reference to the type, or a null pointer + PyObject * + py_find_type (const std::string& name); -//! Return a reference to the named module. -//! -//! @param name fully-qualified name of the module -//! @return a reference to the module, or a null pointer -PyObject * -py_import_module (const std::string& name); + //! Return a reference to the named module. + //! + //! @param name fully-qualified name of the module + //! @return a reference to the module, or a null pointer + PyObject * + py_import_module (const std::string& name); -//! Check whether an object is an instance of a type. -//! -//! @param obj Python object -//! @param type Python type -//! @return @c true if @a obj is an instance of @a type, @c false otherwise -bool -py_isinstance (PyObject *obj, PyObject *type); + //! Check whether an object is an instance of a type. + //! + //! @param obj Python object + //! @param type Python type + //! @return @c true if @a obj is an instance of @a type, @c false otherwise + bool + py_isinstance (PyObject *obj, PyObject *type); -std::string -py_object_class_name (PyObject *obj); + std::string + py_object_class_name (PyObject *obj); -void -py_objstore_del (uint64_t key); + void + py_objstore_del (uint64_t key); -PyObject * -py_objstore_get (uint64_t key); + PyObject * + py_objstore_get (uint64_t key); -uint64_t -py_objstore_put (PyObject *obj); + uint64_t + py_objstore_put (PyObject *obj); -octave_value -pyobject_wrap_object (PyObject *obj); + octave_value + pyobject_wrap_object (PyObject *obj); -PyObject * -pyobject_unwrap_object (const octave_value& value); + PyObject * + pyobject_unwrap_object (const octave_value& value); -bool -is_py_kwargs_argument (PyObject *obj); + bool + is_py_kwargs_argument (PyObject *obj); -PyObject * -update_py_dict (PyObject *dict_orig, PyObject *dict_new); + PyObject * + update_py_dict (PyObject *dict_orig, PyObject *dict_new); }