common.h 19 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
2
    pybind11/common.h -- Basic macros
Wenzel Jakob's avatar
Wenzel Jakob committed
3

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
5
6
7
8
9

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

10
#pragma once
Wenzel Jakob's avatar
Wenzel Jakob committed
11
12

#if !defined(NAMESPACE_BEGIN)
13
#  define NAMESPACE_BEGIN(name) namespace name {
Wenzel Jakob's avatar
Wenzel Jakob committed
14
15
#endif
#if !defined(NAMESPACE_END)
16
#  define NAMESPACE_END(name) }
Wenzel Jakob's avatar
Wenzel Jakob committed
17
18
#endif

19
#if !defined(PYBIND11_EXPORT)
20
21
22
23
24
#  if defined(WIN32) || defined(_WIN32)
#    define PYBIND11_EXPORT __declspec(dllexport)
#  else
#    define PYBIND11_EXPORT __attribute__ ((visibility("default")))
#  endif
Wenzel Jakob's avatar
Wenzel Jakob committed
25
#endif
26

27
#if defined(_MSC_VER)
28
#  define PYBIND11_NOINLINE __declspec(noinline)
29
#else
30
#  define PYBIND11_NOINLINE __attribute__ ((noinline))
31
32
#endif

33
#define PYBIND11_VERSION_MAJOR 1
34
#define PYBIND11_VERSION_MINOR 9
35
#define PYBIND11_VERSION_PATCH dev0
36

Wenzel Jakob's avatar
Wenzel Jakob committed
37
38
/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode
#if defined(_MSC_VER)
39
40
41
42
#  define HAVE_ROUND
#  pragma warning(push)
#  pragma warning(disable: 4510 4610 4512 4005)
#  if _DEBUG
43
#    define PYBIND11_DEBUG_MARKER
44
45
#    undef _DEBUG
#  endif
Wenzel Jakob's avatar
Wenzel Jakob committed
46
#endif
47

Wenzel Jakob's avatar
Wenzel Jakob committed
48
#include <Python.h>
49
#include <frameobject.h>
50
#include <pythread.h>
51

52
53
54
55
56
#if defined(_WIN32) && (defined(min) || defined(max))
#  error Macro clash with min and max -- define NOMINMAX when compiling your program on Windows
#endif

#if defined(isalnum)
57
58
59
60
61
62
63
#  undef isalnum
#  undef isalpha
#  undef islower
#  undef isspace
#  undef isupper
#  undef tolower
#  undef toupper
64
#endif
65

Wenzel Jakob's avatar
Wenzel Jakob committed
66
#if defined(_MSC_VER)
67
#  if defined(PYBIND11_DEBUG_MARKER)
68
#    define _DEBUG
69
70
#    undef PYBIND11_DEBUG_MARKER
#  endif
71
#  pragma warning(pop)
Wenzel Jakob's avatar
Wenzel Jakob committed
72
73
#endif

74
#include <forward_list>
75
76
77
78
79
80
#include <vector>
#include <string>
#include <stdexcept>
#include <unordered_set>
#include <unordered_map>
#include <memory>
81
#include <typeindex>
82

83
#if PY_MAJOR_VERSION >= 3 /// Compatibility macros for various Python versions
Wenzel Jakob's avatar
Wenzel Jakob committed
84
#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)
85
86
87
88
89
#define PYBIND11_BYTES_CHECK PyBytes_Check
#define PYBIND11_BYTES_FROM_STRING PyBytes_FromString
#define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
#define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize
#define PYBIND11_BYTES_AS_STRING PyBytes_AsString
90
#define PYBIND11_BYTES_CHECK PyBytes_Check
91
92
93
#define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
#define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
#define PYBIND11_LONG_AS_UNSIGNED_LONGLONG(o) PyLong_AsUnsignedLongLong(o)
94
#define PYBIND11_BYTES_NAME "bytes"
95
96
#define PYBIND11_STRING_NAME "str"
#define PYBIND11_SLICE_OBJECT PyObject
97
#define PYBIND11_FROM_STRING PyUnicode_FromString
98
#define PYBIND11_STR_TYPE ::pybind11::str
99
100
101
#define PYBIND11_OB_TYPE(ht_type) (ht_type).ob_base.ob_base.ob_type
#define PYBIND11_PLUGIN_IMPL(name) \
    extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
