This file is indexed.

/usr/include/casacore/python/Converters/PycBasicData.h is in casacore-dev 2.2.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//# PycBasicData.h: Convert casa data types to/from python
//# Copyright (C) 2006
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library 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 Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//# $Id: PycBasicData.h,v 1.5 2006/10/25 01:42:13 gvandiep Exp $

#ifndef PYRAP_PYCBASICDATA_H
#define PYRAP_PYCBASICDATA_H

// include python first to avoid _POSIX_C_SOURCE redefined warnings
#include <boost/python.hpp>
#include <boost/python/object.hpp>
#include <casacore/python/Converters/PycArray.h>
#include <casacore/casa/BasicSL/String.h>
#include <casacore/casa/Arrays/IPosition.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/Exceptions/Error.h>
#include <vector>
#include <map>

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif

// Define classes and functions to convert the basic data types and
// containers to and from Python.

namespace casacore { namespace python {

  // Prevent a converter from being registered multiple times.
  class pyregistry
  {
  public:
    static bool get (const std::string& name);
    static void set (const std::string& name);
  private:
    static std::map<std::string,bool> _registry;
  };

  // Check if the given object is a sequence object.
  // If so, return true.
  // py_obj gets changed if the given object was a scalar numpy/numarray.
  // In that case it is flattened.
  bool getSeqObject (boost::python::object& py_obj);

  // Convert a String object to python.
  struct casa_string_to_python_str
  {
    static boost::python::object makeobject(String const& s)
      { return boost::python::object((const std::string&)s); }
    static PyObject* convert(String const& s)
      { return boost::python::incref(makeobject(s).ptr()); }
  };

  // Convert a String object from python.
  struct casa_string_from_python_str
  {
    casa_string_from_python_str()
    {
      boost::python::converter::registry::push_back(
        &convertible,
        &construct,
        boost::python::type_id<String>());
    }

    static void* convertible(PyObject* obj_ptr)
    {
#ifdef IS_PY3K
    if (!PyUnicode_Check(obj_ptr)) return 0;
#else
    if (!PyString_Check(obj_ptr)) return 0;
#endif
      return obj_ptr;
    }

    static void construct(
      PyObject* obj_ptr,
      boost::python::converter::rvalue_from_python_stage1_data* data)
    {
#ifdef IS_PY3K
    PyObject * temp_bytes = PyUnicode_AsEncodedString(obj_ptr, "ASCII", "strict"); // Owned reference
    char* value;
    if (temp_bytes != NULL) {
        value = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
        value = strdup(value);
        Py_DECREF(temp_bytes);
    } else {
      boost::python::throw_error_already_set();
    }
#else
    const char* value = PyString_AsString(obj_ptr);
#endif
      if (value == 0) boost::python::throw_error_already_set();
      void* storage = (
        (boost::python::converter::rvalue_from_python_storage<String>*)
          data)->storage.bytes;
      new (storage) String(value);
      data->convertible = storage;
    }
  };


  // Default operations on all containers for conversion from Python
  // container to C++ one.

  // Copied from
  // scitbx/include/scitbx/boost_python/container_conversions.h that is
  // described in the <a
  // href="http://www.boost.org/libs/python/doc/v2/faq.html">
  // Boost.Python FAQ. </a>

  // @author Ralf W. Grosse-Kunstleve <rwgk@yahoo.com> of
  // <a href="http://www.lbl.gov/">Lawrence Berkeley National Laboratory</a>
  struct default_policy
  {
    static bool check_convertibility_per_element() { return true; }

    template <typename ContainerType>
    static bool check_size(boost::type<ContainerType>, std::size_t)
    {
      return true;
    }

    template <typename ContainerType>
    static void assert_size(boost::type<ContainerType>, std::size_t)
    {}

    template <typename ContainerType>
    static void reserve(ContainerType&, std::size_t)
    {}
  };

  // Operations on containers that have variable capacity for
  // conversion from Python container to C++ one.

