2015-07-05 18:05:44 +00:00
|
|
|
|
/*
|
2015-10-15 16:13:33 +00:00
|
|
|
|
pybind11/common.h -- Basic macros
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-07-11 15:41:48 +00:00
|
|
|
|
#pragma once
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
|
|
#if !defined(NAMESPACE_BEGIN)
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# define NAMESPACE_BEGIN(name) namespace name {
|
2015-07-05 18:05:44 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if !defined(NAMESPACE_END)
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# define NAMESPACE_END(name) }
|
2015-07-05 18:05:44 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-04 13:49:37 +00:00
|
|
|
|
// Neither MSVC nor Intel support enough of C++14 yet (in particular, as of MSVC 2015 and ICC 17
|
|
|
|
|
// beta, neither support extended constexpr, which we rely on in descr.h), so don't enable pybind
|
|
|
|
|
// CPP14 features for them.
|
|
|
|
|
#if !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
|
|
|
|
# if __cplusplus >= 201402L
|
|
|
|
|
# define PYBIND11_CPP14
|
|
|
|
|
# if __cplusplus > 201402L /* Temporary: should be updated to >= the final C++17 value once known */
|
|
|
|
|
# define PYBIND11_CPP17
|
|
|
|
|
# endif
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-10-18 14:48:30 +00:00
|
|
|
|
#if !defined(PYBIND11_EXPORT)
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# if defined(WIN32) || defined(_WIN32)
|
|
|
|
|
# define PYBIND11_EXPORT __declspec(dllexport)
|
|
|
|
|
# else
|
|
|
|
|
# define PYBIND11_EXPORT __attribute__ ((visibility("default")))
|
|
|
|
|
# endif
|
2015-07-05 18:05:44 +00:00
|
|
|
|
#endif
|
2016-01-17 21:36:43 +00:00
|
|
|
|
|
2015-10-19 21:50:51 +00:00
|
|
|
|
#if defined(_MSC_VER)
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# define PYBIND11_NOINLINE __declspec(noinline)
|
2015-10-19 21:50:51 +00:00
|
|
|
|
#else
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# define PYBIND11_NOINLINE __attribute__ ((noinline))
|
2015-10-19 21:50:51 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-04 13:49:37 +00:00
|
|
|
|
#if defined(PYBIND11_CPP14)
|
2016-09-12 03:03:20 +00:00
|
|
|
|
# define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]]
|
|
|
|
|
#elif defined(__clang__)
|
|
|
|
|
# define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason)))
|
|
|
|
|
#elif defined(__GNUG__)
|
|
|
|
|
# define PYBIND11_DEPRECATED(reason) __attribute__((deprecated))
|
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
# define PYBIND11_DEPRECATED(reason) __declspec(deprecated)
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-01-01 12:55:06 +00:00
|
|
|
|
#define PYBIND11_VERSION_MAJOR 2
|
2017-01-01 13:29:40 +00:00
|
|
|
|
#define PYBIND11_VERSION_MINOR 1
|
|
|
|
|
#define PYBIND11_VERSION_PATCH dev0
|
2016-01-17 21:36:43 +00:00
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
|
/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode
|
|
|
|
|
#if defined(_MSC_VER)
|
2017-02-18 13:29:54 +00:00
|
|
|
|
# if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 4)
|
|
|
|
|
# define HAVE_ROUND 1
|
|
|
|
|
# endif
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# pragma warning(push)
|
|
|
|
|
# pragma warning(disable: 4510 4610 4512 4005)
|
2017-01-31 15:54:49 +00:00
|
|
|
|
# if defined(_DEBUG)
|
2016-04-17 18:21:41 +00:00
|
|
|
|
# define PYBIND11_DEBUG_MARKER
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# undef _DEBUG
|
|
|
|
|
# endif
|
2015-07-05 18:05:44 +00:00
|
|
|
|
#endif
|
2016-01-17 21:36:43 +00:00
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
|
#include <Python.h>
|
2015-10-01 14:42:15 +00:00
|
|
|
|
#include <frameobject.h>
|
2016-04-25 01:26:15 +00:00
|
|
|
|
#include <pythread.h>
|
2016-01-17 21:36:43 +00:00
|
|
|
|
|
2016-07-19 15:47:59 +00:00
|
|
|
|
#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)
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# undef isalnum
|
|
|
|
|
# undef isalpha
|
|
|
|
|
# undef islower
|
|
|
|
|
# undef isspace
|
|
|
|
|
# undef isupper
|
|
|
|
|
# undef tolower
|
|
|
|
|
# undef toupper
|
2015-07-30 13:29:00 +00:00
|
|
|
|
#endif
|
2016-01-17 21:36:43 +00:00
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
|
#if defined(_MSC_VER)
|
2016-04-17 18:21:41 +00:00
|
|
|
|
# if defined(PYBIND11_DEBUG_MARKER)
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# define _DEBUG
|
2016-04-17 18:21:41 +00:00
|
|
|
|
# undef PYBIND11_DEBUG_MARKER
|
|
|
|
|
# endif
|
2016-01-17 21:36:43 +00:00
|
|
|
|
# pragma warning(pop)
|
2015-07-05 18:05:44 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-15 05:37:39 +00:00
|
|
|
|
#include <cstddef>
|
2016-06-17 21:35:59 +00:00
|
|
|
|
#include <forward_list>
|
2016-01-17 21:36:43 +00:00
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <memory>
|
2016-01-29 10:39:32 +00:00
|
|
|
|
#include <typeindex>
|
2016-09-11 11:00:40 +00:00
|
|
|
|
#include <type_traits>
|
2016-01-17 21:36:43 +00:00
|
|
|
|
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#if PY_MAJOR_VERSION >= 3 /// Compatibility macros for various Python versions
|
2016-01-17 21:36:44 +00:00
|
|
|
|
#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#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
|
Unicode fixes and docs (#624)
* Propagate unicode conversion failure
If returning a std::string with invalid utf-8 data, we currently fail
with an uninformative TypeError instead of propagating the
UnicodeDecodeError that Python sets on failure.
* Add support for u16/u32strings and literals
This adds support for wchar{16,32}_t character literals and the
associated std::u{16,32}string types. It also folds the
character/string conversion into a single type_caster template, since
the type casters for string and wstring were mostly the same anyway.
* Added too-long and too-big character conversion errors
With this commit, when casting to a single character, as opposed to a
C-style string, we make sure the input wasn't a multi-character string
or a single character with codepoint too large for the character type.
This also changes the character cast op to CharT instead of CharT& (we
need to be able to return a temporary decoded char value, but also
because there's little gained by bothering with an lvalue return here).
Finally it changes the char caster to 'has-a-string-caster' instead of
'is-a-string-caster' because, with the cast_op change above, there's
nothing at all gained from inheritance. This also lets us remove the
`success` from the string caster (which was only there for the char
caster) into the char caster itself. (I also renamed it to 'none' and
inverted its value to better reflect its purpose). The None -> nullptr
loading also now takes place only under a `convert = true` load pass.
Although it's unlikely that a function taking a char also has overloads
that can take a None, it seems marginally more correct to treat it as a
conversion.
This commit simplifies the size assumptions about character sizes with
static_asserts to back them up.
2017-02-14 10:08:19 +00:00
|
|
|
|
#define PYBIND11_BYTES_SIZE PyBytes_Size
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#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)
|
2016-01-17 21:36:38 +00:00
|
|
|
|
#define PYBIND11_BYTES_NAME "bytes"
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#define PYBIND11_STRING_NAME "str"
|
|
|
|
|
#define PYBIND11_SLICE_OBJECT PyObject
|
2016-01-17 21:36:41 +00:00
|
|
|
|
#define PYBIND11_FROM_STRING PyUnicode_FromString
|
2016-08-13 11:39:16 +00:00
|
|
|
|
#define PYBIND11_STR_TYPE ::pybind11::str
|
2016-01-17 21:36:41 +00:00
|
|
|
|
#define PYBIND11_PLUGIN_IMPL(name) \
|
|
|
|
|
extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#else
|
2016-01-17 21:36:44 +00:00
|
|
|
|
#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyMethod_New(ptr, nullptr, class_)
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#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
|
Unicode fixes and docs (#624)
* Propagate unicode conversion failure
If returning a std::string with invalid utf-8 data, we currently fail
with an uninformative TypeError instead of propagating the
UnicodeDecodeError that Python sets on failure.
* Add support for u16/u32strings and literals
This adds support for wchar{16,32}_t character literals and the
associated std::u{16,32}string types. It also folds the
character/string conversion into a single type_caster template, since
the type casters for string and wstring were mostly the same anyway.
* Added too-long and too-big character conversion errors
With this commit, when casting to a single character, as opposed to a
C-style string, we make sure the input wasn't a multi-character string
or a single character with codepoint too large for the character type.
This also changes the character cast op to CharT instead of CharT& (we
need to be able to return a temporary decoded char value, but also
because there's little gained by bothering with an lvalue return here).
Finally it changes the char caster to 'has-a-string-caster' instead of
'is-a-string-caster' because, with the cast_op change above, there's
nothing at all gained from inheritance. This also lets us remove the
`success` from the string caster (which was only there for the char
caster) into the char caster itself. (I also renamed it to 'none' and
inverted its value to better reflect its purpose). The None -> nullptr
loading also now takes place only under a `convert = true` load pass.
Although it's unlikely that a function taking a char also has overloads
that can take a None, it seems marginally more correct to treat it as a
conversion.
This commit simplifies the size assumptions about character sizes with
static_asserts to back them up.
2017-02-14 10:08:19 +00:00
|
|
|
|
#define PYBIND11_BYTES_SIZE PyString_Size
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#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))
|
2016-01-17 21:36:38 +00:00
|
|
|
|
#define PYBIND11_BYTES_NAME "str"
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#define PYBIND11_STRING_NAME "unicode"
|
|
|
|
|
#define PYBIND11_SLICE_OBJECT PySliceObject
|
2016-01-17 21:36:41 +00:00
|
|
|
|
#define PYBIND11_FROM_STRING PyString_FromString
|
2016-08-13 11:39:16 +00:00
|
|
|
|
#define PYBIND11_STR_TYPE ::pybind11::bytes
|
2016-01-17 21:36:41 +00:00
|
|
|
|
#define PYBIND11_PLUGIN_IMPL(name) \
|
2017-01-13 10:12:22 +00:00
|
|
|
|
static PyObject *pybind11_init_wrapper(); \
|
|
|
|
|
extern "C" PYBIND11_EXPORT void init##name() { \
|
|
|
|
|
(void)pybind11_init_wrapper(); \
|
|
|
|
|
} \
|
|
|
|
|
PyObject *pybind11_init_wrapper()
|
2016-01-17 21:36:37 +00:00
|
|
|
|
#endif
|
2015-09-04 21:42:12 +00:00
|
|
|
|
|
2016-04-25 13:02:43 +00:00
|
|
|
|
#if PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03050200
|
|
|
|
|
extern "C" {
|
|
|
|
|
struct _Py_atomic_address { void *value; };
|
|
|
|
|
PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
|
2016-06-17 21:29:10 +00:00
|
|
|
|
}
|
2016-04-25 13:02:43 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-01-17 21:36:41 +00:00
|
|
|
|
#define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
|
2016-03-06 12:38:18 +00:00
|
|
|
|
#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) "__"
|
2016-01-17 21:36:41 +00:00
|
|
|
|
|
2017-01-31 15:54:08 +00:00
|
|
|
|
/** \rst
|
|
|
|
|
This macro creates the entry point that will be invoked when the Python interpreter
|
|
|
|
|
imports a plugin library. Please create a `module` in the function body and return
|
|
|
|
|
the pointer to its underlying Python object at the end.
|
|
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
|
|
PYBIND11_PLUGIN(example) {
|
|
|
|
|
pybind11::module m("example", "pybind11 example plugin");
|
|
|
|
|
/// Set up bindings here
|
|
|
|
|
return m.ptr();
|
|
|
|
|
}
|
|
|
|
|
\endrst */
|
2016-10-09 17:40:15 +00:00
|
|
|
|
#define PYBIND11_PLUGIN(name) \
|
|
|
|
|
static PyObject *pybind11_init(); \
|
|
|
|
|
PYBIND11_PLUGIN_IMPL(name) { \
|
|
|
|
|
int major, minor; \
|
|
|
|
|
if (sscanf(Py_GetVersion(), "%i.%i", &major, &minor) != 2) { \
|
|
|
|
|
PyErr_SetString(PyExc_ImportError, "Can't parse Python version."); \
|
|
|
|
|
return nullptr; \
|
|
|
|
|
} else if (major != PY_MAJOR_VERSION || minor != PY_MINOR_VERSION) { \
|
|
|
|
|
PyErr_Format(PyExc_ImportError, \
|
|
|
|
|
"Python version mismatch: module was compiled for " \
|
|
|
|
|
"version %i.%i, while the interpreter is running " \
|
|
|
|
|
"version %i.%i.", PY_MAJOR_VERSION, PY_MINOR_VERSION, \
|
|
|
|
|
major, minor); \
|
|
|
|
|
return nullptr; \
|
|
|
|
|
} \
|
|
|
|
|
try { \
|
|
|
|
|
return pybind11_init(); \
|
2017-02-08 19:23:56 +00:00
|
|
|
|
} catch (pybind11::error_already_set &e) { \
|
|
|
|
|
e.clear(); \
|
|
|
|
|
PyErr_SetString(PyExc_ImportError, e.what()); \
|
|
|
|
|
return nullptr; \
|
2016-10-09 17:40:15 +00:00
|
|
|
|
} catch (const std::exception &e) { \
|
|
|
|
|
PyErr_SetString(PyExc_ImportError, e.what()); \
|
|
|
|
|
return nullptr; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
2016-01-17 21:36:41 +00:00
|
|
|
|
PyObject *pybind11_init()
|
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
|
NAMESPACE_BEGIN(pybind11)
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
2016-11-15 05:37:39 +00:00
|
|
|
|
using ssize_t = Py_ssize_t;
|
|
|
|
|
using size_t = std::size_t;
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
|
|
/// Approach used to cast a previously unknown C++ instance into a Python object
|
2016-05-10 14:59:01 +00:00
|
|
|
|
enum class return_value_policy : uint8_t {
|
2016-04-25 21:04:27 +00:00
|
|
|
|
/** 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. */
|
2015-07-05 18:05:44 +00:00
|
|
|
|
automatic = 0,
|
2016-04-25 01:24:46 +00:00
|
|
|
|
|
2016-04-25 21:04:27 +00:00
|
|
|
|
/** As above, but use policy return_value_policy::reference when the return
|
2016-06-22 12:29:13 +00:00
|
|
|
|
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. */
|
2016-04-14 12:26:13 +00:00
|
|
|
|
automatic_reference,
|
2016-04-25 01:24:46 +00:00
|
|
|
|
|
2016-04-25 21:04:27 +00:00
|
|
|
|
/** 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.. */
|
2015-07-05 18:05:44 +00:00
|
|
|
|
take_ownership,
|
2016-04-25 01:24:46 +00:00
|
|
|
|
|
2016-04-25 21:04:27 +00:00
|
|
|
|
/** 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
|
2016-04-26 21:19:19 +00:00
|
|
|
|
the C++ side deletes an object that is still referenced and used by
|
|
|
|
|
Python. */
|
2015-07-05 18:05:44 +00:00
|
|
|
|
reference,
|
2016-04-25 01:24:46 +00:00
|
|
|
|
|
2016-04-26 21:19:19 +00:00
|
|
|
|
/** 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 */
|
2016-04-25 21:04:27 +00:00
|
|
|
|
reference_internal
|
2015-07-05 18:05:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Information record describing a Python buffer object
|
|
|
|
|
struct buffer_info {
|
2016-09-02 21:10:02 +00:00
|
|
|
|
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
|
2016-06-26 11:42:34 +00:00
|
|
|
|
std::string format; // For homogeneous buffers, this should be set to format_descriptor<T>::format()
|
2016-09-02 21:10:02 +00:00
|
|
|
|
size_t ndim = 0; // Number of dimensions
|
2016-01-17 21:36:41 +00:00
|
|
|
|
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)
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
2016-09-04 14:03:48 +00:00
|
|
|
|
buffer_info() { }
|
2016-07-24 19:29:44 +00:00
|
|
|
|
|
2016-05-29 11:40:40 +00:00
|
|
|
|
buffer_info(void *ptr, size_t itemsize, const std::string &format, size_t ndim,
|
2015-07-26 14:33:49 +00:00
|
|
|
|
const std::vector<size_t> &shape, const std::vector<size_t> &strides)
|
2016-01-17 21:36:40 +00:00
|
|
|
|
: ptr(ptr), itemsize(itemsize), size(1), format(format),
|
|
|
|
|
ndim(ndim), shape(shape), strides(strides) {
|
2016-05-29 11:40:40 +00:00
|
|
|
|
for (size_t i = 0; i < ndim; ++i)
|
|
|
|
|
size *= shape[i];
|
2015-07-26 14:33:49 +00:00
|
|
|
|
}
|
2016-01-17 21:36:40 +00:00
|
|
|
|
|
2016-07-24 19:29:44 +00:00
|
|
|
|
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 }) { }
|
|
|
|
|
|
2016-10-16 20:27:42 +00:00
|
|
|
|
explicit buffer_info(Py_buffer *view, bool ownview = true)
|
2016-05-29 11:40:40 +00:00
|
|
|
|
: ptr(view->buf), itemsize((size_t) view->itemsize), size(1), format(view->format),
|
2016-09-02 21:10:25 +00:00
|
|
|
|
ndim((size_t) view->ndim), shape((size_t) view->ndim), strides((size_t) view->ndim), view(view), ownview(ownview) {
|
2016-05-29 11:40:40 +00:00
|
|
|
|
for (size_t i = 0; i < (size_t) view->ndim; ++i) {
|
2016-01-17 21:36:40 +00:00
|
|
|
|
shape[i] = (size_t) view->shape[i];
|
|
|
|
|
strides[i] = (size_t) view->strides[i];
|
|
|
|
|
size *= shape[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 22:39:47 +00:00
|
|
|
|
buffer_info(const buffer_info &) = delete;
|
|
|
|
|
buffer_info& operator=(const buffer_info &) = delete;
|
|
|
|
|
|
2016-09-04 14:03:48 +00:00
|
|
|
|
buffer_info(buffer_info &&other) {
|
2016-09-02 21:11:30 +00:00
|
|
|
|
(*this) = std::move(other);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-04 23:25:10 +00:00
|
|
|
|
buffer_info& operator=(buffer_info &&rhs) {
|
2016-09-02 21:11:30 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
|
~buffer_info() {
|
2016-09-02 21:10:25 +00:00
|
|
|
|
if (view && ownview) { PyBuffer_Release(view); delete view; }
|
2016-01-17 21:36:40 +00:00
|
|
|
|
}
|
2016-06-19 13:37:55 +00:00
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
|
private:
|
|
|
|
|
Py_buffer *view = nullptr;
|
2016-09-02 21:10:25 +00:00
|
|
|
|
bool ownview = false;
|
2015-07-05 18:05:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
|
2016-05-05 19:54:24 +00:00
|
|
|
|
inline static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }
|
2016-05-04 20:22:48 +00:00
|
|
|
|
|
2015-10-01 14:42:15 +00:00
|
|
|
|
inline std::string error_string();
|
|
|
|
|
|
2016-01-20 00:26:42 +00:00
|
|
|
|
/// Core part of the 'instance' type which POD (needed to be able to use 'offsetof')
|
|
|
|
|
template <typename type> struct instance_essentials {
|
2015-07-05 18:05:44 +00:00
|
|
|
|
PyObject_HEAD
|
|
|
|
|
type *value;
|
2016-01-17 21:36:39 +00:00
|
|
|
|
PyObject *weakrefs;
|
2015-07-05 18:05:44 +00:00
|
|
|
|
bool owned : 1;
|
Don't construct unique_ptr around unowned pointers (#478)
If we need to initialize a holder around an unowned instance, and the
holder type is non-copyable (i.e. a unique_ptr), we currently construct
the holder type around the value pointer, but then never actually
destruct the holder: the holder destructor is called only for the
instance that actually has `inst->owned = true` set.
This seems no pointer, however, in creating such a holder around an
unowned instance: we never actually intend to use anything that the
unique_ptr gives us: and, in fact, do not want the unique_ptr (because
if it ever actually got destroyed, it would cause destruction of the
wrapped pointer, despite the fact that that wrapped pointer isn't
owned).
This commit changes the logic to only create a unique_ptr holder if we
actually own the instance, and to destruct via the constructed holder
whenever we have a constructed holder--which will now only be the case
for owned-unique-holder or shared-holder types.
Other changes include:
* Added test for non-movable holder constructor/destructor counts
The three alive assertions now pass, before #478 they fail with counts
of 2/2/1 respectively, because of the unique_ptr that we don't want and
don't destroy (because we don't *want* its destructor to run).
* Return cstats reference; fix ConstructStats doc
Small cleanup to the #478 test code, and fix to the ConstructStats
documentation (the static method definition should use `reference` not
`reference_internal`).
* Rename inst->constructed to inst->holder_constructed
This makes it clearer exactly what it's referring to.
2016-11-06 18:12:48 +00:00
|
|
|
|
bool holder_constructed : 1;
|
2016-01-20 00:26:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// 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> {
|
2015-07-05 18:05:44 +00:00
|
|
|
|
holder_type holder;
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-01 14:46:03 +00:00
|
|
|
|
struct overload_hash {
|
2016-11-15 05:37:39 +00:00
|
|
|
|
inline size_t operator()(const std::pair<const PyObject *, const char *>& v) const {
|
2015-10-01 14:46:03 +00:00
|
|
|
|
size_t value = std::hash<const void *>()(v.first);
|
|
|
|
|
value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-13 17:11:24 +00:00
|
|
|
|
/// Internal data structure used to track registered instances and types
|
2015-07-05 18:05:44 +00:00
|
|
|
|
struct internals {
|
2016-08-09 21:57:59 +00:00
|
|
|
|
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*
|
2015-10-01 14:46:03 +00:00
|
|
|
|
std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
|
2016-10-20 15:09:31 +00:00
|
|
|
|
std::unordered_map<std::type_index, std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
|
2016-06-17 21:35:59 +00:00
|
|
|
|
std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
|
2016-10-31 14:11:10 +00:00
|
|
|
|
std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions
|
2017-02-13 17:11:24 +00:00
|
|
|
|
PyTypeObject *static_property_type;
|
|
|
|
|
PyTypeObject *default_metaclass;
|
2017-02-15 20:10:25 +00:00
|
|
|
|
std::unordered_map<size_t, PyObject *> bases; // one base type per `instance_size` (very few)
|
2016-04-25 01:26:15 +00:00
|
|
|
|
#if defined(WITH_THREAD)
|
2016-05-28 10:26:18 +00:00
|
|
|
|
decltype(PyThread_create_key()) tstate = 0; // Usually an int but a long on Cygwin64 with Python 3.x
|
2016-04-25 01:26:15 +00:00
|
|
|
|
PyInterpreterState *istate = nullptr;
|
|
|
|
|
#endif
|
2017-02-15 20:10:25 +00:00
|
|
|
|
|
|
|
|
|
/// Return the appropriate base type for the given instance size
|
|
|
|
|
PyObject *get_base(size_t instance_size);
|
2015-07-05 18:05:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
|
/// Return a reference to the current 'internals' information
|
2015-07-05 18:05:44 +00:00
|
|
|
|
inline internals &get_internals();
|
|
|
|
|
|
Change all_of_t/any_of_t to all_of/any_of, add none_of
This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
with previous use of `all_of_t<Pred, Ts...>` becoming
`all_of<Pred<Ts>...>` (and similarly for `any_of_t`). It also adds a
`none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
in cases where several predicates need to be tested for the same type
instead of the same predicate for multiple types.
This commit replaces the implementation with a more efficient version
for non-MSVC. For MSVC, this changes the workaround to use the
built-in, recursive std::conjunction/std::disjunction instead.
This also removes the `count_t` since `any_of_t` and `all_of_t` were the
only things using it.
This commit also rearranges some of the future std imports to use actual
`std` implementations for C++14/17 features when under the appropriate
compiler mode, as we were already doing for a few things (like
index_sequence). Most of these aren't saving much (the implementation
for enable_if_t, for example, is trivial), but I think it makes the
intention of the code instantly clear. It also enables MSVC's native
std::index_sequence support.
2016-12-12 23:11:49 +00:00
|
|
|
|
/// from __cpp_future__ import (convenient aliases from C++14/17)
|
2016-11-27 19:56:04 +00:00
|
|
|
|
#ifdef PYBIND11_CPP14
|
Change all_of_t/any_of_t to all_of/any_of, add none_of
This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
with previous use of `all_of_t<Pred, Ts...>` becoming
`all_of<Pred<Ts>...>` (and similarly for `any_of_t`). It also adds a
`none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
in cases where several predicates need to be tested for the same type
instead of the same predicate for multiple types.
This commit replaces the implementation with a more efficient version
for non-MSVC. For MSVC, this changes the workaround to use the
built-in, recursive std::conjunction/std::disjunction instead.
This also removes the `count_t` since `any_of_t` and `all_of_t` were the
only things using it.
This commit also rearranges some of the future std imports to use actual
`std` implementations for C++14/17 features when under the appropriate
compiler mode, as we were already doing for a few things (like
index_sequence). Most of these aren't saving much (the implementation
for enable_if_t, for example, is trivial), but I think it makes the
intention of the code instantly clear. It also enables MSVC's native
std::index_sequence support.
2016-12-12 23:11:49 +00:00
|
|
|
|
using std::enable_if_t;
|
|
|
|
|
using std::conditional_t;
|
2017-01-14 16:25:28 +00:00
|
|
|
|
using std::remove_cv_t;
|
Change all_of_t/any_of_t to all_of/any_of, add none_of
This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
with previous use of `all_of_t<Pred, Ts...>` becoming
`all_of<Pred<Ts>...>` (and similarly for `any_of_t`). It also adds a
`none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
in cases where several predicates need to be tested for the same type
instead of the same predicate for multiple types.
This commit replaces the implementation with a more efficient version
for non-MSVC. For MSVC, this changes the workaround to use the
built-in, recursive std::conjunction/std::disjunction instead.
This also removes the `count_t` since `any_of_t` and `all_of_t` were the
only things using it.
This commit also rearranges some of the future std imports to use actual
`std` implementations for C++14/17 features when under the appropriate
compiler mode, as we were already doing for a few things (like
index_sequence). Most of these aren't saving much (the implementation
for enable_if_t, for example, is trivial), but I think it makes the
intention of the code instantly clear. It also enables MSVC's native
std::index_sequence support.
2016-12-12 23:11:49 +00:00
|
|
|
|
#else
|
|
|
|
|
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;
|
2017-01-14 16:25:28 +00:00
|
|
|
|
template <typename T> using remove_cv_t = typename std::remove_cv<T>::type;
|
Change all_of_t/any_of_t to all_of/any_of, add none_of
This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
with previous use of `all_of_t<Pred, Ts...>` becoming
`all_of<Pred<Ts>...>` (and similarly for `any_of_t`). It also adds a
`none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
in cases where several predicates need to be tested for the same type
instead of the same predicate for multiple types.
This commit replaces the implementation with a more efficient version
for non-MSVC. For MSVC, this changes the workaround to use the
built-in, recursive std::conjunction/std::disjunction instead.
This also removes the `count_t` since `any_of_t` and `all_of_t` were the
only things using it.
This commit also rearranges some of the future std imports to use actual
`std` implementations for C++14/17 features when under the appropriate
compiler mode, as we were already doing for a few things (like
index_sequence). Most of these aren't saving much (the implementation
for enable_if_t, for example, is trivial), but I think it makes the
intention of the code instantly clear. It also enables MSVC's native
std::index_sequence support.
2016-12-12 23:11:49 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// Index sequences
|
|
|
|
|
#if defined(PYBIND11_CPP14) || defined(_MSC_VER)
|
2016-11-27 19:56:04 +00:00
|
|
|
|
using std::index_sequence;
|
|
|
|
|
using std::make_index_sequence;
|
|
|
|
|
#else
|
2015-07-26 14:33:49 +00:00
|
|
|
|
template<size_t ...> struct index_sequence { };
|
2016-11-27 19:56:04 +00:00
|
|
|
|
template<size_t N, size_t ...S> struct make_index_sequence_impl : make_index_sequence_impl <N - 1, N - 1, S...> { };
|
|
|
|
|
template<size_t ...S> struct make_index_sequence_impl <0, S...> { typedef index_sequence<S...> type; };
|
|
|
|
|
template<size_t N> using make_index_sequence = typename make_index_sequence_impl<N>::type;
|
|
|
|
|
#endif
|
2015-07-26 14:33:49 +00:00
|
|
|
|
|
2017-03-17 13:51:41 +00:00
|
|
|
|
/// Backports of std::bool_constant and std::negation to accomodate older compilers
|
Change all_of_t/any_of_t to all_of/any_of, add none_of
This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
with previous use of `all_of_t<Pred, Ts...>` becoming
`all_of<Pred<Ts>...>` (and similarly for `any_of_t`). It also adds a
`none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
in cases where several predicates need to be tested for the same type
instead of the same predicate for multiple types.
This commit replaces the implementation with a more efficient version
for non-MSVC. For MSVC, this changes the workaround to use the
built-in, recursive std::conjunction/std::disjunction instead.
This also removes the `count_t` since `any_of_t` and `all_of_t` were the
only things using it.
This commit also rearranges some of the future std imports to use actual
`std` implementations for C++14/17 features when under the appropriate
compiler mode, as we were already doing for a few things (like
index_sequence). Most of these aren't saving much (the implementation
for enable_if_t, for example, is trivial), but I think it makes the
intention of the code instantly clear. It also enables MSVC's native
std::index_sequence support.
2016-12-12 23:11:49 +00:00
|
|
|
|
template <bool B> using bool_constant = std::integral_constant<bool, B>;
|
2017-03-17 13:51:41 +00:00
|
|
|
|
template <typename T> struct negation : bool_constant<!T::value> { };
|
Change all_of_t/any_of_t to all_of/any_of, add none_of
This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
with previous use of `all_of_t<Pred, Ts...>` becoming
`all_of<Pred<Ts>...>` (and similarly for `any_of_t`). It also adds a
`none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
in cases where several predicates need to be tested for the same type
instead of the same predicate for multiple types.
This commit replaces the implementation with a more efficient version
for non-MSVC. For MSVC, this changes the workaround to use the
built-in, recursive std::conjunction/std::disjunction instead.
This also removes the `count_t` since `any_of_t` and `all_of_t` were the
only things using it.
This commit also rearranges some of the future std imports to use actual
`std` implementations for C++14/17 features when under the appropriate
compiler mode, as we were already doing for a few things (like
index_sequence). Most of these aren't saving much (the implementation
for enable_if_t, for example, is trivial), but I think it makes the
intention of the code instantly clear. It also enables MSVC's native
std::index_sequence support.
2016-12-12 23:11:49 +00:00
|
|
|
|
|
2017-01-31 15:54:08 +00:00
|
|
|
|
/// Compile-time all/any/none of that check the boolean value of all template types
|
2017-03-17 03:41:09 +00:00
|
|
|
|
#ifdef __cpp_fold_expressions
|
2016-12-13 17:23:12 +00:00
|
|
|
|
template <class... Ts> using all_of = bool_constant<(Ts::value && ...)>;
|
|
|
|
|
template <class... Ts> using any_of = bool_constant<(Ts::value || ...)>;
|
|
|
|
|
#elif !defined(_MSC_VER)
|
Change all_of_t/any_of_t to all_of/any_of, add none_of
This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
with previous use of `all_of_t<Pred, Ts...>` becoming
`all_of<Pred<Ts>...>` (and similarly for `any_of_t`). It also adds a
`none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
in cases where several predicates need to be tested for the same type
instead of the same predicate for multiple types.
This commit replaces the implementation with a more efficient version
for non-MSVC. For MSVC, this changes the workaround to use the
built-in, recursive std::conjunction/std::disjunction instead.
This also removes the `count_t` since `any_of_t` and `all_of_t` were the
only things using it.
This commit also rearranges some of the future std imports to use actual
`std` implementations for C++14/17 features when under the appropriate
compiler mode, as we were already doing for a few things (like
index_sequence). Most of these aren't saving much (the implementation
for enable_if_t, for example, is trivial), but I think it makes the
intention of the code instantly clear. It also enables MSVC's native
std::index_sequence support.
2016-12-12 23:11:49 +00:00
|
|
|
|
template <bool...> struct bools {};
|
|
|
|
|
template <class... Ts> using all_of = std::is_same<
|
|
|
|
|
bools<Ts::value..., true>,
|
|
|
|
|
bools<true, Ts::value...>>;
|
|
|
|
|
template <class... Ts> using any_of = negation<all_of<negation<Ts>...>>;
|
|
|
|
|
#else
|
|
|
|
|
// MSVC has trouble with the above, but supports std::conjunction, which we can use instead (albeit
|
|
|
|
|
// at a slight loss of compilation efficiency).
|
|
|
|
|
template <class... Ts> using all_of = std::conjunction<Ts...>;
|
|
|
|
|
template <class... Ts> using any_of = std::disjunction<Ts...>;
|
|
|
|
|
#endif
|
|
|
|
|
template <class... Ts> using none_of = negation<any_of<Ts...>>;
|
|
|
|
|
|
Numpy: better compilation errors, long double support (#619)
* Clarify PYBIND11_NUMPY_DTYPE documentation
The current documentation and example reads as though
PYBIND11_NUMPY_DTYPE is a declarative macro along the same lines as
PYBIND11_DECLARE_HOLDER_TYPE, but it isn't. The changes the
documentation and docs example to make it clear that you need to "call"
the macro.
* Add satisfies_{all,any,none}_of<T, Preds>
`satisfies_all_of<T, Pred1, Pred2, Pred3>` is a nice legibility-enhanced
shortcut for `is_all<Pred1<T>, Pred2<T>, Pred3<T>>`.
* Give better error message for non-POD dtype attempts
If you try to use a non-POD data type, you get difficult-to-interpret
compilation errors (about ::name() not being a member of an internal
pybind11 struct, among others), for which isn't at all obvious what the
problem is.
This adds a static_assert for such cases.
It also changes the base case from an empty struct to the is_pod_struct
case by no longer using `enable_if<is_pod_struct>` but instead using a
static_assert: thus specializations avoid the base class, POD types
work, and non-POD types (and unimplemented POD types like std::array)
get a more informative static_assert failure.
* Prefix macros with PYBIND11_
numpy.h uses unprefixed macros, which seems undesirable. This prefixes
them with PYBIND11_ to match all the other macros in numpy.h (and
elsewhere).
* Add long double support
This adds long double and std::complex<long double> support for numpy
arrays.
This allows some simplification of the code used to generate format
descriptors; the new code uses fewer macros, instead putting the code as
different templated options; the template conditions end up simpler with
this because we are now supporting all basic C++ arithmetic types (and
so can use is_arithmetic instead of is_integral + multiple
different specializations).
In addition to testing that it is indeed working in the test script, it
also adds various offset and size calculations there, which
fixes the test failures under x86 compilations.
2017-01-31 16:00:15 +00:00
|
|
|
|
template <class T, template<class> class... Predicates> using satisfies_all_of = all_of<Predicates<T>...>;
|
|
|
|
|
template <class T, template<class> class... Predicates> using satisfies_any_of = any_of<Predicates<T>...>;
|
|
|
|
|
template <class T, template<class> class... Predicates> using satisfies_none_of = none_of<Predicates<T>...>;
|
|
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
|
/// Strip the class from a method type
|
2016-05-03 11:28:40 +00:00
|
|
|
|
template <typename T> struct remove_class { };
|
2015-07-26 14:33:49 +00:00
|
|
|
|
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
|
2016-01-17 21:36:38 +00:00
|
|
|
|
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; };
|
Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```
In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-08-29 01:05:42 +00:00
|
|
|
|
template <typename T> using intrinsic_t = typename intrinsic_type<T>::type;
|
2015-07-26 14:33:49 +00:00
|
|
|
|
|
|
|
|
|
/// Helper type to replace 'void' in some expressions
|
|
|
|
|
struct void_type { };
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
2016-11-27 17:19:34 +00:00
|
|
|
|
/// Helper template which holds a list of types
|
|
|
|
|
template <typename...> struct type_list { };
|
|
|
|
|
|
Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```
In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-08-29 01:05:42 +00:00
|
|
|
|
/// Compile-time integer sum
|
2017-03-17 03:41:09 +00:00
|
|
|
|
#ifdef __cpp_fold_expressions
|
|
|
|
|
template <typename... Ts> constexpr size_t constexpr_sum(Ts... ns) { return (0 + ... + size_t{ns}); }
|
|
|
|
|
#else
|
Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```
In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-08-29 01:05:42 +00:00
|
|
|
|
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...); }
|
2017-03-17 03:41:09 +00:00
|
|
|
|
#endif
|
Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```
In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-08-29 01:05:42 +00:00
|
|
|
|
|
2017-01-23 00:12:24 +00:00
|
|
|
|
NAMESPACE_BEGIN(constexpr_impl)
|
|
|
|
|
/// Implementation details for constexpr functions
|
|
|
|
|
constexpr int first(int i) { return i; }
|
|
|
|
|
template <typename T, typename... Ts>
|
|
|
|
|
constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); }
|
|
|
|
|
|
|
|
|
|
constexpr int last(int /*i*/, int result) { return result; }
|
|
|
|
|
template <typename T, typename... Ts>
|
|
|
|
|
constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); }
|
|
|
|
|
NAMESPACE_END(constexpr_impl)
|
|
|
|
|
|
|
|
|
|
/// Return the index of the first type in Ts which satisfies Predicate<T>. Returns sizeof...(Ts) if
|
|
|
|
|
/// none match.
|
|
|
|
|
template <template<typename> class Predicate, typename... Ts>
|
|
|
|
|
constexpr int constexpr_first() { return constexpr_impl::first(0, Predicate<Ts>::value...); }
|
|
|
|
|
|
|
|
|
|
/// Return the index of the last type in Ts which satisfies Predicate<T>, or -1 if none match.
|
|
|
|
|
template <template<typename> class Predicate, typename... Ts>
|
|
|
|
|
constexpr int constexpr_last() { return constexpr_impl::last(0, -1, Predicate<Ts>::value...); }
|
|
|
|
|
|
2016-09-06 16:27:00 +00:00
|
|
|
|
// 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;
|
Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.
This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options. It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively. (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively). If any unmatched template arguments are provided, a
static assertion fails.
What this means is that you can specify or omit the arguments in any
order:
py::class_<A, PyA> c1(m, "A");
py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");
It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 16:17:06 +00:00
|
|
|
|
};
|
2016-09-06 16:27:00 +00:00
|
|
|
|
template <template<class> class Predicate, class Default, class T, class... Ts>
|
|
|
|
|
struct first_of<Predicate, Default, T, Ts...> {
|
Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.
This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options. It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively. (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively). If any unmatched template arguments are provided, a
static assertion fails.
What this means is that you can specify or omit the arguments in any
order:
py::class_<A, PyA> c1(m, "A");
py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");
It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 16:17:06 +00:00
|
|
|
|
using type = typename std::conditional<
|
|
|
|
|
Predicate<T>::value,
|
|
|
|
|
T,
|
2016-09-06 16:27:00 +00:00
|
|
|
|
typename first_of<Predicate, Default, Ts...>::type
|
Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.
This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options. It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively. (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively). If any unmatched template arguments are provided, a
static assertion fails.
What this means is that you can specify or omit the arguments in any
order:
py::class_<A, PyA> c1(m, "A");
py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");
It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 16:17:06 +00:00
|
|
|
|
>::type;
|
|
|
|
|
};
|
2016-09-06 16:27:00 +00:00
|
|
|
|
template <template<class> class Predicate, class Default, class... T> using first_of_t = typename first_of<Predicate, Default, T...>::type;
|
Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.
This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options. It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively. (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively). If any unmatched template arguments are provided, a
static assertion fails.
What this means is that you can specify or omit the arguments in any
order:
py::class_<A, PyA> c1(m, "A");
py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");
It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 16:17:06 +00:00
|
|
|
|
|
2016-09-04 19:30:08 +00:00
|
|
|
|
/// 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;
|
|
|
|
|
|
2016-09-24 21:54:02 +00:00
|
|
|
|
template <template<typename...> class Base>
|
|
|
|
|
struct is_template_base_of_impl {
|
|
|
|
|
template <typename... Us> static std::true_type check(Base<Us...> *);
|
|
|
|
|
static std::false_type check(...);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Check if a template is the base of a type. For example:
|
|
|
|
|
/// `is_template_base_of<Base, T>` is true if `struct T : Base<U> {}` where U can be anything
|
|
|
|
|
template <template<typename...> class Base, typename T>
|
|
|
|
|
#if !defined(_MSC_VER)
|
2017-01-14 16:25:28 +00:00
|
|
|
|
using is_template_base_of = decltype(is_template_base_of_impl<Base>::check((remove_cv_t<T>*)nullptr));
|
2016-09-24 21:54:02 +00:00
|
|
|
|
#else // MSVC2015 has trouble with decltype in template aliases
|
2017-01-14 16:25:28 +00:00
|
|
|
|
struct is_template_base_of : decltype(is_template_base_of_impl<Base>::check((remove_cv_t<T>*)nullptr)) { };
|
2016-09-24 21:54:02 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-10-18 11:56:33 +00:00
|
|
|
|
/// Check if T is std::shared_ptr<U> where U can be anything
|
|
|
|
|
template <typename T> struct is_shared_ptr : std::false_type { };
|
|
|
|
|
template <typename U> struct is_shared_ptr<std::shared_ptr<U>> : std::true_type { };
|
|
|
|
|
|
Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```
In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-08-29 01:05:42 +00:00
|
|
|
|
/// Ignore that a variable is unused in compiler warnings
|
|
|
|
|
inline void ignore_unused(const int *) { }
|
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
|
NAMESPACE_END(detail)
|
2015-10-01 14:46:03 +00:00
|
|
|
|
|
2016-10-31 14:11:10 +00:00
|
|
|
|
/// Returns a named pointer that is shared among all extension modules (using the same
|
|
|
|
|
/// pybind11 version) running in the current interpreter. Names starting with underscores
|
|
|
|
|
/// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
|
|
|
|
|
inline PYBIND11_NOINLINE void* get_shared_data(const std::string& name) {
|
|
|
|
|
auto& internals = detail::get_internals();
|
|
|
|
|
auto it = internals.shared_data.find(name);
|
|
|
|
|
return it != internals.shared_data.end() ? it->second : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the shared data that can be later recovered by `get_shared_data()`.
|
|
|
|
|
inline PYBIND11_NOINLINE void *set_shared_data(const std::string& name, void *data) {
|
|
|
|
|
detail::get_internals().shared_data[name] = data;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
|
|
|
|
|
/// such entry exists. Otherwise, a new object of default-constructible type `T` is
|
|
|
|
|
/// added to the shared data under the given name and a reference to it is returned.
|
|
|
|
|
template<typename T> T& get_or_create_shared_data(const std::string& name) {
|
|
|
|
|
auto& internals = detail::get_internals();
|
|
|
|
|
auto it = internals.shared_data.find(name);
|
|
|
|
|
T* ptr = (T*) (it != internals.shared_data.end() ? it->second : nullptr);
|
|
|
|
|
if (!ptr) {
|
|
|
|
|
ptr = new T();
|
|
|
|
|
internals.shared_data[name] = ptr;
|
|
|
|
|
}
|
|
|
|
|
return *ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-10 09:58:02 +00:00
|
|
|
|
/// 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);
|
|
|
|
|
}
|
2016-11-24 11:30:11 +00:00
|
|
|
|
|
|
|
|
|
error_already_set(const error_already_set &) = delete;
|
|
|
|
|
|
|
|
|
|
error_already_set(error_already_set &&e)
|
|
|
|
|
: std::runtime_error(e.what()), type(e.type), value(e.value),
|
|
|
|
|
trace(e.trace) { e.type = e.value = e.trace = nullptr; }
|
|
|
|
|
|
2016-11-24 22:11:02 +00:00
|
|
|
|
inline ~error_already_set(); // implementation in pybind11.h
|
2016-11-24 17:52:31 +00:00
|
|
|
|
|
|
|
|
|
error_already_set& operator=(const error_already_set &) = delete;
|
2016-09-10 09:58:02 +00:00
|
|
|
|
|
|
|
|
|
/// Give the error back to Python
|
|
|
|
|
void restore() { PyErr_Restore(type, value, trace); type = value = trace = nullptr; }
|
|
|
|
|
|
2017-02-08 19:23:56 +00:00
|
|
|
|
/// Clear the held Python error state (the C++ `what()` message remains intact)
|
|
|
|
|
void clear() { restore(); PyErr_Clear(); }
|
|
|
|
|
|
2016-09-10 09:58:02 +00:00
|
|
|
|
private:
|
|
|
|
|
PyObject *type, *value, *trace;
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-10 12:43:28 +00:00
|
|
|
|
/// C++ bindings of builtin Python exceptions
|
|
|
|
|
class builtin_exception : public std::runtime_error {
|
|
|
|
|
public:
|
|
|
|
|
using std::runtime_error::runtime_error;
|
2017-01-31 15:54:08 +00:00
|
|
|
|
/// Set the error using the Python C API
|
|
|
|
|
virtual void set_error() const = 0;
|
2016-09-10 12:43:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#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()); } \
|
2016-05-03 11:28:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-09-10 12:43:28 +00:00
|
|
|
|
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
|
2015-10-01 14:46:03 +00:00
|
|
|
|
|
2016-04-30 21:55:44 +00:00
|
|
|
|
[[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); }
|
2016-01-17 21:36:41 +00:00
|
|
|
|
|
2016-05-04 20:22:48 +00:00
|
|
|
|
template <typename T, typename SFINAE = void> struct format_descriptor { };
|
2016-06-19 13:39:41 +00:00
|
|
|
|
|
Numpy: better compilation errors, long double support (#619)
* Clarify PYBIND11_NUMPY_DTYPE documentation
The current documentation and example reads as though
PYBIND11_NUMPY_DTYPE is a declarative macro along the same lines as
PYBIND11_DECLARE_HOLDER_TYPE, but it isn't. The changes the
documentation and docs example to make it clear that you need to "call"
the macro.
* Add satisfies_{all,any,none}_of<T, Preds>
`satisfies_all_of<T, Pred1, Pred2, Pred3>` is a nice legibility-enhanced
shortcut for `is_all<Pred1<T>, Pred2<T>, Pred3<T>>`.
* Give better error message for non-POD dtype attempts
If you try to use a non-POD data type, you get difficult-to-interpret
compilation errors (about ::name() not being a member of an internal
pybind11 struct, among others), for which isn't at all obvious what the
problem is.
This adds a static_assert for such cases.
It also changes the base case from an empty struct to the is_pod_struct
case by no longer using `enable_if<is_pod_struct>` but instead using a
static_assert: thus specializations avoid the base class, POD types
work, and non-POD types (and unimplemented POD types like std::array)
get a more informative static_assert failure.
* Prefix macros with PYBIND11_
numpy.h uses unprefixed macros, which seems undesirable. This prefixes
them with PYBIND11_ to match all the other macros in numpy.h (and
elsewhere).
* Add long double support
This adds long double and std::complex<long double> support for numpy
arrays.
This allows some simplification of the code used to generate format
descriptors; the new code uses fewer macros, instead putting the code as
different templated options; the template conditions end up simpler with
this because we are now supporting all basic C++ arithmetic types (and
so can use is_arithmetic instead of is_integral + multiple
different specializations).
In addition to testing that it is indeed working in the test script, it
also adds various offset and size calculations there, which
fixes the test failures under x86 compilations.
2017-01-31 16:00:15 +00:00
|
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
// Returns the index of the given type in the type char array below, and in the list in numpy.h
|
|
|
|
|
// The order here is: bool; 8 ints ((signed,unsigned)x(8,16,32,64)bits); float,double,long double;
|
|
|
|
|
// complex float,double,long double. Note that the long double types only participate when long
|
|
|
|
|
// double is actually longer than double (it isn't under MSVC).
|
|
|
|
|
// NB: not only the string below but also complex.h and numpy.h rely on this order.
|
|
|
|
|
template <typename T, typename SFINAE = void> struct is_fmt_numeric { static constexpr bool value = false; };
|
|
|
|
|
template <typename T> struct is_fmt_numeric<T, enable_if_t<std::is_arithmetic<T>::value>> {
|
|
|
|
|
static constexpr bool value = true;
|
|
|
|
|
static constexpr int index = std::is_same<T, bool>::value ? 0 : 1 + (
|
|
|
|
|
std::is_integral<T>::value ? detail::log2(sizeof(T))*2 + std::is_unsigned<T>::value : 8 + (
|
|
|
|
|
std::is_same<T, double>::value ? 1 : std::is_same<T, long double>::value ? 2 : 0));
|
|
|
|
|
};
|
|
|
|
|
NAMESPACE_END(detail)
|
|
|
|
|
|
|
|
|
|
template <typename T> struct format_descriptor<T, detail::enable_if_t<detail::is_fmt_numeric<T>::value>> {
|
|
|
|
|
static constexpr const char c = "?bBhHiIqQfdgFDG"[detail::is_fmt_numeric<T>::index];
|
2016-09-27 09:23:52 +00:00
|
|
|
|
static constexpr const char value[2] = { c, '\0' };
|
|
|
|
|
static std::string format() { return std::string(1, c); }
|
2016-05-04 20:22:48 +00:00
|
|
|
|
};
|
2016-06-19 13:39:41 +00:00
|
|
|
|
|
2016-05-04 20:22:48 +00:00
|
|
|
|
template <typename T> constexpr const char format_descriptor<
|
Numpy: better compilation errors, long double support (#619)
* Clarify PYBIND11_NUMPY_DTYPE documentation
The current documentation and example reads as though
PYBIND11_NUMPY_DTYPE is a declarative macro along the same lines as
PYBIND11_DECLARE_HOLDER_TYPE, but it isn't. The changes the
documentation and docs example to make it clear that you need to "call"
the macro.
* Add satisfies_{all,any,none}_of<T, Preds>
`satisfies_all_of<T, Pred1, Pred2, Pred3>` is a nice legibility-enhanced
shortcut for `is_all<Pred1<T>, Pred2<T>, Pred3<T>>`.
* Give better error message for non-POD dtype attempts
If you try to use a non-POD data type, you get difficult-to-interpret
compilation errors (about ::name() not being a member of an internal
pybind11 struct, among others), for which isn't at all obvious what the
problem is.
This adds a static_assert for such cases.
It also changes the base case from an empty struct to the is_pod_struct
case by no longer using `enable_if<is_pod_struct>` but instead using a
static_assert: thus specializations avoid the base class, POD types
work, and non-POD types (and unimplemented POD types like std::array)
get a more informative static_assert failure.
* Prefix macros with PYBIND11_
numpy.h uses unprefixed macros, which seems undesirable. This prefixes
them with PYBIND11_ to match all the other macros in numpy.h (and
elsewhere).
* Add long double support
This adds long double and std::complex<long double> support for numpy
arrays.
This allows some simplification of the code used to generate format
descriptors; the new code uses fewer macros, instead putting the code as
different templated options; the template conditions end up simpler with
this because we are now supporting all basic C++ arithmetic types (and
so can use is_arithmetic instead of is_integral + multiple
different specializations).
In addition to testing that it is indeed working in the test script, it
also adds various offset and size calculations there, which
fixes the test failures under x86 compilations.
2017-01-31 16:00:15 +00:00
|
|
|
|
T, detail::enable_if_t<detail::is_fmt_numeric<T>::value>>::value[2];
|
2016-06-19 13:39:41 +00:00
|
|
|
|
|
2016-11-21 17:40:43 +00:00
|
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
|
|
|
|
|
template <typename T, typename SFINAE = void> struct compare_buffer_info {
|
|
|
|
|
static bool compare(const buffer_info& b) {
|
|
|
|
|
return b.format == format_descriptor<T>::format() && b.itemsize == sizeof(T);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename T> struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
|
|
|
|
|
static bool compare(const buffer_info& b) {
|
|
|
|
|
return b.itemsize == sizeof(T) && (b.format == format_descriptor<T>::value ||
|
|
|
|
|
((sizeof(T) == sizeof(long)) && b.format == (std::is_unsigned<T>::value ? "L" : "l")) ||
|
|
|
|
|
((sizeof(T) == sizeof(size_t)) && b.format == (std::is_unsigned<T>::value ? "N" : "n")));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NAMESPACE_END(detail)
|
|
|
|
|
|
2016-09-10 07:32:17 +00:00
|
|
|
|
/// 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); }
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-28 00:03:15 +00:00
|
|
|
|
/// Dummy destructor wrapper that can be used to expose classes with a private destructor
|
|
|
|
|
struct nodelete { template <typename T> void operator()(T*) { } };
|
2016-08-27 18:57:55 +00:00
|
|
|
|
|
2016-12-08 10:07:52 +00:00
|
|
|
|
// overload_cast requires variable templates: C++14 or MSVC 2015 Update 2
|
|
|
|
|
#if defined(PYBIND11_CPP14) || _MSC_FULL_VER >= 190023918
|
|
|
|
|
#define PYBIND11_OVERLOAD_CAST 1
|
|
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
template <typename... Args>
|
|
|
|
|
struct overload_cast_impl {
|
2017-02-17 11:56:41 +00:00
|
|
|
|
template <typename Return>
|
|
|
|
|
constexpr auto operator()(Return (*pf)(Args...)) const noexcept
|
2016-12-08 10:07:52 +00:00
|
|
|
|
-> decltype(pf) { return pf; }
|
|
|
|
|
|
2017-02-17 11:56:41 +00:00
|
|
|
|
template <typename Return, typename Class>
|
|
|
|
|
constexpr auto operator()(Return (Class::*pmf)(Args...), std::false_type = {}) const noexcept
|
2016-12-08 10:07:52 +00:00
|
|
|
|
-> decltype(pmf) { return pmf; }
|
|
|
|
|
|
2017-02-17 11:56:41 +00:00
|
|
|
|
template <typename Return, typename Class>
|
|
|
|
|
constexpr auto operator()(Return (Class::*pmf)(Args...) const, std::true_type) const noexcept
|
2016-12-08 10:07:52 +00:00
|
|
|
|
-> decltype(pmf) { return pmf; }
|
|
|
|
|
};
|
|
|
|
|
NAMESPACE_END(detail)
|
|
|
|
|
|
|
|
|
|
/// Syntax sugar for resolving overloaded function pointers:
|
|
|
|
|
/// - regular: static_cast<Return (Class::*)(Arg0, Arg1, Arg2)>(&Class::func)
|
|
|
|
|
/// - sweet: overload_cast<Arg0, Arg1, Arg2>(&Class::func)
|
|
|
|
|
template <typename... Args>
|
|
|
|
|
static constexpr detail::overload_cast_impl<Args...> overload_cast = {};
|
|
|
|
|
// MSVC 2015 only accepts this particular initialization syntax for this variable template.
|
|
|
|
|
|
|
|
|
|
/// Const member function selector for overload_cast
|
|
|
|
|
/// - regular: static_cast<Return (Class::*)(Arg) const>(&Class::func)
|
|
|
|
|
/// - sweet: overload_cast<Arg>(&Class::func, const_)
|
|
|
|
|
static constexpr auto const_ = std::true_type{};
|
|
|
|
|
|
|
|
|
|
#endif // overload_cast
|
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
|
NAMESPACE_END(pybind11)
|