LCOV - code coverage report
Current view: top level - python/isc/datasrc - iterator_python.cc (source / functions) Hit Total Coverage
Test: report.info Lines: 41 52 78.8 %
Date: 2012-05-15 Functions: 8 8 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 22 56 39.3 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
       2                 :            : //
       3                 :            : // Permission to use, copy, modify, and/or distribute this software for any
       4                 :            : // purpose with or without fee is hereby granted, provided that the above
       5                 :            : // copyright notice and this permission notice appear in all copies.
       6                 :            : //
       7                 :            : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
       8                 :            : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
       9                 :            : // AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
      10                 :            : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
      11                 :            : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
      12                 :            : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
      13                 :            : // PERFORMANCE OF THIS SOFTWARE.
      14                 :            : 
      15                 :            : // Enable this if you use s# variants with PyArg_ParseTuple(), see
      16                 :            : // http://docs.python.org/py3k/c-api/arg.html#strings-and-buffers
      17                 :            : //#define PY_SSIZE_T_CLEAN
      18                 :            : 
      19                 :            : // Python.h needs to be placed at the head of the program file, see:
      20                 :            : // http://docs.python.org/py3k/extending/extending.html#a-simple-example
      21                 :            : #include <Python.h>
      22                 :            : 
      23                 :            : #include <util/python/pycppwrapper_util.h>
      24                 :            : 
      25                 :            : #include <datasrc/client.h>
      26                 :            : #include <datasrc/database.h>
      27                 :            : #include <datasrc/sqlite3_accessor.h>
      28                 :            : #include <datasrc/iterator.h>
      29                 :            : 
      30                 :            : #include <dns/python/name_python.h>
      31                 :            : #include <dns/python/rrset_python.h>
      32                 :            : 
      33                 :            : #include "datasrc.h"
      34                 :            : #include "iterator_python.h"
      35                 :            : 
      36                 :            : #include "iterator_inc.cc"
      37                 :            : 
      38                 :            : using namespace std;
      39                 :            : using namespace isc::util::python;
      40                 :            : using namespace isc::dns::python;
      41                 :            : using namespace isc::datasrc;
      42                 :            : using namespace isc::datasrc::python;
      43                 :            : 
      44                 :            : namespace {
      45                 :            : // The s_* Class simply covers one instantiation of the object
      46                 :            : class s_ZoneIterator : public PyObject {
      47                 :            : public:
      48                 :            :     s_ZoneIterator() : cppobj(ZoneIteratorPtr()), base_obj(NULL) {};
      49                 :            :     ZoneIteratorPtr cppobj;
      50                 :            :     // This is a reference to a base object; if the object of this class
      51                 :            :     // depends on another object to be in scope during its lifetime,
      52                 :            :     // we use INCREF the base object upon creation, and DECREF it at
      53                 :            :     // the end of the destructor
      54                 :            :     // This is an optional argument to createXXX(). If NULL, it is ignored.
      55                 :            :     PyObject* base_obj;
      56                 :            : };
      57                 :            : 
      58                 :            : // Shortcut type which would be convenient for adding class variables safely.
      59                 :            : typedef CPPPyObjectContainer<s_ZoneIterator, ZoneIterator>
      60                 :            :     ZoneIteratorContainer;
      61                 :            : 
      62                 :            : // General creation and destruction
      63                 :            : int
      64                 :          1 : ZoneIterator_init(s_ZoneIterator* self, PyObject* args) {
      65                 :            :     // can't be called directly
      66                 :            :     PyErr_SetString(PyExc_TypeError,
      67                 :          1 :                     "ZoneIterator cannot be constructed directly");
      68                 :            : 
      69                 :          1 :     return (-1);
      70                 :            : }
      71                 :            : 
      72                 :            : void
      73                 :         10 : ZoneIterator_destroy(s_ZoneIterator* const self) {
      74                 :            :     // cppobj is a shared ptr, but to make sure things are not destroyed in
      75                 :            :     // the wrong order, we reset it here.
      76                 :         10 :     self->cppobj.reset();
      77         [ +  + ]:         10 :     if (self->base_obj != NULL) {
      78         [ +  + ]:          9 :         Py_DECREF(self->base_obj);
      79                 :            :     }
      80                 :         10 :     Py_TYPE(self)->tp_free(self);
      81                 :         10 : }
      82                 :            : 
      83                 :            : //
      84                 :            : // We declare the functions here, the definitions are below
      85                 :            : // the type definition of the object, since both can use the other
      86                 :            : //
      87                 :            : PyObject*
      88                 :        261 : ZoneIterator_getNextRRset(PyObject* po_self, PyObject*) {
      89                 :        261 :     s_ZoneIterator* self = static_cast<s_ZoneIterator*>(po_self);
      90         [ -  + ]:        261 :     if (!self->cppobj) {
      91                 :            :         PyErr_SetString(getDataSourceException("Error"),
      92                 :          0 :                         "get_next_rrset() called past end of iterator");
      93                 :          0 :         return (NULL);
      94                 :            :     }
      95                 :            :     try {
      96         [ +  + ]:        261 :         isc::dns::ConstRRsetPtr rrset = self->cppobj->getNextRRset();
      97         [ +  + ]:        259 :         if (!rrset) {
      98                 :          7 :             Py_RETURN_NONE;
      99                 :            :         }
     100         [ +  - ]:        252 :         return (createRRsetObject(*rrset));
     101                 :          4 :     } catch (const isc::Exception& isce) {
     102                 :            :         // isc::Unexpected is thrown when we call getNextRRset() when we are
     103                 :            :         // already done iterating ('iterating past end')
     104                 :            :         // We could also simply return None again
     105 [ -  + ][ -  + ]:          2 :         PyErr_SetString(getDataSourceException("Error"), isce.what());
     106                 :            :         return (NULL);
     107                 :          0 :     } catch (const std::exception& exc) {
     108 [ #  # ][ #  # ]:          0 :         PyErr_SetString(getDataSourceException("Error"), exc.what());
     109                 :            :         return (NULL);
     110      [ +  -  - ]:          2 :     } catch (...) {
     111                 :            :         PyErr_SetString(getDataSourceException("Error"),
     112 [ #  # ][ #  # ]:          0 :                         "Unexpected exception");
     113                 :            :         return (NULL);
     114                 :            :     }
     115                 :            : }
     116                 :            : 
     117                 :            : PyObject*
     118                 :          5 : ZoneIterator_iter(PyObject *self) {
     119                 :          5 :     Py_INCREF(self);
     120                 :          5 :     return (self);
     121                 :            : }
     122                 :            : 
     123                 :            : PyObject*
     124                 :        241 : ZoneIterator_next(PyObject* self) {
     125                 :        241 :     PyObject *result = ZoneIterator_getNextRRset(self, NULL);
     126                 :            :     // iter_next must return NULL without error instead of Py_None
     127         [ +  + ]:        241 :     if (result == Py_None) {
     128         [ -  + ]:        241 :         Py_DECREF(result);
     129                 :            :         return (NULL);
     130                 :            :     } else {
     131                 :            :         return (result);
     132                 :            :     }
     133                 :            : }
     134                 :            : 
     135                 :            : PyObject*
     136                 :          4 : ZoneIterator_getSOA(PyObject* po_self, PyObject*) {
     137                 :          4 :     s_ZoneIterator* self = static_cast<s_ZoneIterator*>(po_self);
     138                 :            :     try {
     139         [ +  - ]:          4 :         isc::dns::ConstRRsetPtr rrset = self->cppobj->getSOA();
     140         [ +  + ]:          4 :         if (!rrset) {
     141                 :          1 :             Py_RETURN_NONE;
     142                 :            :         }
     143         [ +  - ]:          3 :         return (createRRsetObject(*rrset));
     144                 :          0 :     } catch (const isc::Exception& isce) {
     145                 :            :         // isc::Unexpected is thrown when we call getNextRRset() when we are
     146                 :            :         // already done iterating ('iterating past end')
     147                 :            :         // We could also simply return None again
     148 [ #  # ][ #  # ]:          0 :         PyErr_SetString(getDataSourceException("Error"), isce.what());
     149                 :            :         return (NULL);
     150                 :          0 :     } catch (const std::exception& exc) {
     151 [ #  # ][ #  # ]:          0 :         PyErr_SetString(getDataSourceException("Error"), exc.what());
     152                 :            :         return (NULL);
     153      [ #  #  # ]:          0 :     } catch (...) {
     154                 :            :         PyErr_SetString(getDataSourceException("Error"),
     155 [ #  # ][ #  # ]:          0 :                         "Unexpected exception");
     156                 :            :         return (NULL);
     157                 :            :     }
     158                 :            : }
     159                 :            : 
     160                 :            : PyMethodDef ZoneIterator_methods[] = {
     161                 :            :     { "get_next_rrset", ZoneIterator_getNextRRset, METH_NOARGS,
     162                 :            :       ZoneIterator_getNextRRset_doc },
     163                 :            :     { "get_soa", ZoneIterator_getSOA, METH_NOARGS, ZoneIterator_getSOA_doc },
     164                 :            :     { NULL, NULL, 0, NULL }
     165                 :            : };
     166                 :            : 
     167                 :            : 
     168                 :            : } // end of unnamed namespace
     169                 :            : 
     170                 :            : namespace isc {
     171                 :            : namespace datasrc {
     172                 :            : namespace python {
     173                 :            : PyTypeObject zoneiterator_type = {
     174                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
     175                 :            :     "datasrc.ZoneIterator",
     176                 :            :     sizeof(s_ZoneIterator),             // tp_basicsize
     177                 :            :     0,                                  // tp_itemsize
     178                 :            :     reinterpret_cast<destructor>(ZoneIterator_destroy),// tp_dealloc
     179                 :            :     NULL,                               // tp_print
     180                 :            :     NULL,                               // tp_getattr
     181                 :            :     NULL,                               // tp_setattr
     182                 :            :     NULL,                               // tp_reserved
     183                 :            :     NULL,                               // tp_repr
     184                 :            :     NULL,                               // tp_as_number
     185                 :            :     NULL,                               // tp_as_sequence
     186                 :            :     NULL,                               // tp_as_mapping
     187                 :            :     NULL,                               // tp_hash
     188                 :            :     NULL,                               // tp_call
     189                 :            :     NULL,                               // tp_str
     190                 :            :     NULL,                               // tp_getattro
     191                 :            :     NULL,                               // tp_setattro
     192                 :            :     NULL,                               // tp_as_buffer
     193                 :            :     Py_TPFLAGS_DEFAULT,                 // tp_flags
     194                 :            :     ZoneIterator_doc,
     195                 :            :     NULL,                               // tp_traverse
     196                 :            :     NULL,                               // tp_clear
     197                 :            :     NULL,                               // tp_richcompare
     198                 :            :     0,                                  // tp_weaklistoffset
     199                 :            :     ZoneIterator_iter,                  // tp_iter
     200                 :            :     ZoneIterator_next,                  // tp_iternext
     201                 :            :     ZoneIterator_methods,               // tp_methods
     202                 :            :     NULL,                               // tp_members
     203                 :            :     NULL,                               // tp_getset
     204                 :            :     NULL,                               // tp_base
     205                 :            :     NULL,                               // tp_dict
     206                 :            :     NULL,                               // tp_descr_get
     207                 :            :     NULL,                               // tp_descr_set
     208                 :            :     0,                                  // tp_dictoffset
     209                 :            :     reinterpret_cast<initproc>(ZoneIterator_init),// tp_init
     210                 :            :     NULL,                               // tp_alloc
     211                 :            :     PyType_GenericNew,                  // tp_new
     212                 :            :     NULL,                               // tp_free
     213                 :            :     NULL,                               // tp_is_gc
     214                 :            :     NULL,                               // tp_bases
     215                 :            :     NULL,                               // tp_mro
     216                 :            :     NULL,                               // tp_cache
     217                 :            :     NULL,                               // tp_subclasses
     218                 :            :     NULL,                               // tp_weaklist
     219                 :            :     NULL,                               // tp_del
     220                 :            :     0                                   // tp_version_tag
     221                 :            : };
     222                 :            : 
     223                 :            : PyObject*
     224                 :          9 : createZoneIteratorObject(isc::datasrc::ZoneIteratorPtr source,
     225                 :            :                          PyObject* base_obj)
     226                 :            : {
     227                 :            :     s_ZoneIterator* py_zi = static_cast<s_ZoneIterator*>(
     228                 :          9 :         zoneiterator_type.tp_alloc(&zoneiterator_type, 0));
     229         [ +  - ]:          9 :     if (py_zi != NULL) {
     230                 :          9 :         py_zi->cppobj = source;
     231                 :          9 :         py_zi->base_obj = base_obj;
     232         [ +  - ]:          9 :         if (base_obj != NULL) {
     233                 :          9 :             Py_INCREF(base_obj);
     234                 :            :         }
     235                 :            :     }
     236                 :          9 :     return (py_zi);
     237                 :            : }
     238                 :            : 
     239                 :            : } // namespace python
     240                 :            : } // namespace datasrc
     241                 :        104 : } // namespace isc
     242                 :            : 

Generated by: LCOV version 1.9