102
#else
Wenzel Jakob's avatar
Wenzel Jakob committed
103
#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyMethod_New(ptr, nullptr, class_)
104
105
106
107
108
#define PYBIND11_BYTES_CHECK PyString_Check
#define PYBIND11_BYTES_FROM_STRING PyString_FromString
#define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
#define PYBIND11_BYTES_AS_STRING_AND_SIZE PyString_AsStringAndSize
#define PYBIND11_BYTES_AS_STRING PyString_AsString
109
#define PYBIND11_BYTES_CHECK PyString_Check
110
111
112
#define PYBIND11_LONG_CHECK(o) (PyInt_Check(o) || PyLong_Check(o))
#define PYBIND11_LONG_AS_LONGLONG(o) (PyInt_Check(o) ? (long long) PyLong_AsLong(o) : PyLong_AsLongLong(o))
#define PYBIND11_LONG_AS_UNSIGNED_LONGLONG(o) (PyInt_Check(o) ? (unsigned long long) PyLong_AsUnsignedLong(o) : PyLong_AsUnsignedLongLong(o))
113
#define PYBIND11_BYTES_NAME "str"
114
115
#define PYBIND11_STRING_NAME "unicode"
#define PYBIND11_SLICE_OBJECT PySliceObject
116
#define PYBIND11_FROM_STRING PyString_FromString
117
#define PYBIND11_STR_TYPE ::pybind11::bytes
118
119
120
#define PYBIND11_OB_TYPE(ht_type) (ht_type).ob_type
#define PYBIND11_PLUGIN_IMPL(name) \
    extern "C" PYBIND11_EXPORT PyObject *init##name()
121
#endif
122

123
124
125
126
#if PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03050200
extern "C" {
    struct _Py_atomic_address { void *value; };
    PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
127
}
128
129
#endif

130
#define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
131
132
133
134
#define PYBIND11_STRINGIFY(x) #x
#define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x)
#define PYBIND11_INTERNALS_ID "__pybind11_" \
    PYBIND11_TOSTRING(PYBIND11_VERSION_MAJOR) "_" PYBIND11_TOSTRING(PYBIND11_VERSION_MINOR) "__"
135
136
137
138
139
140
141
142
143
144
145
146
147

#define PYBIND11_PLUGIN(name) \
    static PyObject *pybind11_init(); \
    PYBIND11_PLUGIN_IMPL(name) { \
        try { \
            return pybind11_init(); \
        } catch (const std::exception &e) { \
            PyErr_SetString(PyExc_ImportError, e.what()); \
            return nullptr; \
        } \
    } \
    PyObject *pybind11_init()

148
NAMESPACE_BEGIN(pybind11)
Wenzel Jakob's avatar
Wenzel Jakob committed
149
150
151
152

typedef Py_ssize_t ssize_t;

