LCOV - code coverage report
Current view: top level - dns/python - rrttl_python.cc (source / functions) Hit Total Coverage
Test: report.info Lines: 85 90 94.4 %
Date: 2012-05-15 Functions: 9 10 90.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 65 116 56.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (C) 2010  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                 :            : #include <Python.h>
      16                 :            : #include <vector>
      17                 :            : 
      18                 :            : #include <dns/rrttl.h>
      19                 :            : #include <dns/messagerenderer.h>
      20                 :            : #include <util/buffer.h>
      21                 :            : #include <util/python/pycppwrapper_util.h>
      22                 :            : 
      23                 :            : #include "rrttl_python.h"
      24                 :            : #include "pydnspp_common.h"
      25                 :            : #include "messagerenderer_python.h"
      26                 :            : 
      27                 :            : using namespace std;
      28                 :            : using namespace isc::dns;
      29                 :            : using namespace isc::dns::python;
      30                 :            : using namespace isc::util;
      31                 :            : using namespace isc::util::python;
      32                 :            : 
      33                 :            : namespace {
      34                 :            : // The s_* Class simply covers one instantiation of the object
      35                 :            : class s_RRTTL : public PyObject {
      36                 :            : public:
      37                 :            :     s_RRTTL() : cppobj(NULL) {};
      38                 :            :     isc::dns::RRTTL* cppobj;
      39                 :            : };
      40                 :            : 
      41                 :            : typedef CPPPyObjectContainer<s_RRTTL, RRTTL> RRTTLContainer;
      42                 :            : 
      43                 :            : PyObject* RRTTL_toText(s_RRTTL* self);
      44                 :            : // This is a second version of toText, we need one where the argument
      45                 :            : // is a PyObject*, for the str() function in python.
      46                 :            : PyObject* RRTTL_str(PyObject* self);
      47                 :            : PyObject* RRTTL_toWire(s_RRTTL* self, PyObject* args);
      48                 :            : PyObject* RRTTL_getValue(s_RRTTL* self);
      49                 :            : PyObject* RRTTL_richcmp(s_RRTTL* self, s_RRTTL* other, int op);
      50                 :            : 
      51                 :            : // This list contains the actual set of functions we have in
      52                 :            : // python. Each entry has
      53                 :            : // 1. Python method name
      54                 :            : // 2. Our static function here
      55                 :            : // 3. Argument type
      56                 :            : // 4. Documentation
      57                 :            : PyMethodDef RRTTL_methods[] = {
      58                 :            :     { "to_text", reinterpret_cast<PyCFunction>(RRTTL_toText), METH_NOARGS,
      59                 :            :       "Returns the string representation" },
      60                 :            :     { "to_wire", reinterpret_cast<PyCFunction>(RRTTL_toWire), METH_VARARGS,
      61                 :            :       "Converts the RRTTL object to wire format.\n"
      62                 :            :       "The argument can be either a MessageRenderer or an object that "
      63                 :            :       "implements the sequence interface. If the object is mutable "
      64                 :            :       "(for instance a bytearray()), the wire data is added in-place.\n"
      65                 :            :       "If it is not (for instance a bytes() object), a new object is "
      66                 :            :       "returned" },
      67                 :            :     { "get_value", reinterpret_cast<PyCFunction>(RRTTL_getValue), METH_NOARGS,
      68                 :            :       "Returns the TTL as an integer" },
      69                 :            :     { NULL, NULL, 0, NULL }
      70                 :            : };
      71                 :            : 
      72                 :            : int
      73                 :       1060 : RRTTL_init(s_RRTTL* self, PyObject* args) {
      74                 :            :     const char* s;
      75                 :            :     long long i;
      76                 :       1060 :     PyObject* bytes = NULL;
      77                 :            :     // The constructor argument can be a string ("1234"), an integer (1),
      78                 :            :     // or a sequence of numbers between 0 and 255 (wire code)
      79                 :            : 
      80                 :            :     // Note that PyArg_ParseType can set PyError, and we need to clear
      81                 :            :     // that if we try several like here. Otherwise the *next* python
      82                 :            :     // call will suddenly appear to throw an exception.
      83                 :            :     // (the way to do exceptions is to set PyErr and return -1)
      84                 :            :     try {
      85 [ +  - ][ +  + ]:       1060 :         if (PyArg_ParseTuple(args, "s", &s)) {
      86 [ +  - ][ +  - ]:         10 :             self->cppobj = new RRTTL(s);
                 [ +  + ]
      87                 :            :             return (0);
      88 [ +  - ][ +  + ]:       1050 :         } else if (PyArg_ParseTuple(args, "L", &i)) {
      89         [ +  - ]:       1046 :             PyErr_Clear();
      90         [ +  + ]:       1046 :             if (i < 0 || i > 0xffffffff) {
      91         [ +  - ]:          1 :                 PyErr_SetString(PyExc_ValueError, "RR TTL number out of range");
      92                 :            :                 return (-1);
      93                 :            :             }
      94         [ +  - ]:       1045 :             self->cppobj = new RRTTL(i);
      95                 :       1045 :             return (0);
      96 [ +  - ][ +  - ]:          8 :         } else if (PyArg_ParseTuple(args, "O", &bytes) &&
         [ +  + ][ +  + ]
      97         [ +  - ]:          4 :                    PySequence_Check(bytes)) {
      98         [ +  - ]:          2 :             Py_ssize_t size = PySequence_Size(bytes);
      99                 :          2 :             vector<uint8_t> data(size);
     100         [ +  - ]:          2 :             int result = readDataFromSequence(&data[0], size, bytes);
     101         [ +  - ]:          2 :             if (result != 0) {
     102                 :            :                 return (result);
     103                 :            :             }
     104                 :          2 :             InputBuffer ib(&data[0], size);
     105 [ +  - ][ +  + ]:          2 :             self->cppobj = new RRTTL(ib);
     106         [ +  - ]:          1 :             PyErr_Clear();
     107                 :            :             return (0);
     108                 :            :         }
     109                 :          2 :     } catch (const IncompleteRRTTL& icc) {
     110                 :            :         // Ok so one of our functions has thrown a C++ exception.
     111                 :            :         // We need to translate that to a Python Exception
     112                 :            :         // First clear any existing error that was set
     113         [ -  + ]:          1 :         PyErr_Clear();
     114                 :            :         // Now set our own exception
     115         [ -  + ]:          1 :         PyErr_SetString(po_IncompleteRRTTL, icc.what());
     116                 :            :         // And return negative
     117                 :            :         return (-1);
     118      [ -  +  + ]:          5 :     } catch (const InvalidRRTTL& ic) {
     119         [ -  + ]:          2 :         PyErr_Clear();
     120         [ -  + ]:          2 :         PyErr_SetString(po_InvalidRRTTL, ic.what());
     121                 :            :         return (-1);
     122                 :            :     }
     123                 :          2 :     PyErr_Clear();
     124                 :            :     PyErr_SetString(PyExc_TypeError,
     125                 :          2 :                     "no valid type in constructor argument");
     126                 :       1060 :     return (-1);
     127                 :            : }
     128                 :            : 
     129                 :            : void
     130                 :       2081 : RRTTL_destroy(s_RRTTL* self) {
     131                 :       2081 :     delete self->cppobj;
     132                 :       2081 :     self->cppobj = NULL;
     133                 :       2081 :     Py_TYPE(self)->tp_free(self);
     134                 :       2081 : }
     135                 :            : 
     136                 :            : PyObject*
     137                 :          7 : RRTTL_toText(s_RRTTL* self) {
     138                 :            :     // Py_BuildValue makes python objects from native data
     139         [ +  - ]:          7 :     return (Py_BuildValue("s", self->cppobj->toText().c_str()));
     140                 :            : }
     141                 :            : 
     142                 :            : PyObject*
     143                 :          5 : RRTTL_str(PyObject* self) {
     144                 :            :     // Simply call the to_text method we already defined
     145                 :            :     return (PyObject_CallMethod(self,
     146                 :            :                                const_cast<char*>("to_text"),
     147                 :          5 :                                 const_cast<char*>("")));
     148                 :            : }
     149                 :            : 
     150                 :            : PyObject*
     151                 :          4 : RRTTL_toWire(s_RRTTL* self, PyObject* args) {
     152                 :            :     PyObject* bytes;
     153                 :            :     PyObject* mr;
     154                 :            : 
     155 [ +  - ][ +  + ]:          4 :     if (PyArg_ParseTuple(args, "O", &bytes) && PySequence_Check(bytes)) {
                 [ +  + ]
     156                 :          2 :         PyObject* bytes_o = bytes;
     157                 :            : 
     158                 :          2 :         OutputBuffer buffer(4);
     159         [ +  - ]:          2 :         self->cppobj->toWire(buffer);
     160                 :          2 :         PyObject* n = PyBytes_FromStringAndSize(static_cast<const char*>(buffer.getData()),
     161         [ +  - ]:          2 :                                                 buffer.getLength());
     162         [ +  - ]:          2 :         PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
     163                 :            :         // We need to release the object we temporarily created here
     164                 :            :         // to prevent memory leak
     165 [ +  - ][ +  - ]:          2 :         Py_DECREF(n);
     166                 :            :         return (result);
     167         [ +  + ]:          2 :     } else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, &mr)) {
     168                 :          1 :         self->cppobj->toWire(PyMessageRenderer_ToMessageRenderer(mr));
     169                 :            :         // If we return NULL it is seen as an error, so use this for
     170                 :            :         // None returns
     171                 :          1 :         Py_RETURN_NONE;
     172                 :            :     }
     173                 :          1 :     PyErr_Clear();
     174                 :            :     PyErr_SetString(PyExc_TypeError,
     175                 :          1 :                     "toWire argument must be a sequence object or a MessageRenderer");
     176                 :          4 :     return (NULL);
     177                 :            : }
     178                 :            : 
     179                 :            : PyObject*
     180                 :          3 : RRTTL_getValue(s_RRTTL* self) {
     181                 :          3 :     return (Py_BuildValue("I", self->cppobj->getValue()));
     182                 :            : }
     183                 :            : 
     184                 :            : PyObject*
     185                 :        493 : RRTTL_richcmp(s_RRTTL* self, s_RRTTL* other, int op) {
     186                 :        493 :     bool c = false;
     187                 :            : 
     188                 :            :     // Check for null and if the types match. If different type,
     189                 :            :     // simply return False
     190 [ +  - ][ +  + ]:        493 :     if (!other || (self->ob_type != other->ob_type)) {
     191                 :          1 :         Py_RETURN_FALSE;
     192                 :            :     }
     193                 :            : 
     194   [ +  +  +  +  :        492 :     switch (op) {
                +  +  - ]
     195                 :            :     case Py_LT:
     196                 :         20 :         c = *self->cppobj < *other->cppobj;
     197                 :         10 :         break;
     198                 :            :     case Py_LE:
     199                 :          2 :         c = *self->cppobj < *other->cppobj ||
     200 [ -  + ][ #  # ]:          2 :             *self->cppobj == *other->cppobj;
     201                 :          2 :         break;
     202                 :            :     case Py_EQ:
     203                 :        574 :         c = *self->cppobj == *other->cppobj;
     204                 :        287 :         break;
     205                 :            :     case Py_NE:
     206                 :        378 :         c = *self->cppobj != *other->cppobj;
     207                 :        189 :         break;
     208                 :            :     case Py_GT:
     209                 :          4 :         c = *other->cppobj < *self->cppobj;
     210                 :          2 :         break;
     211                 :            :     case Py_GE:
     212                 :          2 :         c = *other->cppobj < *self->cppobj ||
     213 [ +  - ][ +  - ]:          2 :             *self->cppobj == *other->cppobj;
     214                 :          2 :         break;
     215                 :            :     }
     216         [ +  + ]:        492 :     if (c)
     217                 :        285 :         Py_RETURN_TRUE;
     218                 :            :     else
     219                 :        493 :         Py_RETURN_FALSE;
     220                 :            : }
     221                 :            : 
     222                 :            : } // end anonymous namespace
     223                 :            : 
     224                 :            : namespace isc {
     225                 :            : namespace dns {
     226                 :            : namespace python {
     227                 :            : 
     228                 :            : //
     229                 :            : // Declaration of the custom exceptions
     230                 :            : // Initialization and addition of these go in the initModulePart
     231                 :            : // function in pydnspp.cc
     232                 :            : //
     233                 :            : PyObject* po_InvalidRRTTL;
     234                 :            : PyObject* po_IncompleteRRTTL;
     235                 :            : 
     236                 :            : // This defines the complete type for reflection in python and
     237                 :            : // parsing of PyObject* to s_RRTTL
     238                 :            : // Most of the functions are not actually implemented and NULL here.
     239                 :            : PyTypeObject rrttl_type = {
     240                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
     241                 :            :     "pydnspp.RRTTL",
     242                 :            :     sizeof(s_RRTTL),                    // tp_basicsize
     243                 :            :     0,                                  // tp_itemsize
     244                 :            :     (destructor)RRTTL_destroy,          // tp_dealloc
     245                 :            :     NULL,                               // tp_print
     246                 :            :     NULL,                               // tp_getattr
     247                 :            :     NULL,                               // tp_setattr
     248                 :            :     NULL,                               // tp_reserved
     249                 :            :     NULL,                               // tp_repr
     250                 :            :     NULL,                               // tp_as_number
     251                 :            :     NULL,                               // tp_as_sequence
     252                 :            :     NULL,                               // tp_as_mapping
     253                 :            :     NULL,                               // tp_hash
     254                 :            :     NULL,                               // tp_call
     255                 :            :     RRTTL_str,                          // tp_str
     256                 :            :     NULL,                               // tp_getattro
     257                 :            :     NULL,                               // tp_setattro
     258                 :            :     NULL,                               // tp_as_buffer
     259                 :            :     Py_TPFLAGS_DEFAULT,                 // tp_flags
     260                 :            :     "The RRTTL class encapsulates TTLs used in DNS resource records.\n\n"
     261                 :            :     "This is a straightforward class; an RRTTL object simply maintains a "
     262                 :            :     "32-bit unsigned integer corresponding to the TTL value.  The main purpose "
     263                 :            :     "of this class is to provide convenient interfaces to convert a textual "
     264                 :            :     "representation into the integer TTL value and vice versa, and to handle "
     265                 :            :     "wire-format representations.",
     266                 :            :     NULL,                               // tp_traverse
     267                 :            :     NULL,                               // tp_clear
     268                 :            :     (richcmpfunc)RRTTL_richcmp,         // tp_richcompare
     269                 :            :     0,                                  // tp_weaklistoffset
     270                 :            :     NULL,                               // tp_iter
     271                 :            :     NULL,                               // tp_iternext
     272                 :            :     RRTTL_methods,                      // tp_methods
     273                 :            :     NULL,                               // tp_members
     274                 :            :     NULL,                               // tp_getset
     275                 :            :     NULL,                               // tp_base
     276                 :            :     NULL,                               // tp_dict
     277                 :            :     NULL,                               // tp_descr_get
     278                 :            :     NULL,                               // tp_descr_set
     279                 :            :     0,                                  // tp_dictoffset
     280                 :            :     (initproc)RRTTL_init,               // tp_init
     281                 :            :     NULL,                               // tp_alloc
     282                 :            :     PyType_GenericNew,                  // tp_new
     283                 :            :     NULL,                               // tp_free
     284                 :            :     NULL,                               // tp_is_gc
     285                 :            :     NULL,                               // tp_bases
     286                 :            :     NULL,                               // tp_mro
     287                 :            :     NULL,                               // tp_cache
     288                 :            :     NULL,                               // tp_subclasses
     289                 :            :     NULL,                               // tp_weaklist
     290                 :            :     NULL,                               // tp_del
     291                 :            :     0                                   // tp_version_tag
     292                 :            : };
     293                 :            : 
     294                 :            : PyObject*
     295                 :       1021 : createRRTTLObject(const RRTTL& source) {
     296                 :       1021 :     RRTTLContainer container(PyObject_New(s_RRTTL, &rrttl_type));
     297 [ +  - ][ +  - ]:       1021 :     container.set(new RRTTL(source));
     298                 :       1021 :     return (container.release());
     299                 :            : }
     300                 :            : 
     301                 :            : bool
     302                 :          0 : PyRRTTL_Check(PyObject* obj) {
     303         [ #  # ]:          0 :     if (obj == NULL) {
     304 [ #  # ][ #  # ]:          0 :         isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
     305                 :            :     }
     306 [ #  # ][ #  # ]:          0 :     return (PyObject_TypeCheck(obj, &rrttl_type));
     307                 :            : }
     308                 :            : 
     309                 :            : const RRTTL&
     310                 :       1143 : PyRRTTL_ToRRTTL(const PyObject* rrttl_obj) {
     311         [ -  + ]:       1143 :     if (rrttl_obj == NULL) {
     312 [ #  # ][ #  # ]:          0 :         isc_throw(PyCPPWrapperException,
     313                 :            :                   "obj argument NULL in RRTTL PyObject conversion");
     314                 :            :     }
     315                 :       1143 :     const s_RRTTL* rrttl = static_cast<const s_RRTTL*>(rrttl_obj);
     316                 :       1143 :     return (*rrttl->cppobj);
     317                 :            : }
     318                 :            : 
     319                 :            : } // namespace python
     320                 :            : } // namespace dns
     321                 :          6 : } // namespace isc

Generated by: LCOV version 1.9