iterator_python.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // Enable this if you use s# variants with PyArg_ParseTuple(), see
  15. // http://docs.python.org/py3k/c-api/arg.html#strings-and-buffers
  16. //#define PY_SSIZE_T_CLEAN
  17. // Python.h needs to be placed at the head of the program file, see:
  18. // http://docs.python.org/py3k/extending/extending.html#a-simple-example
  19. #include <Python.h>
  20. #include <util/python/pycppwrapper_util.h>
  21. #include <datasrc/client.h>
  22. #include <datasrc/database.h>
  23. #include <datasrc/sqlite3_accessor.h>
  24. #include <datasrc/iterator.h>
  25. #include <dns/python/name_python.h>
  26. #include <dns/python/rrset_python.h>
  27. #include "datasrc.h"
  28. #include "iterator_python.h"
  29. #include "iterator_inc.cc"
  30. using namespace std;
  31. using namespace isc::util::python;
  32. using namespace isc::dns::python;
  33. using namespace isc::datasrc;
  34. using namespace isc::datasrc::python;
  35. namespace {
  36. // The s_* Class simply covers one instantiation of the object
  37. class s_ZoneIterator : public PyObject {
  38. public:
  39. s_ZoneIterator() : cppobj(ZoneIteratorPtr()) {};
  40. ZoneIteratorPtr cppobj;
  41. };
  42. // Shortcut type which would be convenient for adding class variables safely.
  43. typedef CPPPyObjectContainer<s_ZoneIterator, ZoneIterator>
  44. ZoneIteratorContainer;
  45. // General creation and destruction
  46. int
  47. ZoneIterator_init(s_ZoneIterator* self, PyObject* args) {
  48. // can't be called directly
  49. PyErr_SetString(PyExc_TypeError,
  50. "ZoneIterator cannot be constructed directly");
  51. return (-1);
  52. }
  53. // This is a template of typical code logic of python object destructor.
  54. // In many cases you can use it without modification, but check that carefully.
  55. void
  56. ZoneIterator_destroy(s_ZoneIterator* const self) {
  57. // cppobj is a shared ptr, but to make sure things are not destroyed in
  58. // the wrong order, we reset it here.
  59. self->cppobj.reset();
  60. Py_TYPE(self)->tp_free(self);
  61. }
  62. //
  63. // We declare the functions here, the definitions are below
  64. // the type definition of the object, since both can use the other
  65. //
  66. PyObject*
  67. ZoneIterator_getNextRRset(PyObject* po_self, PyObject*) {
  68. s_ZoneIterator* self = static_cast<s_ZoneIterator*>(po_self);
  69. if (!self->cppobj) {
  70. PyErr_SetString(getDataSourceException("Error"),
  71. "get_next_rrset() called past end of iterator");
  72. return (NULL);
  73. }
  74. try {
  75. isc::dns::ConstRRsetPtr rrset = self->cppobj->getNextRRset();
  76. if (!rrset) {
  77. Py_RETURN_NONE;
  78. }
  79. return (createRRsetObject(*rrset));
  80. } catch (const isc::Exception& isce) {
  81. // isc::Unexpected is thrown when we call getNextRRset() when we are
  82. // already done iterating ('iterating past end')
  83. // We could also simply return None again
  84. PyErr_SetString(getDataSourceException("Error"), isce.what());
  85. return (NULL);
  86. } catch (const std::exception& exc) {
  87. PyErr_SetString(getDataSourceException("Error"), exc.what());
  88. return (NULL);
  89. } catch (...) {
  90. PyErr_SetString(getDataSourceException("Error"),
  91. "Unexpected exception");
  92. return (NULL);
  93. }
  94. }
  95. PyObject*
  96. ZoneIterator_iter(PyObject *self) {
  97. Py_INCREF(self);
  98. return (self);
  99. }
  100. PyObject*
  101. ZoneIterator_next(PyObject* self) {
  102. PyObject *result = ZoneIterator_getNextRRset(self, NULL);
  103. // iter_next must return NULL without error instead of Py_None
  104. if (result == Py_None) {
  105. Py_DECREF(result);
  106. return (NULL);
  107. } else {
  108. return (result);
  109. }
  110. }
  111. PyMethodDef ZoneIterator_methods[] = {
  112. { "get_next_rrset",
  113. reinterpret_cast<PyCFunction>(ZoneIterator_getNextRRset), METH_NOARGS,
  114. ZoneIterator_getNextRRset_doc },
  115. { NULL, NULL, 0, NULL }
  116. };
  117. } // end of unnamed namespace
  118. namespace isc {
  119. namespace datasrc {
  120. namespace python {
  121. PyTypeObject zoneiterator_type = {
  122. PyVarObject_HEAD_INIT(NULL, 0)
  123. "datasrc.ZoneIterator",
  124. sizeof(s_ZoneIterator), // tp_basicsize
  125. 0, // tp_itemsize
  126. reinterpret_cast<destructor>(ZoneIterator_destroy),// tp_dealloc
  127. NULL, // tp_print
  128. NULL, // tp_getattr
  129. NULL, // tp_setattr
  130. NULL, // tp_reserved
  131. NULL, // tp_repr
  132. NULL, // tp_as_number
  133. NULL, // tp_as_sequence
  134. NULL, // tp_as_mapping
  135. NULL, // tp_hash
  136. NULL, // tp_call
  137. NULL, // tp_str
  138. NULL, // tp_getattro
  139. NULL, // tp_setattro
  140. NULL, // tp_as_buffer
  141. Py_TPFLAGS_DEFAULT, // tp_flags
  142. ZoneIterator_doc,
  143. NULL, // tp_traverse
  144. NULL, // tp_clear
  145. NULL, // tp_richcompare
  146. 0, // tp_weaklistoffset
  147. ZoneIterator_iter, // tp_iter
  148. ZoneIterator_next, // tp_iternext
  149. ZoneIterator_methods, // tp_methods
  150. NULL, // tp_members
  151. NULL, // tp_getset
  152. NULL, // tp_base
  153. NULL, // tp_dict
  154. NULL, // tp_descr_get
  155. NULL, // tp_descr_set
  156. 0, // tp_dictoffset
  157. reinterpret_cast<initproc>(ZoneIterator_init),// tp_init
  158. NULL, // tp_alloc
  159. PyType_GenericNew, // tp_new
  160. NULL, // tp_free
  161. NULL, // tp_is_gc
  162. NULL, // tp_bases
  163. NULL, // tp_mro
  164. NULL, // tp_cache
  165. NULL, // tp_subclasses
  166. NULL, // tp_weaklist
  167. NULL, // tp_del
  168. 0 // tp_version_tag
  169. };
  170. PyObject*
  171. createZoneIteratorObject(isc::datasrc::ZoneIteratorPtr source) {
  172. s_ZoneIterator* py_zi = static_cast<s_ZoneIterator*>(
  173. zoneiterator_type.tp_alloc(&zoneiterator_type, 0));
  174. if (py_zi != NULL) {
  175. py_zi->cppobj = source;
  176. }
  177. return (py_zi);
  178. }
  179. } // namespace python
  180. } // namespace datasrc
  181. } // namespace isc