zonetable_accessor_python.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (C) 2013 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 <datasrc/zone_table_accessor.h>
  21. #include "datasrc.h"
  22. #include "zonetable_accessor_python.h"
  23. #include "zonetable_iterator_python.h"
  24. using namespace std;
  25. using namespace isc::datasrc;
  26. using namespace isc::datasrc::python;
  27. namespace {
  28. // The s_* Class simply covers one instantiation of the object
  29. class s_ZoneTableAccessor : public PyObject {
  30. public:
  31. s_ZoneTableAccessor() :
  32. cppobj(ConstZoneTableAccessorPtr()),
  33. base_obj(NULL)
  34. {}
  35. ConstZoneTableAccessorPtr cppobj;
  36. // This is a reference to a base object; if the object of this class
  37. // depends on another object to be in scope during its lifetime,
  38. // we use INCREF the base object upon creation, and DECREF it at
  39. // the end of the destructor
  40. // This is an optional argument to createXXX(). If NULL, it is ignored.
  41. PyObject* base_obj;
  42. };
  43. int
  44. ZoneTableAccessor_init(PyObject*, PyObject*, PyObject*) {
  45. // can't be called directly
  46. PyErr_SetString(PyExc_TypeError,
  47. "ZoneTableAccessor cannot be constructed directly");
  48. return (-1);
  49. }
  50. void
  51. ZoneTableAccessor_destroy(PyObject* po_self) {
  52. s_ZoneTableAccessor* const self =
  53. static_cast<s_ZoneTableAccessor*>(po_self);
  54. // cppobj is a shared ptr, but to make sure things are not destroyed in
  55. // the wrong order, we reset it here.
  56. self->cppobj.reset();
  57. if (self->base_obj != NULL) {
  58. Py_DECREF(self->base_obj);
  59. }
  60. Py_TYPE(self)->tp_free(self);
  61. }
  62. PyObject*
  63. ZoneTableAccessor_iter(PyObject* po_self) {
  64. s_ZoneTableAccessor* const self =
  65. static_cast<s_ZoneTableAccessor*>(po_self);
  66. try {
  67. return (createZoneTableIteratorObject(self->cppobj->getIterator(),
  68. po_self));
  69. } catch (const std::exception& exc) {
  70. PyErr_SetString(getDataSourceException("Error"), exc.what());
  71. return (NULL);
  72. } catch (...) {
  73. PyErr_SetString(getDataSourceException("Error"),
  74. "Unexpected exception");
  75. return (NULL);
  76. }
  77. }
  78. // This list contains the actual set of functions we have in
  79. // python. Each entry has
  80. // 1. Python method name
  81. // 2. Our static function here
  82. // 3. Argument type
  83. // 4. Documentation
  84. PyMethodDef ZoneTableAccessor_methods[] = {
  85. { NULL, NULL, 0, NULL }
  86. };
  87. const char* const ZoneTableAccessor_doc = "\
  88. An accessor to a zone table for a data source.\n\
  89. \n\
  90. This class object is intended to be used by applications that load zones\
  91. into memory, so that the application can get a list of zones to be loaded.";
  92. } // end anonymous namespace
  93. namespace isc {
  94. namespace datasrc {
  95. namespace python {
  96. // This defines the complete type for reflection in python and
  97. // parsing of PyObject* to s_ZoneTableAccessor
  98. // Most of the functions are not actually implemented and NULL here.
  99. PyTypeObject zonetableaccessor_type = {
  100. PyVarObject_HEAD_INIT(NULL, 0)
  101. "datasrc.ZoneTableAccessor",
  102. sizeof(s_ZoneTableAccessor), // tp_basicsize
  103. 0, // tp_itemsize
  104. ZoneTableAccessor_destroy, // tp_dealloc
  105. NULL, // tp_print
  106. NULL, // tp_getattr
  107. NULL, // tp_setattr
  108. NULL, // tp_reserved
  109. NULL, // tp_repr
  110. NULL, // tp_as_number
  111. NULL, // tp_as_sequence
  112. NULL, // tp_as_mapping
  113. NULL, // tp_hash
  114. NULL, // tp_call
  115. NULL, // tp_str
  116. NULL, // tp_getattro
  117. NULL, // tp_setattro
  118. NULL, // tp_as_buffer
  119. Py_TPFLAGS_DEFAULT, // tp_flags
  120. ZoneTableAccessor_doc, // tp_doc
  121. NULL, // tp_traverse
  122. NULL, // tp_clear
  123. NULL, // tp_richcompare
  124. 0, // tp_weaklistoffset
  125. ZoneTableAccessor_iter, // tp_iter
  126. NULL, // tp_iternext
  127. ZoneTableAccessor_methods, // tp_methods
  128. NULL, // tp_members
  129. NULL, // tp_getset
  130. NULL, // tp_base
  131. NULL, // tp_dict
  132. NULL, // tp_descr_get
  133. NULL, // tp_descr_set
  134. 0, // tp_dictoffset
  135. ZoneTableAccessor_init, // tp_init
  136. NULL, // tp_alloc
  137. PyType_GenericNew, // tp_new
  138. NULL, // tp_free
  139. NULL, // tp_is_gc
  140. NULL, // tp_bases
  141. NULL, // tp_mro
  142. NULL, // tp_cache
  143. NULL, // tp_subclasses
  144. NULL, // tp_weaklist
  145. NULL, // tp_del
  146. 0 // tp_version_tag
  147. };
  148. PyObject*
  149. createZoneTableAccessorObject(isc::datasrc::ConstZoneTableAccessorPtr source,
  150. PyObject* base_obj)
  151. {
  152. s_ZoneTableAccessor* py_zt = static_cast<s_ZoneTableAccessor*>(
  153. zonetableaccessor_type.tp_alloc(&zonetableaccessor_type, 0));
  154. if (py_zt != NULL) {
  155. py_zt->cppobj = source;
  156. py_zt->base_obj = base_obj;
  157. if (base_obj != NULL) {
  158. Py_INCREF(base_obj);
  159. }
  160. }
  161. return (py_zt);
  162. }
  163. } // namespace python
  164. } // namespace datasrc
  165. } // namespace isc