  // Copied from
  // scitbx/include/scitbx/boost_python/container_conversions.h that is
  // described in the <a
  // href="http://www.boost.org/libs/python/doc/v2/faq.html">
  // Boost.Python FAQ. </a>

  // @author Ralf W. Grosse-Kunstleve <rwgk@yahoo.com> of
  // <a href="http://www.lbl.gov/">Lawrence Berkeley National Laboratory</a>
  struct stl_variable_capacity_policy : default_policy
  {
    template <typename ContainerType>
    static void reserve(ContainerType& a, std::size_t sz)
    {
      a.reserve(sz);
    }

    template <typename ContainerType, typename ValueType>
    static void set_value(ContainerType& a, std::size_t i, ValueType const& v)
    {
      AlwaysAssert(a.size() == i, AipsError);
      a.push_back(v);
    }
  };

  struct casa_variable_capacity_policy : default_policy
  {
    template <typename ContainerType>
    static void reserve(ContainerType& a, std::size_t sz)
    {
      a.resize(sz);
    }

    template <typename ContainerType, typename ValueType>
    static void set_value(ContainerType& a, std::size_t i, ValueType const& v)
    {
      assert(a.size() > i);
      a[i] = v;
    }
  };

  struct casa_reversed_variable_capacity_policy : default_policy
  {
    template <typename ContainerType>
    static void reserve(ContainerType& a, std::size_t sz)
    {
      a.resize(sz);
    }

    template <typename ContainerType, typename ValueType>
    static void set_value(ContainerType& a, std::size_t i, ValueType const& v)
    {
      assert(a.size() > i);
      a[a.size() - i - 1] = v;
    }
  };


  // A wrapper of a conversion function to convert a STL vector to a
  // Python list.  This class satisfies the requirements of the
  // boost::python::to_python_converter conversion template argument.

  // Copied from
  // scitbx/include/scitbx/boost_python/container_conversions.h that is
  // described in the <a
  // href="http://www.boost.org/libs/python/doc/v2/faq.html">
  // Boost.Python FAQ. </a>