/// Approach used to cast a previously unknown C++ instance into a Python object
153
enum class return_value_policy : uint8_t {
Wenzel Jakob's avatar
Wenzel Jakob committed
154
155
156
157
158
    /** This is the default return value policy, which falls back to the policy
        return_value_policy::take_ownership when the return value is a pointer.
        Otherwise, it uses return_value::move or return_value::copy for rvalue
        and lvalue references, respectively. See below for a description of what
        all of these different policies do. */
Wenzel Jakob's avatar
Wenzel Jakob committed
159
    automatic = 0,
Wenzel Jakob's avatar
Wenzel Jakob committed
160

Wenzel Jakob's avatar
Wenzel Jakob committed
161
    /** As above, but use policy return_value_policy::reference when the return
162
163
164
        value is a pointer. This is the default conversion policy for function
        arguments when calling Python functions manually from C++ code (i.e. via
        handle::operator()). You probably won't need to use this. */
165
    automatic_reference,
Wenzel Jakob's avatar
Wenzel Jakob committed
166

Wenzel Jakob's avatar
Wenzel Jakob committed
167
168
169
170
    /** Reference an existing object (i.e. do not create a new copy) and take
        ownership. Python will call the destructor and delete operator when the
        object’s reference count reaches zero. Undefined behavior ensues when
        the C++ side does the same.. */
Wenzel Jakob's avatar
Wenzel Jakob committed
171
    take_ownership,
Wenzel Jakob's avatar
Wenzel Jakob committed
172

Wenzel Jakob's avatar
Wenzel Jakob committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    /** Create a new copy of the returned object, which will be owned by
        Python. This policy is comparably safe because the lifetimes of the two
        instances are decoupled. */
    copy,

    /** Use std::move to move the return value contents into a new instance
        that will be owned by Python. This policy is comparably safe because the
        lifetimes of the two instances (move source and destination) are
        decoupled. */
    move,

    /** Reference an existing object, but do not take ownership. The C++ side
        is responsible for managing the object’s lifetime and deallocating it
        when it is no longer used. Warning: undefined behavior will ensue when
Wenzel Jakob's avatar
Wenzel Jakob committed
187
188
        the C++ side deletes an object that is still referenced and used by
        Python. */
Wenzel Jakob's avatar
Wenzel Jakob committed
189
    reference,
Wenzel Jakob's avatar
Wenzel Jakob committed
190

Wenzel Jakob's avatar
Wenzel Jakob committed
191
192
193
194
195
196
197
198
199
200
    /** This policy only applies to methods and properties. It references the
        object without taking ownership similar to the above
        return_value_policy::reference policy. In contrast to that policy, the
        function or property’s implicit this argument (called the parent) is
        considered to be the the owner of the return value (the child).
        pybind11 then couples the lifetime of the parent to the child via a
        reference relationship that ensures that the parent cannot be garbage
        collected while Python is still using the child. More advanced
        variations of this scheme are also possible using combinations of
        return_value_policy::reference and the keep_alive call policy */
Wenzel Jakob's avatar
Wenzel Jakob committed
201
    reference_internal
Wenzel Jakob's avatar
Wenzel Jakob committed
202
203
204
205
};

