28 #ifndef PYRAP_PYCBASICDATA_H
29 #define PYRAP_PYCBASICDATA_H
32 #include <boost/python.hpp>
33 #include <boost/python/object.hpp>
34 #include <casacore/python/Converters/PycArray.h>
35 #include <casacore/casa/BasicSL/String.h>
36 #include <casacore/casa/Arrays/IPosition.h>
37 #include <casacore/casa/Arrays/Vector.h>
38 #include <casacore/casa/Utilities/Assert.h>
39 #include <casacore/casa/Exceptions/Error.h>
43 #if PY_MAJOR_VERSION >= 3
50 namespace casacore {
namespace python {
56 static bool get (
const std::string& name);
57 static void set (
const std::string& name);
72 {
return boost::python::object((
const std::string&)s); }
74 {
return boost::python::incref(
makeobject(s).ptr()); }
82 boost::python::converter::registry::push_back(
85 boost::python::type_id<String>());
91 if (!PyUnicode_Check(obj_ptr))
return 0;
93 if (!PyString_Check(obj_ptr) && !PyUnicode_Check(obj_ptr))
return 0;
100 boost::python::converter::rvalue_from_python_stage1_data* data)
103 boost::python::object temp_bytes_obj;
104 if (PyUnicode_Check(obj_ptr)) {
105 PyObject * temp_bytes = PyUnicode_AsEncodedString(obj_ptr,
"UTF-8",
"strict");
107 if (temp_bytes != NULL) {
109 temp_bytes_obj = boost::python::object(boost::python::handle<>(temp_bytes));
110 value = PyBytes_AS_STRING(temp_bytes);
112 boost::python::throw_error_already_set();
115 }
else if (PyString_Check(obj_ptr)) {
116 value = PyString_AsString(obj_ptr);
119 boost::python::throw_error_already_set();
121 if (
value == 0) boost::python::throw_error_already_set();
123 (boost::python::converter::rvalue_from_python_storage<String>*)
124 data)->storage.bytes;
126 data->convertible = storage;
146 template <
typename ContainerType>
147 static bool check_size(boost::type<ContainerType>, std::size_t)
152 template <
typename ContainerType>
156 template <
typename ContainerType>
157 static void reserve(ContainerType&, std::size_t)
174 template <
typename ContainerType>
175 static void reserve(ContainerType& a, std::size_t sz)
180 template <
typename ContainerType,
typename ValueType>
181 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
190 template <
typename ContainerType>
191 static void reserve(ContainerType& a, std::size_t sz)
196 template <
typename ContainerType,
typename ValueType>
197 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
199 assert(a.size() > i);
206 template <
typename ContainerType>
207 static void reserve(ContainerType& a, std::size_t sz)
212 template <
typename ContainerType,
typename ValueType>
213 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
215 assert(a.size() > i);
216 a[a.size() - i - 1] = v;
233 template <
typename ContainerType >
243 boost::python::list result;
244 typename ContainerType::const_iterator i =
c.begin();
245 typename ContainerType::const_iterator iEnd =
c.end();
246 for( ; i != iEnd; ++i) {
264 boost::python::list result;
265 for (
int i=
c.size()-1; i>=0; --i) {
287 boost::python::list result;
288 ContainerType::const_iterator i =
c.begin();
289 ContainerType::const_iterator iEnd =
c.end();
290 for( ; i != iEnd; ++i) {
307 boost::python::list result;
308 ContainerType::const_iterator i =
c.begin();
309 ContainerType::const_iterator iEnd =
c.end();
310 for( ; i != iEnd; ++i) {
311 result.append((std::string
const&)(*i));
326 boost::python::list result;
329 for( ; i != iEnd; ++i) {
330 result.append((std::string
const&)(*i));
345 boost::python::list result;
346 ContainerType::const_iterator i =
c.begin();
347 ContainerType::const_iterator iEnd =
c.end();
348 for( ; i != iEnd; ++i) {
349 result.append((std::string
const&)(*i));
367 template <
typename T >
372 boost::python::to_python_converter < std::vector < T >,
376 template <
typename T >
381 boost::python::to_python_converter < casacore::Array < T >,
385 template <
typename T >
390 boost::python::to_python_converter < casacore::Vector < T >,
413 template <
typename ContainerType,
typename ConversionPolicy>
420 boost::python::converter::registry::push_back(
423 boost::python::type_id<ContainerType>());
430 using namespace boost::python;
431 handle<> py_hdl(obj_ptr);
432 if (PyErr_Occurred()) {
436 object py_obj(py_hdl);
439 if (PyBool_Check(obj_ptr)
440 || PyLong_Check(obj_ptr)
441 || PyFloat_Check(obj_ptr)
442 || PyComplex_Check(obj_ptr)
444 || PyInt_Check(obj_ptr)
445 || PyString_Check(obj_ptr)
447 || PyUnicode_Check(obj_ptr)) {
448 extract<container_element_type> elem_proxy(py_obj);
449 if (!elem_proxy.check())
return 0;
464 handle<> obj_iter(allow_null(PyObject_GetIter(py_obj.ptr())));
465 if (!obj_iter.get()) {
478 boost::python::converter::rvalue_from_python_stage1_data* data)
480 using namespace boost::python;
481 using boost::python::converter::rvalue_from_python_storage;
483 (rvalue_from_python_storage<ContainerType>*)
484 data)->storage.bytes;
485 new (storage) ContainerType();
486 data->convertible = storage;
487 ContainerType& result = *((ContainerType*)storage);
488 if (PyBool_Check(obj_ptr)
489 || PyLong_Check(obj_ptr)
490 || PyFloat_Check(obj_ptr)
491 || PyComplex_Check(obj_ptr)
492 || PyUnicode_Check(obj_ptr)
494 || PyString_Check(obj_ptr)
495 || PyInt_Check(obj_ptr)
498 extract<container_element_type> elem_proxy(obj_ptr);
499 ConversionPolicy::reserve(result, 1);
500 ConversionPolicy::set_value(result, 0, elem_proxy());
503 handle<> py_hdl(obj_ptr);
504 object py_obj = object(py_hdl);
516 ContainerType result;
524 using namespace boost::python;
525 int obj_size = PyObject_Length(obj_ptr);
526 handle<> obj_iter(PyObject_GetIter(obj_ptr));
527 ConversionPolicy::reserve(result, obj_size);
530 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
531 if (PyErr_Occurred()) throw_error_already_set();
532 if (!py_elem_hdl.get())
break;
533 object py_elem_obj(py_elem_hdl);
534 extract<container_element_type> elem_proxy(py_elem_obj);
535 ConversionPolicy::set_value(result, i, elem_proxy());
537 ConversionPolicy::assert_size(boost::type<ContainerType>(), i);
542 using namespace boost::python;
543 handle<> obj_iter(allow_null(PyObject_GetIter(obj_ptr)));
544 if (!obj_iter.get()) {
548 int obj_size = PyObject_Length(obj_ptr);
553 if (ConversionPolicy::check_convertibility_per_element()) {
554 if (!ConversionPolicy::check_size(
555 boost::type<ContainerType>(), obj_size))
return false;
558 bool is_same = PyRange_Check(obj_ptr) ||
559 (PySequence_Check(obj_ptr)
560 && !PyTuple_Check(obj_ptr) && !PyList_Check(obj_ptr));
563 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
564 if (PyErr_Occurred()) {
568 if (!py_elem_hdl.get())
break;
569 object py_elem_obj(py_elem_hdl);
570 extract<container_element_type> elem_proxy(py_elem_obj);
571 if (!elem_proxy.check())
return false;
574 if (!is_same) assert(i == obj_size );
597 template <
typename T >
602 std::string tname(
typeid(std::vector<T>).name());
611 template <
typename T >
616 template <
typename T >
631 template <
typename T >