  // @author Ralf W. Grosse-Kunstleve <rwgk@yahoo.com> of 
  // <a href="http://www.lbl.gov/">Lawrence Berkeley National Laboratory</a>
  template < typename ContainerType >
  struct to_list
  {
    // Creates and returns a Python list from the elements copied
    // from a STL container. The ContainerType must be a container
    // with STL iterators defined on it.
    // It may contain any type of object supported by the
    // boost::python::object constructor.
    static boost::python::object makeobject (ContainerType const& c)
    {
      boost::python::list result;
      typename ContainerType::const_iterator i = c.begin();
      typename ContainerType::const_iterator iEnd = c.end();
      for( ; i != iEnd; ++i) {
	result.append(*i);
      }
      return result;
    }
    static PyObject* convert (ContainerType const& c)
    {
      return boost::python::incref(makeobject(c).ptr());
    }
  };
  //# Make specialisations for various types.
  template <>
  struct to_list <casacore::IPosition >
  {
    typedef IPosition ContainerType;
    static boost::python::list makeobject (ContainerType const& c)
    {
      // Reverse IPosition values.
      boost::python::list result;
      for (int i=c.size()-1; i>=0; --i) {
	result.append(c[i]);
      }
      return result;
    }
    static PyObject* convert (ContainerType const& c)
    {
      return boost::python::incref(makeobject(c).ptr());
    }
  };
  //# This specialisation is needed because on OS-X 10.9 clang-3.5 with
  //# Boost-Python 1.57 gives a compile error
  //# /opt/casa/01/include/boost/python/converter/arg_to_python.hpp:209:9:
  //#   error: no matching constructor for initialization of
  //#     'boost::python::converter::detail::arg_to_python_base'
  //#       : arg_to_python_base(&x, registered<T>::converters)
  template <>
  struct to_list <std::vector <bool> >
  {
    typedef std::vector <bool> ContainerType;
    static boost::python::list makeobject (ContainerType const& c)
    {
      boost::python::list result;
      ContainerType::const_iterator i = c.begin();
      ContainerType::const_iterator iEnd = c.end();
      for( ; i != iEnd; ++i) {
        bool b = *i;
	result.append(b);
      }
      return result;
    }
    static PyObject* convert (ContainerType const& c)
    {
      return boost::python::incref(makeobject(c).ptr());
    }
  };
  template <>
  struct to_list <std::vector <casacore::String> >
  {
    typedef std::vector <casacore::String> ContainerType;
    static boost::python::list makeobject (ContainerType const& c)
    {
      boost::python::list result;
      ContainerType::const_iterator i = c.begin();
      ContainerType::const_iterator iEnd = c.end();
      for( ; i != iEnd; ++i) {
	result.append((std::string const&)(*i));
      }
      return result;
    }
    static PyObject* convert (ContainerType const& c)
    {
      return boost::python::incref(makeobject(c).ptr());
    }
  };
  template <>
  struct to_list <casacore::Array <casacore::String> >
  {
    typedef casacore::Array <casacore::String> ContainerType;
    static boost::python::object makeobject (ContainerType const& c)
    {
      boost::python::list result;
      ContainerType::const_iterator i = c.begin();
      ContainerType::const_iterator iEnd = c.end();
      for( ; i != iEnd; ++i) {
	result.append((std::string const&)(*i));
      }
      return result;
    }
    static PyObject* convert (ContainerType const& c)
    {
      return boost::python::incref(makeobject(c).ptr());
    }
  };
  template <>
  struct to_list <casacore::Vector <casacore::String> >
  {
    typedef casacore::Vector <casacore::String> ContainerType;
    static boost::python::object makeobject (ContainerType const& c)
    {
      boost::python::list result;
      ContainerType::const_iterator i = c.begin();
      ContainerType::const_iterator iEnd = c.end();
      for( ; i != iEnd; ++i) {
	result.append((std::string const&)(*i));
      }
      return result;
    }
    static PyObject* convert (ContainerType const& c)
    {
      return boost::python::incref(makeobject(c).ptr());
    }
  };

  // Converts an STL vector or casa Array of T objects to Python list. 
  // Copied from
  // scitbx/include/scitbx/boost_python/container_conversions.h that is
  // described in the <a
  // href="http://www.boost.org/libs/python/doc/v2/faq.html">
  // Boost.Python FAQ. </a>
  // @author Ralf W. Grosse-Kunstleve <rwgk@yahoo.com> of
  // <a href="http://www.lbl.gov/">Lawrence Berkeley National Laboratory</a>
  template < typename T >
  struct std_vector_to_list 
  {
    std_vector_to_list ()
    {
      boost::python::to_python_converter < std::vector < T >, 
	                         to_list < std::vector < T > >  > ();
    }
  };
  template < typename T >
  struct casa_array_to_list 
  {
    casa_array_to_list ()
    {
      boost::python::to_python_converter < casacore::Array < T >, 
	                         to_list < casacore::Array < T > >  > ();
    }
  };
  template < typename T >
  struct casa_vector_to_list 
  {
    casa_vector_to_list ()
    {
      boost::python::to_python_converter < casacore::Vector < T >, 
	                         to_list < casacore::Vector < T > >  > ();
    }
  };
  struct casa_iposition_to_list 
  {
    casa_iposition_to_list ()
    {
      boost::python::to_python_converter < casacore::IPosition, 
	                         to_list < casacore::IPosition >  > ();
    }
  };


  // Conversion of Python sequence to C++ container.

  // Copied from
  // scitbx/include/scitbx/boost_python/container_conversions.h that is
  // described in the <a
  // href="http://www.boost.org/libs/python/doc/v2/faq.html">
  // Boost.Python FAQ. </a>
  // @author Ralf W. Grosse-Kunstleve <rwgk@yahoo.com> of
  // <a href="http://www.lbl.gov/">Lawrence Berkeley National Laboratory</a>
  template <typename ContainerType, typename ConversionPolicy>
  struct from_python_sequence
  {
    typedef typename ContainerType::value_type container_element_type;

