blob: daafa7dd935a671bb389d41611c92fdfcd02ec8a [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02002 pybind11/common.h -- Basic macros
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02005
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020010#pragma once
Wenzel Jakob38bd7112015-07-05 20:05:44 +020011
12#if !defined(NAMESPACE_BEGIN)
Wenzel Jakob53b26542016-01-17 22:36:43 +010013# define NAMESPACE_BEGIN(name) namespace name {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020014#endif
15#if !defined(NAMESPACE_END)
Wenzel Jakob53b26542016-01-17 22:36:43 +010016# define NAMESPACE_END(name) }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020017#endif
18
Wenzel Jakobb1b71402015-10-18 16:48:30 +020019#if !defined(PYBIND11_EXPORT)
Wenzel Jakob53b26542016-01-17 22:36:43 +010020# if defined(WIN32) || defined(_WIN32)
21# define PYBIND11_EXPORT __declspec(dllexport)
22# else
23# define PYBIND11_EXPORT __attribute__ ((visibility("default")))
24# endif
Wenzel Jakob0fb85282015-10-19 23:50:51 +020025#endif
26
Wenzel Jakob53b26542016-01-17 22:36:43 +010027#if defined(_MSC_VER)
28# define PYBIND11_NOINLINE __declspec(noinline)
29#else
30# define PYBIND11_NOINLINE __attribute__ ((noinline))
31#endif
32
Wenzel Jakob1ae77fe2016-01-17 22:36:43 +010033#define PYBIND11_VERSION_MAJOR 1
Wenzel Jakobf85c5292016-06-14 15:24:47 +020034#define PYBIND11_VERSION_MINOR 9
Wenzel Jakob1ae77fe2016-01-17 22:36:43 +010035
Wenzel Jakob53b26542016-01-17 22:36:43 +010036/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode
37#if defined(_MSC_VER)
38# define HAVE_ROUND
39# pragma warning(push)
40# pragma warning(disable: 4510 4610 4512 4005)
41# if _DEBUG
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +020042# define PYBIND11_DEBUG_MARKER
Wenzel Jakob53b26542016-01-17 22:36:43 +010043# undef _DEBUG
44# endif
45#endif
46
47#include <Python.h>
48#include <frameobject.h>
Wenzel Jakob39e97e62016-04-25 03:26:15 +020049#include <pythread.h>
Wenzel Jakob53b26542016-01-17 22:36:43 +010050
51#ifdef isalnum
52# undef isalnum
53# undef isalpha
54# undef islower
55# undef isspace
56# undef isupper
57# undef tolower
58# undef toupper
59#endif
60
61#if defined(_MSC_VER)
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +020062# if defined(PYBIND11_DEBUG_MARKER)
Wenzel Jakob53b26542016-01-17 22:36:43 +010063# define _DEBUG
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +020064# undef PYBIND11_DEBUG_MARKER
65# endif
Wenzel Jakob53b26542016-01-17 22:36:43 +010066# pragma warning(pop)
67#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +020068
Wenzel Jakob38bd7112015-07-05 20:05:44 +020069#include <vector>
70#include <string>
71#include <stdexcept>
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020072#include <unordered_set>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020073#include <unordered_map>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020074#include <memory>
Wenzel Jakobb6cf75d2016-01-29 11:39:32 +010075#include <typeindex>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020076
Wenzel Jakob27e8e102016-01-17 22:36:37 +010077#if PY_MAJOR_VERSION >= 3 /// Compatibility macros for various Python versions
Wenzel Jakob48548ea2016-01-17 22:36:44 +010078#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)
Wenzel Jakob27e8e102016-01-17 22:36:37 +010079#define PYBIND11_BYTES_CHECK PyBytes_Check
80#define PYBIND11_BYTES_FROM_STRING PyBytes_FromString
81#define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
82#define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize
83#define PYBIND11_BYTES_AS_STRING PyBytes_AsString
Wenzel Jakob5612a0c2016-05-01 00:32:18 +020084#define PYBIND11_BYTES_CHECK PyBytes_Check
Wenzel Jakob27e8e102016-01-17 22:36:37 +010085#define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
86#define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
87#define PYBIND11_LONG_AS_UNSIGNED_LONGLONG(o) PyLong_AsUnsignedLongLong(o)
Wenzel Jakob56e9f492016-01-17 22:36:38 +010088#define PYBIND11_BYTES_NAME "bytes"
Wenzel Jakob27e8e102016-01-17 22:36:37 +010089#define PYBIND11_STRING_NAME "str"
90#define PYBIND11_SLICE_OBJECT PyObject
Wenzel Jakobd561cb02016-01-17 22:36:41 +010091#define PYBIND11_FROM_STRING PyUnicode_FromString
92#define PYBIND11_OB_TYPE(ht_type) (ht_type).ob_base.ob_base.ob_type
93#define PYBIND11_PLUGIN_IMPL(name) \
94 extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
Wenzel Jakob27e8e102016-01-17 22:36:37 +010095#else
Wenzel Jakob48548ea2016-01-17 22:36:44 +010096#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyMethod_New(ptr, nullptr, class_)
Wenzel Jakob27e8e102016-01-17 22:36:37 +010097#define PYBIND11_BYTES_CHECK PyString_Check
98#define PYBIND11_BYTES_FROM_STRING PyString_FromString
99#define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
100#define PYBIND11_BYTES_AS_STRING_AND_SIZE PyString_AsStringAndSize
101#define PYBIND11_BYTES_AS_STRING PyString_AsString
Wenzel Jakob5612a0c2016-05-01 00:32:18 +0200102#define PYBIND11_BYTES_CHECK PyString_Check
Wenzel Jakob27e8e102016-01-17 22:36:37 +0100103#define PYBIND11_LONG_CHECK(o) (PyInt_Check(o) || PyLong_Check(o))
104#define PYBIND11_LONG_AS_LONGLONG(o) (PyInt_Check(o) ? (long long) PyLong_AsLong(o) : PyLong_AsLongLong(o))
105#define PYBIND11_LONG_AS_UNSIGNED_LONGLONG(o) (PyInt_Check(o) ? (unsigned long long) PyLong_AsUnsignedLong(o) : PyLong_AsUnsignedLongLong(o))
Wenzel Jakob56e9f492016-01-17 22:36:38 +0100106#define PYBIND11_BYTES_NAME "str"
Wenzel Jakob27e8e102016-01-17 22:36:37 +0100107#define PYBIND11_STRING_NAME "unicode"
108#define PYBIND11_SLICE_OBJECT PySliceObject
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100109#define PYBIND11_FROM_STRING PyString_FromString
110#define PYBIND11_OB_TYPE(ht_type) (ht_type).ob_type
111#define PYBIND11_PLUGIN_IMPL(name) \
112 extern "C" PYBIND11_EXPORT PyObject *init##name()
Wenzel Jakob27e8e102016-01-17 22:36:37 +0100113#endif
Wenzel Jakob57082212015-09-04 23:42:12 +0200114
Wenzel Jakobfbafdea2016-04-25 15:02:43 +0200115#if PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03050200
116extern "C" {
117 struct _Py_atomic_address { void *value; };
118 PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
Ivan Smirnov3ae5bd72016-06-17 22:29:10 +0100119}
Wenzel Jakobfbafdea2016-04-25 15:02:43 +0200120#endif
121
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100122#define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
Wenzel Jakob77586fd2016-03-06 13:38:18 +0100123#define PYBIND11_STRINGIFY(x) #x
124#define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x)
125#define PYBIND11_INTERNALS_ID "__pybind11_" \
126 PYBIND11_TOSTRING(PYBIND11_VERSION_MAJOR) "_" PYBIND11_TOSTRING(PYBIND11_VERSION_MINOR) "__"
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100127
128#define PYBIND11_PLUGIN(name) \
129 static PyObject *pybind11_init(); \
130 PYBIND11_PLUGIN_IMPL(name) { \
131 try { \
132 return pybind11_init(); \
133 } catch (const std::exception &e) { \
134 PyErr_SetString(PyExc_ImportError, e.what()); \
135 return nullptr; \
136 } \
137 } \
138 PyObject *pybind11_init()
139
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200140NAMESPACE_BEGIN(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200141
142typedef Py_ssize_t ssize_t;
143
144/// Approach used to cast a previously unknown C++ instance into a Python object
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100145enum class return_value_policy : uint8_t {
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200146 /** This is the default return value policy, which falls back to the policy
147 return_value_policy::take_ownership when the return value is a pointer.
148 Otherwise, it uses return_value::move or return_value::copy for rvalue
149 and lvalue references, respectively. See below for a description of what
150 all of these different policies do. */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200151 automatic = 0,
Wenzel Jakob7d0162a2016-04-25 03:24:46 +0200152
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200153 /** As above, but use policy return_value_policy::reference when the return
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200154 value is a pointer. You probably won't need to use this. */
Wenzel Jakob8bd31c72016-04-14 14:26:13 +0200155 automatic_reference,
Wenzel Jakob7d0162a2016-04-25 03:24:46 +0200156
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200157 /** Reference an existing object (i.e. do not create a new copy) and take
158 ownership. Python will call the destructor and delete operator when the
159 object’s reference count reaches zero. Undefined behavior ensues when
160 the C++ side does the same.. */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200161 take_ownership,
Wenzel Jakob7d0162a2016-04-25 03:24:46 +0200162
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200163 /** Create a new copy of the returned object, which will be owned by
164 Python. This policy is comparably safe because the lifetimes of the two
165 instances are decoupled. */
166 copy,
167
168 /** Use std::move to move the return value contents into a new instance
169 that will be owned by Python. This policy is comparably safe because the
170 lifetimes of the two instances (move source and destination) are
171 decoupled. */
172 move,
173
174 /** Reference an existing object, but do not take ownership. The C++ side
175 is responsible for managing the object’s lifetime and deallocating it
176 when it is no longer used. Warning: undefined behavior will ensue when
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200177 the C++ side deletes an object that is still referenced and used by
178 Python. */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200179 reference,
Wenzel Jakob7d0162a2016-04-25 03:24:46 +0200180
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200181 /** This policy only applies to methods and properties. It references the
182 object without taking ownership similar to the above
183 return_value_policy::reference policy. In contrast to that policy, the
184 function or property’s implicit this argument (called the parent) is
185 considered to be the the owner of the return value (the child).
186 pybind11 then couples the lifetime of the parent to the child via a
187 reference relationship that ensures that the parent cannot be garbage
188 collected while Python is still using the child. More advanced
189 variations of this scheme are also possible using combinations of
190 return_value_policy::reference and the keep_alive call policy */
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200191 reference_internal
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200192};
193
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200194/// Information record describing a Python buffer object
195struct buffer_info {
Wenzel Jakobe45b2902016-01-17 22:36:41 +0100196 void *ptr; // Pointer to the underlying storage
197 size_t itemsize; // Size of individual items in bytes
198 size_t size; // Total number of entries
199 std::string format; // For homogeneous buffers, this should be set to format_descriptor<T>::value
Wenzel Jakob0a078052016-05-29 13:40:40 +0200200 size_t ndim; // Number of dimensions
Wenzel Jakobe45b2902016-01-17 22:36:41 +0100201 std::vector<size_t> shape; // Shape of the tensor (1 entry per dimension)
202 std::vector<size_t> strides; // Number of entries between adjacent entries (for each per dimension)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200203
Johan Mabillea63d93b2016-05-11 11:25:15 +0200204 buffer_info() : ptr(nullptr), view(nullptr) {}
Wenzel Jakob0a078052016-05-29 13:40:40 +0200205 buffer_info(void *ptr, size_t itemsize, const std::string &format, size_t ndim,
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200206 const std::vector<size_t> &shape, const std::vector<size_t> &strides)
Wenzel Jakobd33361a2016-01-17 22:36:40 +0100207 : ptr(ptr), itemsize(itemsize), size(1), format(format),
208 ndim(ndim), shape(shape), strides(strides) {
Wenzel Jakob0a078052016-05-29 13:40:40 +0200209 for (size_t i = 0; i < ndim; ++i)
210 size *= shape[i];
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200211 }
Wenzel Jakobd33361a2016-01-17 22:36:40 +0100212
213 buffer_info(Py_buffer *view)
Wenzel Jakob0a078052016-05-29 13:40:40 +0200214 : ptr(view->buf), itemsize((size_t) view->itemsize), size(1), format(view->format),
215 ndim((size_t) view->ndim), shape((size_t) view->ndim), strides((size_t) view->ndim), view(view) {
216 for (size_t i = 0; i < (size_t) view->ndim; ++i) {
Wenzel Jakobd33361a2016-01-17 22:36:40 +0100217 shape[i] = (size_t) view->shape[i];
218 strides[i] = (size_t) view->strides[i];
219 size *= shape[i];
220 }
221 }
222
223 ~buffer_info() {
224 if (view) { PyBuffer_Release(view); delete view; }
225 }
226private:
227 Py_buffer *view = nullptr;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200228};
229
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200230NAMESPACE_BEGIN(detail)
231
Wenzel Jakobe6118232016-05-05 21:54:24 +0200232inline static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }
Wenzel Jakob876eeab2016-05-04 22:22:48 +0200233
Wenzel Jakob96c10532015-10-01 16:42:15 +0200234inline std::string error_string();
235
Wenzel Jakob88d1d042016-01-20 01:26:42 +0100236/// Core part of the 'instance' type which POD (needed to be able to use 'offsetof')
237template <typename type> struct instance_essentials {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200238 PyObject_HEAD
239 type *value;
240 PyObject *parent;
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100241 PyObject *weakrefs;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200242 bool owned : 1;
243 bool constructed : 1;
Wenzel Jakob88d1d042016-01-20 01:26:42 +0100244};
245
246/// PyObject wrapper around generic types, includes a special holder type that is responsible for lifetime management
247template <typename type, typename holder_type = std::unique_ptr<type>> struct instance : instance_essentials<type> {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200248 holder_type holder;
249};
250
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200251struct overload_hash {
252 inline std::size_t operator()(const std::pair<const PyObject *, const char *>& v) const {
253 size_t value = std::hash<const void *>()(v.first);
254 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2);
255 return value;
256 }
257};
258
Wenzel Jakob43398a82015-07-28 16:12:20 +0200259/// Internal data struture used to track registered instances and types
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200260struct internals {
Wenzel Jakobb6cf75d2016-01-29 11:39:32 +0100261 std::unordered_map<std::type_index, void*> registered_types_cpp; // std::type_index -> type_info
262 std::unordered_map<const void *, void*> registered_types_py; // PyTypeObject* -> type_info
263 std::unordered_map<const void *, void*> registered_instances; // void * -> PyObject*
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200264 std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
Wenzel Jakob39e97e62016-04-25 03:26:15 +0200265#if defined(WITH_THREAD)
Boris Schäling20ee9352016-05-28 12:26:18 +0200266 decltype(PyThread_create_key()) tstate = 0; // Usually an int but a long on Cygwin64 with Python 3.x
Wenzel Jakob39e97e62016-04-25 03:26:15 +0200267 PyInterpreterState *istate = nullptr;
268#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200269};
270
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200271/// Return a reference to the current 'internals' information
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200272inline internals &get_internals();
273
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200274/// Index sequence for convenient template metaprogramming involving tuples
275template<size_t ...> struct index_sequence { };
276template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence <N - 1, N - 1, S...> { };
277template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; };
278
279/// Strip the class from a method type
Wenzel Jakobad696342016-05-03 13:28:40 +0200280template <typename T> struct remove_class { };
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200281template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); };
282template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); };
283
284/// Helper template to strip away type modifiers
Wenzel Jakob4177ed42016-01-17 22:36:38 +0100285template <typename T> struct intrinsic_type { typedef T type; };
286template <typename T> struct intrinsic_type<const T> { typedef typename intrinsic_type<T>::type type; };
287template <typename T> struct intrinsic_type<T*> { typedef typename intrinsic_type<T>::type type; };
288template <typename T> struct intrinsic_type<T&> { typedef typename intrinsic_type<T>::type type; };
289template <typename T> struct intrinsic_type<T&&> { typedef typename intrinsic_type<T>::type type; };
290template <typename T, size_t N> struct intrinsic_type<const T[N]> { typedef typename intrinsic_type<T>::type type; };
291template <typename T, size_t N> struct intrinsic_type<T[N]> { typedef typename intrinsic_type<T>::type type; };
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200292
293/// Helper type to replace 'void' in some expressions
294struct void_type { };
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200295
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200296NAMESPACE_END(detail)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200297
Wenzel Jakobad696342016-05-03 13:28:40 +0200298#define PYBIND11_RUNTIME_EXCEPTION(name) \
299 class name : public std::runtime_error { public: \
300 name(const std::string &w) : std::runtime_error(w) { }; \
301 name(const char *s) : std::runtime_error(s) { }; \
302 name() : std::runtime_error("") { } \
303 };
304
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200305// C++ bindings of core Python exceptions
Wenzel Jakobad696342016-05-03 13:28:40 +0200306class error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} };
307PYBIND11_RUNTIME_EXCEPTION(stop_iteration)
308PYBIND11_RUNTIME_EXCEPTION(index_error)
Sergey Lyskova315c7a2016-05-07 18:50:26 -0400309PYBIND11_RUNTIME_EXCEPTION(value_error)
Wenzel Jakobad696342016-05-03 13:28:40 +0200310PYBIND11_RUNTIME_EXCEPTION(cast_error) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200311
Wenzel Jakobfc92d822016-04-30 23:55:44 +0200312[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }
313[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); }
Wenzel Jakob678d7872016-01-17 22:36:41 +0100314
Wenzel Jakob876eeab2016-05-04 22:22:48 +0200315/// Format strings for basic number types
316#define PYBIND11_DECL_FMT(t, v) template<> struct format_descriptor<t> { static constexpr const char *value = v; }
317template <typename T, typename SFINAE = void> struct format_descriptor { };
318template <typename T> struct format_descriptor<T, typename std::enable_if<std::is_integral<T>::value>::type> {
319 static constexpr const char value[2] =
320 { "bBhHiIqQ"[detail::log2(sizeof(T))*2 + (std::is_unsigned<T>::value ? 1 : 0)], '\0' };
321};
322template <typename T> constexpr const char format_descriptor<
323 T, typename std::enable_if<std::is_integral<T>::value>::type>::value[2];
324PYBIND11_DECL_FMT(float, "f"); PYBIND11_DECL_FMT(double, "d"); PYBIND11_DECL_FMT(bool, "?");
325
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200326NAMESPACE_END(pybind11)