pydnspp_common.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef __LIBDNS_PYTHON_COMMON_H
  15. #define __LIBDNS_PYTHON_COMMON_H 1
  16. #include <Python.h>
  17. #include <boost/static_assert.hpp>
  18. #include <climits> // for CHAR_BIT
  19. #include <stdexcept>
  20. #include <string>
  21. namespace isc {
  22. namespace dns {
  23. namespace python {
  24. // For our 'general' isc::Exceptions
  25. extern PyObject* po_IscException;
  26. extern PyObject* po_InvalidParameter;
  27. // For our own isc::dns::Exception
  28. extern PyObject* po_DNSMessageBADVERS;
  29. // This function reads 'bytes' from a sequence
  30. // This sequence can be anything that implements the Sequence interface,
  31. // but must consist of Numbers between 0 and 255 for every value.
  32. //
  33. // The data is stored in *data.
  34. // Data must be allocated and have at least len bytes available.
  35. //
  36. // The current implementation removes read items from the
  37. // head of the sequence, unless it fails (and returns -1), in which
  38. // case nothing is removed
  39. int readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence);
  40. int addClassVariable(PyTypeObject& c, const char* name, PyObject* obj);
  41. // Short term workaround for unifying the return type of tp_hash
  42. #if PY_MINOR_VERSION < 2
  43. typedef long Py_hash_t;
  44. #endif
  45. /// \brief Convert a hash value of arbitrary type to a Python hash value.
  46. ///
  47. /// This templated function is a convenient wrapper to produce a valid hash
  48. /// value of type Py_hash_t, which is expected to be used as the return value
  49. /// of a PyTypeObject.tp_hash implementation. Py_hash_t is a signed integer
  50. /// the same size as Py_ssize_t.
  51. ///
  52. /// The major concern is that -1 means an error in tp_hash and so we need to
  53. /// ensure that this value is never returned.
  54. ///
  55. /// If the size of the original hash type is small enough, we convert
  56. /// the original hash value to a value of Py_hash_t, resetting all higher bits
  57. /// to 0. Due to the assumption on the sizes, the conversion to HashvalType
  58. /// is safe, and (after clearing the higher bits) results in a valid positive
  59. /// value.
  60. ///
  61. /// If the size of the input and return types is the same, we clear the
  62. /// highest bit of the original hash so the resulting value will be in a valid
  63. /// positive range of Py_hash_t. If the original type is unsigned, there's
  64. /// probably no better conversion than this because otherwise the conversion
  65. /// to Py_hash_t could result in an undefined behavior. If the original type
  66. /// is signed, this conversion reduces the effective value range of the
  67. /// original hash. If this is not desired, the caller should convert it
  68. /// by itself (note that -1 should be still avoided).
  69. ///
  70. /// This function does not support the case where the size of the original
  71. /// hash type is larger than that of Py_hash_t.
  72. template <typename HashvalType>
  73. Py_hash_t
  74. convertToPyHash(HashvalType val) {
  75. BOOST_STATIC_ASSERT(sizeof(HashvalType) <= sizeof(Py_hash_t));
  76. // Some versions of g++ doesn't ignore the impossible case of if/else
  77. // below (depending on the size of HashvalType) and triggers a false
  78. // warning.
  79. // To work around it we use an intermediate mutable variable.
  80. // See Trac #1883 for details.
  81. size_t hash_val_bits = CHAR_BIT * sizeof(HashvalType);
  82. if (sizeof(HashvalType) < sizeof(Py_hash_t)) {
  83. // The original hash type is small enough. Do trivial conversion.
  84. const Py_hash_t mask = ~(static_cast<Py_hash_t>(-1) << hash_val_bits);
  85. return (static_cast<Py_hash_t>(val) & mask);
  86. } else {
  87. // Clear the highest bit of the original hash so the conversion is
  88. // safe and avoids -1.
  89. HashvalType mask = ~(static_cast<HashvalType>(1) <<
  90. (hash_val_bits - 1));
  91. return (val & mask);
  92. }
  93. }
  94. } // namespace python
  95. } // namespace dns
  96. } // namespace isc
  97. #endif // __LIBDNS_PYTHON_COMMON_H
  98. // Local Variables:
  99. // mode: c++
  100. // End: