comparison oct-py-types.cc @ 340:06b8aeea456f

Add functions to conversion API to extract bool, double, and complex values * oct-py-types.cc, oct-py-types.h (pytave::extract_py_bool, pytave::extract_py_complex, pytave::extract_py_float): New functions to extract numeric values from Python numeric types. * python_to_octave.cc (pytave::pyobj_to_octvalue): Use them.
author Mike Miller <mtmiller@octave.org>
date Mon, 15 Aug 2016 21:52:50 -0700
parents d3d355dc44ad
children 6bd8f5e3542a
comparison
equal deleted inserted replaced
339:832ee1f14862 340:06b8aeea456f
55 55
56 PyObject * 56 PyObject *
57 make_py_float (double value) 57 make_py_float (double value)
58 { 58 {
59 return PyFloat_FromDouble (value); 59 return PyFloat_FromDouble (value);
60 }
61
62 bool
63 extract_py_bool (PyObject *obj)
64 {
65 if (! obj)
66 throw object_convert_exception ("failed to extract boolean: null object");
67
68 if (! PyBool_Check (obj))
69 throw object_convert_exception ("failed to extract boolean: wrong type");
70
71 return (obj == Py_True);
72 }
73
74 std::complex<double>
75 extract_py_complex (PyObject *obj)
76 {
77 if (! obj)
78 throw object_convert_exception ("failed to extract complex: null object");
79
80 if (! PyComplex_Check (obj))
81 throw object_convert_exception ("failed to extract complex: wrong type");
82
83 Py_complex value = PyComplex_AsCComplex (obj);
84 return reinterpret_cast<std::complex<double>&> (value);
85 }
86
87 double
88 extract_py_float (PyObject *obj)
89 {
90 if (! obj)
91 throw object_convert_exception ("failed to extract float: null object");
92
93 if (! PyFloat_Check (obj))
94 throw object_convert_exception ("failed to extract float: wrong type");
95
96 return PyFloat_AsDouble (obj);
60 } 97 }
61 98
62 inline PyObject * 99 inline PyObject *
63 make_py_int (int32_t value) 100 make_py_int (int32_t value)
64 { 101 {