/// Information record describing a Python buffer object
struct buffer_info {
Jason Newton's avatar
Jason Newton committed
206
207
208
    void *ptr = nullptr;         // Pointer to the underlying storage
    size_t itemsize = 0;         // Size of individual items in bytes
    size_t size = 0;             // Total number of entries
209
    std::string format;          // For homogeneous buffers, this should be set to format_descriptor<T>::format()
Jason Newton's avatar
Jason Newton committed
210
    size_t ndim = 0;             // Number of dimensions
211
212
    std::vector<size_t> shape;   // Shape of the tensor (1 entry per dimension)
    std::vector<size_t> strides; // Number of entries between adjacent entries (for each per dimension)
Wenzel Jakob's avatar
Wenzel Jakob committed
213

Wenzel Jakob's avatar
Wenzel Jakob committed
214
    buffer_info() { }
215

216
    buffer_info(void *ptr, size_t itemsize, const std::string &format, size_t ndim,
Wenzel Jakob's avatar
Wenzel Jakob committed
217
                const std::vector<size_t> &shape, const std::vector<size_t> &strides)
218
219
        : ptr(ptr), itemsize(itemsize), size(1), format(format),
          ndim(ndim), shape(shape), strides(strides) {
220
221
        for (size_t i = 0; i < ndim; ++i)
            size *= shape[i];
Wenzel Jakob's avatar
Wenzel Jakob committed
222
    }
223

224
225
226
227
    buffer_info(void *ptr, size_t itemsize, const std::string &format, size_t size)
    : buffer_info(ptr, itemsize, format, 1, std::vector<size_t> { size },
                  std::vector<size_t> { itemsize }) { }

Jason Newton's avatar
Jason Newton committed
228
    buffer_info(Py_buffer *view, bool ownview = true)
229
        : ptr(view->buf), itemsize((size_t) view->itemsize), size(1), format(view->format),
Jason Newton's avatar
Jason Newton committed
230
          ndim((size_t) view->ndim), shape((size_t) view->ndim), strides((size_t) view->ndim), view(view), ownview(ownview) {
231
        for (size_t i = 0; i < (size_t) view->ndim; ++i) {
232
233
234
235
236
237
            shape[i] = (size_t) view->shape[i];
            strides[i] = (size_t) view->strides[i];
            size *= shape[i];
        }
    }

238
239
240
    buffer_info(const buffer_info &) = delete;
    buffer_info& operator=(const buffer_info &) = delete;

Wenzel Jakob's avatar
Wenzel Jakob committed
241
    buffer_info(buffer_info &&other) {
242
243
244
        (*this) = std::move(other);
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
245
    buffer_info& operator=(buffer_info &&rhs) {
246
247
248
249
250
251
252
253
254
255
256
257
        ptr = rhs.ptr;
        itemsize = rhs.itemsize;
        size = rhs.size;
        format = std::move(rhs.format);
        ndim = rhs.ndim;
        shape = std::move(rhs.shape);
        strides = std::move(rhs.strides);
        std::swap(view, rhs.view);
        std::swap(ownview, rhs.ownview);
        return *this;
    }

258
    ~buffer_info() {
Jason Newton's avatar
Jason Newton committed
259
        if (view && ownview) { PyBuffer_Release(view); delete view; }
260
    }
261

262
263
private:
    Py_buffer *view = nullptr;
Jason Newton's avatar
Jason Newton committed
264
    bool ownview = false;
Wenzel Jakob's avatar
Wenzel Jakob committed
265
266
267
268
};

NAMESPACE_BEGIN(detail)

Wenzel Jakob's avatar
Wenzel Jakob committed
269
inline static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }
270

271
272
inline std::string error_string();

273
274
/// Core part of the 'instance' type which POD (needed to be able to use 'offsetof')
template <typename type> struct instance_essentials {
Wenzel Jakob's avatar
Wenzel Jakob committed
275
276
    PyObject_HEAD
    type *value;
277
    PyObject *weakrefs;
Wenzel Jakob's avatar
Wenzel Jakob committed
278
279
    bool owned : 1;
    bool constructed : 1;
280
281
282
283
};

/// PyObject wrapper around generic types, includes a special holder type that is responsible for lifetime management
template <typename type, typename holder_type = std::unique_ptr<type>> struct instance : instance_essentials<type> {
Wenzel Jakob's avatar
Wenzel Jakob committed
284
285
286
    holder_type holder;
};

287
288
289
290
291
292
293
294
struct overload_hash {
    inline std::size_t operator()(const std::pair<const PyObject *, const char *>& v) const {
        size_t value = std::hash<const void *>()(v.first);
        value ^= std::hash<const void *>()(v.second)  + 0x9e3779b9 + (value<<6) + (value>>2);
        return value;
    }
};

Wenzel Jakob's avatar
Wenzel Jakob committed
295
/// Internal data struture used to track registered instances and types
Wenzel Jakob's avatar
Wenzel Jakob committed
296
struct internals {
297
298
299
    std::unordered_map<std::type_index, void*> registered_types_cpp;   // std::type_index -> type_info
    std::unordered_map<const void *, void*> registered_types_py;       // PyTypeObject* -> type_info
    std::unordered_multimap<const void *, void*> registered_instances; // void * -> PyObject*
300
    std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
301
    std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
302
#if defined(WITH_THREAD)
303
    decltype(PyThread_create_key()) tstate = 0; // Usually an int but a long on Cygwin64 with Python 3.x
304
305
    PyInterpreterState *istate = nullptr;
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
306
307
};

