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))
return 0;
100 boost::python::converter::rvalue_from_python_stage1_data* data)
103 PyObject * temp_bytes = PyUnicode_AsEncodedString(obj_ptr,
"ASCII",
"strict");
105 if (temp_bytes != NULL) {
106 value = PyBytes_AS_STRING(temp_bytes);
107 value = strdup(value);
108 Py_DECREF(temp_bytes);
110 boost::python::throw_error_already_set();
113 const char* value = PyString_AsString(obj_ptr);
115 if (value == 0) boost::python::throw_error_already_set();
117 (boost::python::converter::rvalue_from_python_storage<String>*)
118 data)->storage.bytes;
119 new (storage)
String(value);
120 data->convertible = storage;
140 template <
typename ContainerType>
141 static bool check_size(boost::type<ContainerType>, std::size_t)
146 template <
typename ContainerType>
150 template <
typename ContainerType>
151 static void reserve(ContainerType&, std::size_t)
168 template <
typename ContainerType>
169 static void reserve(ContainerType& a, std::size_t sz)
174 template <
typename ContainerType,
typename ValueType>
175 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
184 template <
typename ContainerType>
185 static void reserve(ContainerType& a, std::size_t sz)
190 template <
typename ContainerType,
typename ValueType>
191 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
193 assert(a.size() > i);
200 template <
typename ContainerType>
201 static void reserve(ContainerType& a, std::size_t sz)
206 template <
typename ContainerType,
typename ValueType>
207 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
209 assert(a.size() > i);
210 a[a.size() - i - 1] = v;
227 template <
typename ContainerType >
237 boost::python::list result;
238 typename ContainerType::const_iterator i = c.begin();
239 typename ContainerType::const_iterator iEnd = c.end();
240 for( ; i != iEnd; ++i) {
247 return boost::python::incref(makeobject(c).ptr());
258 boost::python::list result;
259 for (
int i=c.
size()-1; i>=0; --i) {
266 return boost::python::incref(makeobject(c).ptr());
281 boost::python::list result;
282 ContainerType::const_iterator i = c.begin();
283 ContainerType::const_iterator iEnd = c.end();
284 for( ; i != iEnd; ++i) {
292 return boost::python::incref(makeobject(c).ptr());
301 boost::python::list result;
302 ContainerType::const_iterator i = c.begin();
303 ContainerType::const_iterator iEnd = c.end();
304 for( ; i != iEnd; ++i) {
305 result.append((std::string
const&)(*i));
311 return boost::python::incref(makeobject(c).ptr());
320 boost::python::list result;
323 for( ; i != iEnd; ++i) {
324 result.append((std::string
const&)(*i));
330 return boost::python::incref(makeobject(c).ptr());
339 boost::python::list result;
342 for( ; i != iEnd; ++i) {
343 result.append((std::string
const&)(*i));
349 return boost::python::incref(makeobject(c).ptr());
361 template <
typename T >
366 boost::python::to_python_converter < std::vector < T >,
370 template <
typename T >
375 boost::python::to_python_converter < casacore::Array < T >,
379 template <
typename T >
384 boost::python::to_python_converter < casacore::Vector < T >,
407 template <
typename ContainerType,
typename ConversionPolicy>
414 boost::python::converter::registry::push_back(
417 boost::python::type_id<ContainerType>());
424 using namespace boost::python;
425 handle<> py_hdl(obj_ptr);
426 if (PyErr_Occurred()) {
430 object py_obj(py_hdl);
433 if (PyBool_Check(obj_ptr)
435 || PyLong_Check(obj_ptr)
437 || PyInt_Check(obj_ptr)
439 || PyLong_Check(obj_ptr)
440 || PyFloat_Check(obj_ptr)
441 || PyComplex_Check(obj_ptr)
443 || PyUnicode_Check(obj_ptr)) {
445 || PyString_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()) {
469 if (!check_convertibility (py_obj.ptr())) {
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)
490 || PyInt_Check(obj_ptr)
492 || PyLong_Check(obj_ptr)
493 || PyFloat_Check(obj_ptr)
494 || PyComplex_Check(obj_ptr)
496 || PyUnicode_Check(obj_ptr)
498 || PyString_Check(obj_ptr)
501 extract<container_element_type> elem_proxy(obj_ptr);
502 ConversionPolicy::reserve(result, 1);
503 ConversionPolicy::set_value(result, 0, elem_proxy());
506 handle<> py_hdl(obj_ptr);
507 object py_obj = object(py_hdl);
510 fill_container (result, py_obj.ptr());
519 ContainerType result;
520 fill_container (result, obj_ptr);
527 using namespace boost::python;
528 int obj_size = PyObject_Length(obj_ptr);
529 handle<> obj_iter(PyObject_GetIter(obj_ptr));
530 ConversionPolicy::reserve(result, obj_size);
533 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
534 if (PyErr_Occurred()) throw_error_already_set();
535 if (!py_elem_hdl.get())
break;
536 object py_elem_obj(py_elem_hdl);
537 extract<container_element_type> elem_proxy(py_elem_obj);
538 ConversionPolicy::set_value(result, i, elem_proxy());
540 ConversionPolicy::assert_size(boost::type<ContainerType>(), i);
545 using namespace boost::python;
546 handle<> obj_iter(allow_null(PyObject_GetIter(obj_ptr)));
547 if (!obj_iter.get()) {
551 int obj_size = PyObject_Length(obj_ptr);
556 if (ConversionPolicy::check_convertibility_per_element()) {
557 if (!ConversionPolicy::check_size(
558 boost::type<ContainerType>(), obj_size))
return false;
561 bool is_same = PyRange_Check(obj_ptr) ||
562 (PySequence_Check(obj_ptr)
563 && !PyTuple_Check(obj_ptr) && !PyList_Check(obj_ptr));
566 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
567 if (PyErr_Occurred()) {
571 if (!py_elem_hdl.get())
break;
572 object py_elem_obj(py_elem_hdl);
573 extract<container_element_type> elem_proxy(py_elem_obj);
574 if (!elem_proxy.check())
return false;
577 if (!is_same) assert(i == obj_size );
600 template <
typename T >
605 std::string tname(
typeid(std::vector<T>).name());
614 template <
typename T >
619 template <
typename T >
634 template <
typename T >
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
static PyObject * convert(ContainerType const &c)
A Vector of integers, for indexing into Array<T> objects.
Register the casacore::Vector conversions.
A 1-D Specialization of the Array class.
void register_convert_casa_string()
static bool check_size(boost::type< ContainerType >, std::size_t)
Convert a String object to python.
static boost::python::list makeobject(ContainerType const &c)
static boost::python::object makeobject(String const &s)
void register_convert_basicdata()
Register all standard basic conversions.
static boost::python::list makeobject(ContainerType const &c)
Define real & complex conjugation for non-complex types and put comparisons into std namespace...
casa_string_from_python_str()
iterator begin()
Get the begin iterator object for any array.
static boost::python::object makeobject(ContainerType const &c)
static void assert_size(boost::type< ContainerType >, std::size_t)
std::vector< bool > ContainerType
void register_convert_std_vector()
Register the String conversion.
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Constructs a C++ container from a Python sequence.
Register the std::vector conversions.
static void * convertible(PyObject *obj_ptr)
Appears to return obj_ptr if it is type of Python sequence that can be convertible to C++ container...
bool getSeqObject(boost::python::object &py_obj)
Check if the given object is a sequence object.
static void reserve(ContainerType &a, std::size_t sz)
static boost::python::object makeobject(ContainerType const &c)
Creates and returns a Python list from the elements copied from a STL container.
static void fill_container(ContainerType &result, PyObject *obj_ptr)
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
static void reserve(ContainerType &a, std::size_t sz)
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
Bool PycArrayScalarCheck(PyObject *obj_ptr)
Check if the PyObject is an array scalar object.
Converts an STL vector or casa Array of T objects to Python list.
void register_convert_casa_iposition()
static bool check_convertibility(PyObject *obj_ptr)
void register_convert_casa_vector()
static PyObject * convert(ContainerType const &c)
template <class T, class U> class vector;
static boost::python::object makeobject(ContainerType const &c)
static std::map< std::string, bool > _registry
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Register the IPosition conversion.
Operations on containers that have variable capacity for conversion from Python container to C++ one...
Base class for all Casacore library errors.
static void * convertible(PyObject *obj_ptr)
static ContainerType make_container(PyObject *obj_ptr)
Constructs a C++ container from a Python sequence.
const Double c
Fundamental physical constants (SI units):
Prevent a converter from being registered multiple times.
A wrapper of a conversion function to convert a STL vector to a Python list.
String: the storage and methods of handling collections of characters.
Default operations on all containers for conversion from Python container to C++ one.
static void reserve(ContainerType &, std::size_t)
static PyObject * convert(ContainerType const &c)
static PyObject * convert(ContainerType const &c)
static void set(const std::string &name)
casacore::Vector< casacore::String > ContainerType
static PyObject * convert(String const &s)
static bool get(const std::string &name)
static PyObject * convert(ContainerType const &c)
ContainerType::value_type container_element_type
static boost::python::list makeobject(ContainerType const &c)
static bool check_convertibility_per_element()
Conversion of Python sequence to C++ container.
static void reserve(ContainerType &a, std::size_t sz)
this file contains all the compiler specific defines
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
casacore::Array< casacore::String > ContainerType
Convert a String object from python.
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
std::vector< casacore::String > ContainerType
static PyObject * convert(ContainerType const &c)
ConstIteratorSTL const_iterator