pydnspp_common.h 4.9 KB

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