    from_python_sequence()
    {
      boost::python::converter::registry::push_back(
        &convertible,
        &construct,
        boost::python::type_id<ContainerType>());
    }

    // Appears to return @a obj_ptr if it is type of Python sequence
    // that can be convertible to C++ container.
    static void* convertible(PyObject* obj_ptr)
    {
      using namespace boost::python;
      handle<> py_hdl(obj_ptr);
      if (PyErr_Occurred()) {
	PyErr_Clear();
	return 0;
      }
      object py_obj(py_hdl);
      incref(obj_ptr);        // incr refcount, because ~object decrements it
      // Accept single values.
      if (PyBool_Check(obj_ptr)
#ifdef IS_PY3K
	  || PyLong_Check(obj_ptr)
#else
      || PyInt_Check(obj_ptr)
#endif
	  || PyLong_Check(obj_ptr)
	  || PyFloat_Check(obj_ptr)
	  || PyComplex_Check(obj_ptr)
#ifdef IS_PY3K
	  || PyUnicode_Check(obj_ptr)) {
#else
      || PyString_Check(obj_ptr)) {
#endif

	extract<container_element_type> elem_proxy(py_obj);
	if (!elem_proxy.check()) return 0;
	return obj_ptr;
      }
      // An array scalar is accepted.
      if (PycArrayScalarCheck(obj_ptr)) {
	return obj_ptr;
      }
      // Get the sequence object.
      // It can be a numarray/numpy scalar in which case
      // it fills py_obj with a flattened array.
      if (! getSeqObject (py_obj)) {
	return 0;
      }
      // Check the sequence.
      // It must be convertible to an iterator.
      handle<> obj_iter(allow_null(PyObject_GetIter(py_obj.ptr())));
      if (!obj_iter.get()) {
	PyErr_Clear();
	return 0;
      }
      if (!check_convertibility (py_obj.ptr())) {
	return 0;
      }
      return obj_ptr;
    }

    // Constructs a C++ container from a Python sequence.
    static void construct(
      PyObject* obj_ptr,
      boost::python::converter::rvalue_from_python_stage1_data* data)
    {
      using namespace boost::python;
      using boost::python::converter::rvalue_from_python_storage;
      void* storage = (
        (rvalue_from_python_storage<ContainerType>*)
          data)->storage.bytes;
      new (storage) ContainerType();
      data->convertible = storage;
      ContainerType& result = *((ContainerType*)storage);
      if (PyBool_Check(obj_ptr)
#ifndef IS_PY3K
	  || PyInt_Check(obj_ptr)
#endif
	  || PyLong_Check(obj_ptr)
	  || PyFloat_Check(obj_ptr)
	  || PyComplex_Check(obj_ptr)
#ifdef IS_PY3K
	  || PyUnicode_Check(obj_ptr)
#else
      || PyString_Check(obj_ptr)
#endif
	  || PycArrayScalarCheck(obj_ptr)) {
	extract<container_element_type> elem_proxy(obj_ptr);
	ConversionPolicy::reserve(result, 1);
        ConversionPolicy::set_value(result, 0, elem_proxy());
	return;
      }
      handle<> py_hdl(obj_ptr);
      object py_obj = object(py_hdl);
      incref(obj_ptr);      // incr refcount, because ~object decrements it
      assert (getSeqObject (py_obj));
      fill_container (result, py_obj.ptr());
      //	ConversionPolicy::reserve(result, 1);
      //	ConversionPolicy::set_value(result, 0, 
      //	    extract<container_element_type>(py_flat.attr("__getitem__")(0)));
    }

    // Constructs a C++ container from a Python sequence.
    static ContainerType make_container(PyObject* obj_ptr)
    {
      ContainerType result;
      fill_container (result, obj_ptr);
      return result;
    }