Wenzel Jakob's avatar
Wenzel Jakob committed
308
/// Return a reference to the current 'internals' information
Wenzel Jakob's avatar
Wenzel Jakob committed
309
310
inline internals &get_internals();

Wenzel Jakob's avatar
Wenzel Jakob committed
311
312
313
314
315
316
/// Index sequence for convenient template metaprogramming involving tuples
template<size_t ...> struct index_sequence  { };
template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence <N - 1, N - 1, S...> { };
template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; };

/// Strip the class from a method type
Wenzel Jakob's avatar
Wenzel Jakob committed
317
template <typename T> struct remove_class { };
Wenzel Jakob's avatar
Wenzel Jakob committed
318
319
320
321
template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); };
template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); };

/// Helper template to strip away type modifiers
322
323
324
325
326
327
328
template <typename T> struct intrinsic_type                       { typedef T type; };
template <typename T> struct intrinsic_type<const T>              { typedef typename intrinsic_type<T>::type type; };
template <typename T> struct intrinsic_type<T*>                   { typedef typename intrinsic_type<T>::type type; };
template <typename T> struct intrinsic_type<T&>                   { typedef typename intrinsic_type<T>::type type; };
template <typename T> struct intrinsic_type<T&&>                  { typedef typename intrinsic_type<T>::type type; };
template <typename T, size_t N> struct intrinsic_type<const T[N]> { typedef typename intrinsic_type<T>::type type; };
template <typename T, size_t N> struct intrinsic_type<T[N]>       { typedef typename intrinsic_type<T>::type type; };
329
template <typename T> using intrinsic_t = typename intrinsic_type<T>::type;
Wenzel Jakob's avatar
Wenzel Jakob committed
330
331
332

/// Helper type to replace 'void' in some expressions
struct void_type { };
333

334
335
336
337
338
339
340
341
342
343
344
/// from __cpp_future__ import (convenient aliases from C++14/17)
template <bool B> using bool_constant = std::integral_constant<bool, B>;
template <class T> using negation = bool_constant<!T::value>;
template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type;

/// Compile-time integer sum
constexpr size_t constexpr_sum() { return 0; }
template <typename T, typename... Ts>
constexpr size_t constexpr_sum(T n, Ts... ns) { return size_t{n} + constexpr_sum(ns...); }

Jason Rhinelander's avatar
Jason Rhinelander committed
345
// Counts the number of types in the template parameter pack matching the predicate
346
347
#if !defined(_MSC_VER)
template <template<typename> class Predicate, typename... Ts>
Jason Rhinelander's avatar
Jason Rhinelander committed
348
using count_t = std::integral_constant<size_t, constexpr_sum(Predicate<Ts>::value...)>;
349
350
#else
// MSVC workaround (2015 Update 3 has issues with some member type aliases and constexpr)
Jason Rhinelander's avatar
Jason Rhinelander committed
351
352
353
354
template <template<typename> class Predicate, typename... Ts> struct count_t;
template <template<typename> class Predicate> struct count_t<Predicate> : std::integral_constant<size_t, 0> {};
template <template<typename> class Predicate, class T, class... Ts>
struct count_t<Predicate, T, Ts...> : std::integral_constant<size_t, Predicate<T>::value + count_t<Predicate, Ts...>::value> {};
355
356
#endif

Jason Rhinelander's avatar
Jason Rhinelander committed
357
358
359
360
361
362
/// Return true if all/any Ts satify Predicate<T>
template <template<typename> class Predicate, typename... Ts>
using all_of_t = bool_constant<(count_t<Predicate, Ts...>::value == sizeof...(Ts))>;
template <template<typename> class Predicate, typename... Ts>
using any_of_t = bool_constant<(count_t<Predicate, Ts...>::value > 0)>;

