summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/python/pydnspp_common.h
blob: 8e498b3f936049431c25f1d448baf19d171d10d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef LIBDNS_PYTHON_COMMON_H
#define LIBDNS_PYTHON_COMMON_H 1

#include <Python.h>

#include <boost/static_assert.hpp>

#include <climits>  // for CHAR_BIT
#include <stdexcept>
#include <string>

namespace isc {
namespace dns {
namespace python {
// For our 'general' isc::Exceptions
extern PyObject* po_IscException;
extern PyObject* po_InvalidOperation;
extern PyObject* po_InvalidParameter;

// For our own isc::dns::Exception
extern PyObject* po_DNSMessageBADVERS;

// This function reads 'bytes' from a sequence
// This sequence can be anything that implements the Sequence interface,
// but must consist of Numbers between 0 and 255 for every value.
//
// The data is stored in *data.
// Data must be allocated and have at least len bytes available.
//
// The current implementation removes read items from the
// head of the sequence, unless it fails (and returns -1), in which
// case nothing is removed
int readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence);

int addClassVariable(PyTypeObject& c, const char* name, PyObject* obj);

/// \brief Initialize a wrapped class type, and add to module
///
/// The type object is initialized, and its refcount is increased after
/// successful addition to the module.
///
/// \param type The type object to initialize
/// \param name The python name of the class to add
/// \param mod The python module to add the class to
///
/// \return true on success, false on failure
bool initClass(PyTypeObject& type, const char* name, PyObject* mod);

// Short term workaround for unifying the return type of tp_hash
#if PY_MINOR_VERSION < 2
typedef long Py_hash_t;
#endif

/// \brief Convert a hash value of arbitrary type to a Python hash value.
///
/// This templated function is a convenient wrapper to produce a valid hash
/// value of type Py_hash_t, which is expected to be used as the return value
/// of a PyTypeObject.tp_hash implementation.  Py_hash_t is a signed integer
/// the same size as Py_ssize_t.
///
/// The major concern is that -1 means an error in tp_hash and so we need to
/// ensure that this value is never returned.
///
/// If the size of the original hash type is small enough, we convert
/// the original hash value to a value of Py_hash_t, resetting all higher bits
/// to 0.  Due to the assumption on the sizes, the conversion to HashvalType
/// is safe, and (after clearing the higher bits) results in a valid positive
/// value.
///
/// If the size of the input and return types is the same, we clear the
/// highest bit of the original hash so the resulting value will be in a valid
/// positive range of Py_hash_t.  If the original type is unsigned, there's
/// probably no better conversion than this because otherwise the conversion
/// to Py_hash_t could result in an undefined behavior.  If the original type
/// is signed, this conversion reduces the effective value range of the
/// original hash.  If this is not desired, the caller should convert it
/// by itself (note that -1 should be still avoided).
///
/// This function does not support the case where the size of the original
/// hash type is larger than that of Py_hash_t.
template <typename HashvalType>
Py_hash_t
convertToPyHash(HashvalType val) {
    BOOST_STATIC_ASSERT(sizeof(HashvalType) <= sizeof(Py_hash_t));

    // Some versions of g++ doesn't ignore the impossible case of if/else
    // below (depending on the size of HashvalType) and triggers a false
    // warning.
    // To work around it we use an intermediate mutable variable.
    // See Trac #1883 for details.
    size_t hash_val_bits = CHAR_BIT * sizeof(HashvalType);

    if (sizeof(HashvalType) < sizeof(Py_hash_t)) {
        // The original hash type is small enough.  Do trivial conversion.
        const Py_hash_t mask = ~(static_cast<Py_hash_t>(-1) << hash_val_bits);
        return (static_cast<Py_hash_t>(val) & mask);
    } else {
        // Clear the highest bit of the original hash so the conversion is
        // safe and avoids -1.
        HashvalType mask = ~(static_cast<HashvalType>(1) <<
                             (hash_val_bits - 1));
        return (val & mask);
    }
}
} // namespace python
} // namespace dns
} // namespace isc
#endif // LIBDNS_PYTHON_COMMON_H

// Local Variables:
// mode: c++
// End: