libdns_python_common.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // $Id$
  15. #include <Python.h>
  16. int
  17. readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence)
  18. {
  19. PyObject* el = NULL;
  20. for (size_t i = 0; i < len; i++) {
  21. el = PySequence_GetItem(sequence, 0);
  22. if (!el) {
  23. PyErr_SetString(PyExc_TypeError,
  24. "sequence too short");
  25. return -1;
  26. }
  27. if (PyLong_Check(el)) {
  28. long l = PyLong_AsLong(el);
  29. if (l < 0 || l > 255) {
  30. PyErr_SetString(PyExc_TypeError,
  31. "number in fromWire sequence not between 0 and 255");
  32. return -1;
  33. }
  34. data[i] = (uint8_t) PyLong_AsLong(el);
  35. PySequence_DelItem(sequence, 0);
  36. } else {
  37. PyErr_SetString(PyExc_TypeError,
  38. "not a number in fromWire sequence");
  39. return -1;
  40. }
  41. }
  42. return 0;
  43. }
  44. void addClassVariable(PyTypeObject& c, const char* name, PyObject* obj)
  45. {
  46. PyDict_SetItemString(c.tp_dict, name, obj);
  47. }