LCOV - code coverage report
Current view: top level - python/isc/datasrc - journal_reader_python.cc (source / functions) Hit Total Coverage
Test: report.info Lines: 35 40 87.5 %
Date: 2012-05-15 Functions: 7 7 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 16 36 44.4 %

           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                 :            : // Python.h needs to be placed at the head of the program file, see:
      16                 :            : // http://docs.python.org/py3k/extending/extending.html#a-simple-example
      17                 :            : #include <Python.h>
      18                 :            : 
      19                 :            : #include <util/python/pycppwrapper_util.h>
      20                 :            : 
      21                 :            : #include <datasrc/client.h>
      22                 :            : #include <datasrc/database.h>
      23                 :            : 
      24                 :            : #include <dns/python/rrset_python.h>
      25                 :            : 
      26                 :            : #include "datasrc.h"
      27                 :            : #include "journal_reader_python.h"
      28                 :            : 
      29                 :            : #include "journal_reader_inc.cc"
      30                 :            : 
      31                 :            : using namespace isc::util::python;
      32                 :            : using namespace isc::dns::python;
      33                 :            : using namespace isc::datasrc;
      34                 :            : using namespace isc::datasrc::python;
      35                 :            : 
      36                 :            : namespace {
      37                 :            : // The s_* Class simply covers one instantiation of the object
      38                 :            : class s_ZoneJournalReader : public PyObject {
      39                 :            : public:
      40                 :            :     s_ZoneJournalReader() : cppobj(ZoneJournalReaderPtr()), base_obj(NULL) {};
      41                 :            :     ZoneJournalReaderPtr cppobj;
      42                 :            :     // This is a reference to a base object; if the object of this class
      43                 :            :     // depends on another object to be in scope during its lifetime,
      44                 :            :     // we use INCREF the base object upon creation, and DECREF it at
      45                 :            :     // the end of the destructor
      46                 :            :     // This is an optional argument to createXXX(). If NULL, it is ignored.
      47                 :            :     PyObject* base_obj;
      48                 :            : };
      49                 :            : 
      50                 :            : // General creation and destruction
      51                 :            : int
      52                 :          1 : ZoneJournalReader_init(PyObject*, PyObject*, PyObject*) {
      53                 :            :     // can't be called directly
      54                 :            :     PyErr_SetString(PyExc_TypeError,
      55                 :          1 :                     "ZoneJournalReader cannot be constructed directly");
      56                 :            : 
      57                 :          1 :     return (-1);
      58                 :            : }
      59                 :            : 
      60                 :            : void
      61                 :          5 : ZoneJournalReader_destroy(PyObject* po_self) {
      62                 :            :     s_ZoneJournalReader* const self =
      63                 :          5 :         static_cast<s_ZoneJournalReader*>(po_self) ;
      64                 :            :     // cppobj is a shared ptr, but to make sure things are not destroyed in
      65                 :            :     // the wrong order, we reset it here.
      66                 :          5 :     self->cppobj.reset();
      67         [ +  + ]:          5 :     if (self->base_obj != NULL) {
      68         [ +  + ]:          4 :         Py_DECREF(self->base_obj);
      69                 :            :     }
      70                 :          5 :     Py_TYPE(self)->tp_free(self);
      71                 :          5 : }
      72                 :            : 
      73                 :            : //
      74                 :            : // We declare the functions here, the definitions are below
      75                 :            : // the type definition of the object, since both can use the other
      76                 :            : //
      77                 :            : PyObject*
      78                 :        215 : ZoneJournalReader_getNextDiff(PyObject* po_self, PyObject*) {
      79                 :        215 :     s_ZoneJournalReader* self = static_cast<s_ZoneJournalReader*>(po_self);
      80                 :            :     try {
      81         [ +  + ]:        215 :         isc::dns::ConstRRsetPtr rrset = self->cppobj->getNextDiff();
      82         [ +  + ]:        214 :         if (!rrset) {
      83                 :          3 :             Py_RETURN_NONE;
      84                 :            :         }
      85         [ +  - ]:        211 :         return (createRRsetObject(*rrset));
      86                 :          2 :     } catch (const isc::InvalidOperation& ex) {
      87         [ -  + ]:          1 :         PyErr_SetString(PyExc_ValueError, ex.what());
      88                 :            :         return (NULL);
      89                 :          0 :     } catch (const isc::Exception& isce) {
      90 [ #  # ][ #  # ]:          0 :         PyErr_SetString(getDataSourceException("Error"), isce.what());
      91                 :            :         return (NULL);
      92                 :          0 :     } catch (const std::exception& exc) {
      93 [ #  # ][ #  # ]:          0 :         PyErr_SetString(getDataSourceException("Error"), exc.what());
      94                 :            :         return (NULL);
      95   [ +  -  -  - ]:          1 :     } catch (...) {
      96                 :            :         PyErr_SetString(getDataSourceException("Error"),
      97 [ #  # ][ #  # ]:          0 :                         "Unexpected exception");
      98                 :            :         return (NULL);
      99                 :            :     }
     100                 :            : }
     101                 :            : 
     102                 :            : PyObject*
     103                 :          2 : ZoneJournalReader_iter(PyObject *self) {
     104                 :          2 :     Py_INCREF(self);
     105                 :          2 :     return (self);
     106                 :            : }
     107                 :            : 
     108                 :            : PyObject*
     109                 :        210 : ZoneJournalReader_next(PyObject* self) {
     110                 :        210 :     PyObject* result = ZoneJournalReader_getNextDiff(self, NULL);
     111                 :            :     // iter_next must return NULL without error instead of Py_None
     112         [ +  + ]:        210 :     if (result == Py_None) {
     113         [ -  + ]:        210 :         Py_DECREF(result);
     114                 :            :         return (NULL);
     115                 :            :     } else {
     116                 :            :         return (result);
     117                 :            :     }
     118                 :            : }
     119                 :            : 
     120                 :            : PyMethodDef ZoneJournalReader_methods[] = {
     121                 :            :     { "get_next_diff", ZoneJournalReader_getNextDiff, METH_NOARGS,
     122                 :            :       ZoneJournalReader_getNextDiff_doc },
     123                 :            :     { NULL, NULL, 0, NULL }
     124                 :            : };
     125                 :            : 
     126                 :            : 
     127                 :            : } // end of unnamed namespace
     128                 :            : 
     129                 :            : namespace isc {
     130                 :            : namespace datasrc {
     131                 :            : namespace python {
     132                 :            : PyTypeObject journal_reader_type = {
     133                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
     134                 :            :     "datasrc.ZoneJournalReader",
     135                 :            :     sizeof(s_ZoneJournalReader),             // tp_basicsize
     136                 :            :     0,                                  // tp_itemsize
     137                 :            :     ZoneJournalReader_destroy,          // tp_dealloc
     138                 :            :     NULL,                               // tp_print
     139                 :            :     NULL,                               // tp_getattr
     140                 :            :     NULL,                               // tp_setattr
     141                 :            :     NULL,                               // tp_reserved
     142                 :            :     NULL,                               // tp_repr
     143                 :            :     NULL,                               // tp_as_number
     144                 :            :     NULL,                               // tp_as_sequence
     145                 :            :     NULL,                               // tp_as_mapping
     146                 :            :     NULL,                               // tp_hash
     147                 :            :     NULL,                               // tp_call
     148                 :            :     NULL,                               // tp_str
     149                 :            :     NULL,                               // tp_getattro
     150                 :            :     NULL,                               // tp_setattro
     151                 :            :     NULL,                               // tp_as_buffer
     152                 :            :     Py_TPFLAGS_DEFAULT,                 // tp_flags
     153                 :            :     ZoneJournalReader_doc,
     154                 :            :     NULL,                               // tp_traverse
     155                 :            :     NULL,                               // tp_clear
     156                 :            :     NULL,                               // tp_richcompare
     157                 :            :     0,                                  // tp_weaklistoffset
     158                 :            :     ZoneJournalReader_iter,                  // tp_iter
     159                 :            :     ZoneJournalReader_next,                  // tp_iternext
     160                 :            :     ZoneJournalReader_methods,               // tp_methods
     161                 :            :     NULL,                               // tp_members
     162                 :            :     NULL,                               // tp_getset
     163                 :            :     NULL,                               // tp_base
     164                 :            :     NULL,                               // tp_dict
     165                 :            :     NULL,                               // tp_descr_get
     166                 :            :     NULL,                               // tp_descr_set
     167                 :            :     0,                                  // tp_dictoffset
     168                 :            :     ZoneJournalReader_init,             // tp_init
     169                 :            :     NULL,                               // tp_alloc
     170                 :            :     PyType_GenericNew,                  // tp_new
     171                 :            :     NULL,                               // tp_free
     172                 :            :     NULL,                               // tp_is_gc
     173                 :            :     NULL,                               // tp_bases
     174                 :            :     NULL,                               // tp_mro
     175                 :            :     NULL,                               // tp_cache
     176                 :            :     NULL,                               // tp_subclasses
     177                 :            :     NULL,                               // tp_weaklist
     178                 :            :     NULL,                               // tp_del
     179                 :            :     0                                   // tp_version_tag
     180                 :            : };
     181                 :            : 
     182                 :            : PyObject*
     183                 :          4 : createZoneJournalReaderObject(ZoneJournalReaderPtr source,
     184                 :            :                               PyObject* base_obj)
     185                 :            : {
     186                 :            :     s_ZoneJournalReader* po = static_cast<s_ZoneJournalReader*>(
     187                 :          4 :         journal_reader_type.tp_alloc(&journal_reader_type, 0));
     188         [ +  - ]:          4 :     if (po != NULL) {
     189                 :          4 :         po->cppobj = source;
     190                 :          4 :         po->base_obj = base_obj;
     191         [ +  - ]:          4 :         if (base_obj != NULL) {
     192                 :          4 :             Py_INCREF(base_obj);
     193                 :            :         }
     194                 :            :     }
     195                 :          4 :     return (po);
     196                 :            : }
     197                 :            : 
     198                 :            : } // namespace python
     199                 :            : } // namespace datasrc
     200                 :        104 : } // namespace isc

Generated by: LCOV version 1.9