363
364
365
366
// Extracts the first type from the template parameter pack matching the predicate, or Default if none match.
template <template<class> class Predicate, class Default, class... Ts> struct first_of;
template <template<class> class Predicate, class Default> struct first_of<Predicate, Default> {
    using type = Default;
367
};
368
369
template <template<class> class Predicate, class Default, class T, class... Ts>
struct first_of<Predicate, Default, T, Ts...> {
370
371
372
    using type = typename std::conditional<
        Predicate<T>::value,
        T,
373
        typename first_of<Predicate, Default, Ts...>::type
374
375
    >::type;
};
376
template <template<class> class Predicate, class Default, class... T> using first_of_t = typename first_of<Predicate, Default, T...>::type;
377

378
379
380
381
/// Defer the evaluation of type T until types Us are instantiated
template <typename T, typename... /*Us*/> struct deferred_type { using type = T; };
template <typename T, typename... Us> using deferred_t = typename deferred_type<T, Us...>::type;

382
383
384
/// Ignore that a variable is unused in compiler warnings
inline void ignore_unused(const int *) { }

Wenzel Jakob's avatar
Wenzel Jakob committed
385
NAMESPACE_END(detail)
386

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/// Fetch and hold an error which was already set in Python
class error_already_set : public std::runtime_error {
public:
    error_already_set() : std::runtime_error(detail::error_string()) {
        PyErr_Fetch(&type, &value, &trace);
    }
    ~error_already_set() { Py_XDECREF(type); Py_XDECREF(value); Py_XDECREF(trace); }

    /// Give the error back to Python
    void restore() { PyErr_Restore(type, value, trace); type = value = trace = nullptr; }

private:
    PyObject *type, *value, *trace;
};

402
403
404
405
406
407
408
409
410
411
412
413
/// C++ bindings of builtin Python exceptions
class builtin_exception : public std::runtime_error {
public:
    using std::runtime_error::runtime_error;
    virtual void set_error() const = 0; /// Set the error using the Python C API
};

#define PYBIND11_RUNTIME_EXCEPTION(name, type) \
    class name : public builtin_exception { public: \
        using builtin_exception::builtin_exception; \
        name() : name("") { } \
        void set_error() const override { PyErr_SetString(type, what()); } \
Wenzel Jakob's avatar
Wenzel Jakob committed
414
415
    };

416
417
418
419
420
421
422
PYBIND11_RUNTIME_EXCEPTION(stop_iteration, PyExc_StopIteration)
PYBIND11_RUNTIME_EXCEPTION(index_error, PyExc_IndexError)
PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError)
PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError)
PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError)
PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally
423

424
425
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); }
426

427
/// Format strings for basic number types
428
#define PYBIND11_DECL_FMT(t, v) template<> struct format_descriptor<t> \
429
    { static constexpr const char* value = v; /* for backwards compatibility */ \
430
      static std::string format() { return value; } }
431

432
template <typename T, typename SFINAE = void> struct format_descriptor { };
433

434
template <typename T> struct format_descriptor<T, typename std::enable_if<std::is_integral<T>::value>::type> {
435
    static constexpr const char value[2] =
436
        { "bBhHiIqQ"[detail::log2(sizeof(T))*2 + (std::is_unsigned<T>::value ? 1 : 0)], '\0' };
437
    static std::string format() { return value; }
438
};
439

440
template <typename T> constexpr const char format_descriptor<
441
    T, typename std::enable_if<std::is_integral<T>::value>::type>::value[2];
442

Wenzel Jakob's avatar
Wenzel Jakob committed
443
444
445
446
447
448
449
/// RAII wrapper that temporarily clears any Python error state
struct error_scope {
    PyObject *type, *value, *trace;
    error_scope() { PyErr_Fetch(&type, &value, &trace); }
    ~error_scope() { PyErr_Restore(type, value, trace); }
};

450
451
452
PYBIND11_DECL_FMT(float, "f");
PYBIND11_DECL_FMT(double, "d");
PYBIND11_DECL_FMT(bool, "?");
453

Wenzel Jakob's avatar
Wenzel Jakob committed
454
455
/// Dummy destructor wrapper that can be used to expose classes with a private destructor
struct nodelete { template <typename T> void operator()(T*) { } };
456

457
NAMESPACE_END(pybind11)