  private:
    static void fill_container (ContainerType& result, PyObject* obj_ptr)
    {
      using namespace boost::python;
      int obj_size = PyObject_Length(obj_ptr);
      handle<> obj_iter(PyObject_GetIter(obj_ptr));
      ConversionPolicy::reserve(result, obj_size);
      std::size_t i=0;
      for(;;i++) {
        handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
        if (PyErr_Occurred()) throw_error_already_set();
        if (!py_elem_hdl.get()) break; // end of iteration
        object py_elem_obj(py_elem_hdl);
        extract<container_element_type> elem_proxy(py_elem_obj);
        ConversionPolicy::set_value(result, i, elem_proxy());
      }
      ConversionPolicy::assert_size(boost::type<ContainerType>(), i);
    }

    static bool check_convertibility(PyObject* obj_ptr)
    {
      using namespace boost::python;
      handle<> obj_iter(allow_null(PyObject_GetIter(obj_ptr)));
      if (!obj_iter.get()) {       // must be convertible to an iterator
        PyErr_Clear();
        return false;
      }
      int obj_size = PyObject_Length(obj_ptr);
      if (obj_size < 0) {        // must be a measurable sequence
	PyErr_Clear();
	return false;
      }
      if (ConversionPolicy::check_convertibility_per_element()) {
        if (!ConversionPolicy::check_size(
          boost::type<ContainerType>(), obj_size)) return false;
	// All elements in a range and array have the same type, so
	// need to check the first element only.
        bool is_same = PyRange_Check(obj_ptr) ||
	              (PySequence_Check(obj_ptr)
		       && !PyTuple_Check(obj_ptr) && !PyList_Check(obj_ptr));
	int i = 0;
        for (;;i++) {
          handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
          if (PyErr_Occurred()) {
            PyErr_Clear();
            return false;
          }
          if (!py_elem_hdl.get()) break;         // end of iteration
          object py_elem_obj(py_elem_hdl);
          extract<container_element_type> elem_proxy(py_elem_obj);
          if (!elem_proxy.check()) return false;
          if (is_same) break; // all elements are of the same type
        }
        if (!is_same) assert(i == obj_size );
      }
      return true;
    }
  };

  // Register the String conversion.
  struct convert_casa_string
  {
    static void reg();
  };
  inline void register_convert_casa_string()
    { convert_casa_string::reg(); }

  // Register the IPosition conversion.
  struct convert_casa_iposition
  {
    static void reg();
  };
  inline void register_convert_casa_iposition()
    { convert_casa_iposition::reg(); }

  // Register the std::vector conversions.
  template < typename T >
  struct convert_std_vector
  {
    static void reg()
    {
      std::string tname(typeid(std::vector<T>).name());
      if (! pyregistry::get (tname)) {
	pyregistry::set (tname);
	std_vector_to_list < T > ();
	from_python_sequence < std::vector < T >,
	                       stl_variable_capacity_policy > ();
      }
    }
  };
  template < typename T >
  inline void register_convert_std_vector()
    { convert_std_vector<T>::reg(); }

  // Register the casacore::Vector conversions.
  template < typename T >
  struct convert_casa_vector
  {
    static void reg()
    {
      std::string tname(typeid(casacore::Vector<T>).name());
      if (! pyregistry::get (tname)) {
	pyregistry::set (tname);
	casa_array_to_list < T > ();
	casa_vector_to_list < T > ();
	from_python_sequence < casacore::Vector < T >,
	                       casa_variable_capacity_policy > ();
      }
    }
  };
  template < typename T >
  inline void register_convert_casa_vector()
    { convert_casa_vector<T>::reg(); }


  // Register all standard basic conversions.
  // These are String, IPosition, Vector<String>, Vector<Int>,
  // Vector<Double>, Vector<DComplex>.
  void register_convert_basicdata();
}}

#endif