Commit 7040b90d authored by PyBind11 Upstream's avatar PyBind11 Upstream Committed by Atkins, Charles Vernon
Browse files

pybind11 2018-02-07 (f117a48e)

Code extracted from:

    https://github.com/pybind/pybind11.git

at commit f117a48ea2fd446d2865826a58d08027d4579cb3 (v2.2.2).

Upstream Shortlog
-----------------

Antony Lee (1):
      7ab1cd34 Remove spurious quote in error message. (#1202)

Elliott Sales de Andrade (1):
      53e0aa03 Update PyPI URLs.

Jason Rhinelander (17):
      fe1266e0 Fix `char &` arguments being non-bindable
      a28393cf Fix 2D Nx1/1xN inputs to eigen dense vector args
      6519388f Build /permissive- under VS2017
      fbb2ef7e Miscellaneous travis-ci updates/fixes
      fc8d1c76 Fix new flake8 E741 error from using `l` variable
      07dc73da __qualname__ and nested class naming fixes (#1171)
      3793c7ed Silence new MSVC C++17 deprecation warnings
      c715c70e Fix premature destruction of args/kwargs arguments
      0c7aec48 Simplify arg copying
      8310aa46 Added py::args ref counting tests
      7f170fe4 Fixes for numpy 1.14.0 compatibility
      c8f07b5d Use a named rather than anon struct in instance
      20d6d1d4 Fix segfault when reloading interpreter with external modules (#1092)
      56c1edb4 Don't add duplicate patients
      c3d81d23 Use stricter brace initialization
      f99f6851 MSVC workaround for broken `using detail::_` warning
      19e90dc3 Updated version/changelog for 2.2.2

Jeff VanOss (1):
      17ad517d fix return from std::map bindings to __delitem__ (#1229)

Unknown (1):
      09579726 Trivial typos

Wenzel Jakob (4):
      2406a0cf added citation reference (fixes #767) (#1189)
      83e73091 Moved section on licensing of contributions (fixes #1109) (#1188)
      ebe16361 Fix pybind11 interoperability with Clang trunk
      f117a48e mark release date in changelog

Zach DeVito (1):
      155cc7c4 Fix leak in var arg handling

luz.paz (1):
      ed0a72eb misc. typos
parent 940b1aeb
......@@ -34,6 +34,7 @@ install:
if ($env:APPVEYOR_JOB_NAME -like "*Visual Studio 2017*") {
$env:CMAKE_GENERATOR = "Visual Studio 15 2017"
$env:CMAKE_INCLUDE_PATH = "C:\Libraries\boost_1_64_0"
$env:CXXFLAGS = "-permissive-"
} else {
$env:CMAKE_GENERATOR = "Visual Studio 14 2015"
}
......
......@@ -100,22 +100,22 @@ before_install:
if [ "$TRAVIS_OS_NAME" = "linux" ]; then
if [ -n "$CLANG" ]; then
export CXX=clang++-$CLANG CC=clang-$CLANG
COMPILER_PACKAGES="clang-$CLANG llvm-$CLANG-dev"
EXTRA_PACKAGES+=" clang-$CLANG llvm-$CLANG-dev"
else
if [ -z "$GCC" ]; then GCC=4.8
else COMPILER_PACKAGES=g++-$GCC
else EXTRA_PACKAGES+=" g++-$GCC"
fi
export CXX=g++-$GCC CC=gcc-$GCC
fi
if [ "$GCC" = "6" ]; then DOCKER=${ARCH:+$ARCH/}debian:stretch
elif [ "$GCC" = "7" ]; then DOCKER=debian:buster
elif [ "$GCC" = "7" ]; then DOCKER=debian:buster EXTRA_PACKAGES+=" catch" DOWNLOAD_CATCH=OFF
fi
elif [ "$TRAVIS_OS_NAME" = "osx" ]; then
export CXX=clang++ CC=clang;
fi
if [ -n "$CPP" ]; then CPP=-std=c++$CPP; fi
if [ "${PYTHON:0:1}" = "3" ]; then PY=3; fi
if [ -n "$DEBUG" ]; then CMAKE_EXTRA_ARGS="${CMAKE_EXTRA_ARGS} -DCMAKE_BUILD_TYPE=Debug"; fi
if [ -n "$DEBUG" ]; then CMAKE_EXTRA_ARGS+=" -DCMAKE_BUILD_TYPE=Debug"; fi
- |
# Initialize environment
set -e
......@@ -133,7 +133,7 @@ before_install:
if [ "$PYPY" = "5.8" ]; then
curl -fSL https://bitbucket.org/pypy/pypy/downloads/pypy2-v5.8.0-linux64.tar.bz2 | tar xj
PY_CMD=$(echo `pwd`/pypy2-v5.8.0-linux64/bin/pypy)
CMAKE_EXTRA_ARGS="${CMAKE_EXTRA_ARGS} -DPYTHON_EXECUTABLE:FILEPATH=$PY_CMD"
CMAKE_EXTRA_ARGS+=" -DPYTHON_EXECUTABLE:FILEPATH=$PY_CMD"
else
PY_CMD=python$PYTHON
if [ "$TRAVIS_OS_NAME" = "osx" ]; then
......@@ -157,12 +157,12 @@ install:
if [ -n "$DOCKER" ]; then
if [ -n "$DEBUG" ]; then
PY_DEBUG="python$PYTHON-dbg python$PY-scipy-dbg"
CMAKE_EXTRA_ARGS="${CMAKE_EXTRA_ARGS} -DPYTHON_EXECUTABLE=/usr/bin/python${PYTHON}dm"
CMAKE_EXTRA_ARGS+=" -DPYTHON_EXECUTABLE=/usr/bin/python${PYTHON}dm"
fi
$SCRIPT_RUN_PREFIX sh -c "for s in 0 15; do sleep \$s; \
apt-get -qy --no-install-recommends install \
$PY_DEBUG python$PYTHON-dev python$PY-pytest python$PY-scipy \
libeigen3-dev libboost-dev cmake make ${COMPILER_PACKAGES} && break; done"
libeigen3-dev libboost-dev cmake make ${EXTRA_PACKAGES} && break; done"
else
if [ "$CLANG" = "5.0" ]; then
......@@ -195,7 +195,7 @@ install:
wget -q -O eigen.tar.gz https://bitbucket.org/eigen/eigen/get/3.3.3.tar.gz
tar xzf eigen.tar.gz
export CMAKE_INCLUDE_PATH="${CMAKE_INCLUDE_PATH:+:}$PWD/eigen-eigen-67e894c6cd8f"
export CMAKE_INCLUDE_PATH="${CMAKE_INCLUDE_PATH:+$CMAKE_INCLUDE_PATH:}$PWD/eigen-eigen-67e894c6cd8f"
fi
set +e
script:
......@@ -203,7 +203,7 @@ script:
-DPYBIND11_PYTHON_VERSION=$PYTHON
-DPYBIND11_CPP_STANDARD=$CPP
-DPYBIND11_WERROR=${WERROR:-ON}
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_CATCH=${DOWNLOAD_CATCH:-ON}
- $SCRIPT_RUN_PREFIX make pytest -j 2
- $SCRIPT_RUN_PREFIX make cpptest -j 2
- if [ -n "$CMAKE" ]; then $SCRIPT_RUN_PREFIX make test_cmake_build; fi
......
......@@ -30,8 +30,18 @@ adhere to the following rules to make the process as smooth as possible:
* This project has a strong focus on providing general solutions using a
minimal amount of code, thus small pull requests are greatly preferred.
### License
### Licensing of contributions
pybind11 is provided under a BSD-style license that can be found in the
``LICENSE`` file. By using, distributing, or contributing to this project, you
agree to the terms and conditions of this license.
You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
("Enhancements") to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to the author of this software, without
imposing a separate written license agreement for such Enhancements, then you
hereby grant the following license: a non-exclusive, royalty-free perpetual
license to install, use, modify, prepare derivative works, incorporate into
other computer software, distribute, and sublicense such enhancements or
derivative works thereof, in binary and source code form.
......@@ -25,12 +25,5 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
("Enhancements") to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to the author of this software, without
imposing a separate written license agreement for such Enhancements, then you
hereby grant the following license: a non-exclusive, royalty-free perpetual
license to install, use, modify, prepare derivative works, incorporate into
other computer software, distribute, and sublicense such enhancements or
derivative works thereof, in binary and source code form.
Please also refer to the file CONTRIBUTING.md, which clarifies licensing of
external contributions to this project including patches, pull requests, etc.
......@@ -41,7 +41,7 @@ consideration: by default, numpy matrices and eigen matrices are *not* storage
compatible.
If the numpy matrix cannot be used as is (either because its types differ, e.g.
passing an array of integers to an Eigen paramater requiring doubles, or
passing an array of integers to an Eigen parameter requiring doubles, or
because the storage is incompatible), pybind11 makes a temporary copy and
passes the copy instead.
......@@ -89,7 +89,7 @@ as dictated by the binding function's return value policy (see the
documentation on :ref:`return_value_policies` for full details). That means,
without an explicit return value policy, lvalue references will be copied and
pointers will be managed by pybind11. In order to avoid copying, you should
explictly specify an appropriate return value policy, as in the following
explicitly specify an appropriate return value policy, as in the following
example:
.. code-block:: cpp
......@@ -287,7 +287,7 @@ On the other hand, pybind11 allows you to pass 1-dimensional arrays of length N
as Eigen parameters. If the Eigen type can hold a column vector of length N it
will be passed as such a column vector. If not, but the Eigen type constraints
will accept a row vector, it will be passed as a row vector. (The column
vector takes precendence when both are supported, for example, when passing a
vector takes precedence when both are supported, for example, when passing a
1D numpy array to a MatrixXd argument). Note that the type need not be
expicitly a vector: it is permitted to pass a 1D numpy array of size 5 to an
Eigen ``Matrix<double, Dynamic, 5>``: you would end up with a 1x5 Eigen matrix.
......
......@@ -138,5 +138,5 @@ section.
error return without exception set``.
Exceptions that you do not plan to handle should simply not be caught, or
may be explicity (re-)thrown to delegate it to the other,
may be explicitly (re-)thrown to delegate it to the other,
previously-declared existing exception translators.
......@@ -126,7 +126,7 @@ targeted arguments can be passed through the :class:`cpp_function` constructor:
.. warning::
Code with invalid return value policies might access unitialized memory or
Code with invalid return value policies might access uninitialized memory or
free data structures multiple times, which can lead to hard-to-debug
non-determinism and segmentation faults, hence it is worth spending the
time to understand all the different options in the table above.
......@@ -473,7 +473,7 @@ Overload resolution order
When a function or method with multiple overloads is called from Python,
pybind11 determines which overload to call in two passes. The first pass
attempts to call each overload without allowing argument conversion (as if
every argument had been specified as ``py::arg().noconvert()`` as decribed
every argument had been specified as ``py::arg().noconvert()`` as described
above).
If no overload succeeds in the no-conversion first pass, a second pass is
......
......@@ -6,10 +6,70 @@ Changelog
Starting with version 1.8.0, pybind11 releases use a `semantic versioning
<http://semver.org>`_ policy.
v2.3.0 (Not yet released)
v2.2.2 (February 7, 2018)
-----------------------------------------------------
* TBD
* Fixed a segfault when combining embedded interpreter
shutdown/reinitialization with external loaded pybind11 modules.
`#1092 <https://github.com/pybind/pybind11/pull/1092>`_.
* Eigen support: fixed a bug where Nx1/1xN numpy inputs couldn't be passed as
arguments to Eigen vectors (which for Eigen are simply compile-time fixed
Nx1/1xN matrices).
`#1106 <https://github.com/pybind/pybind11/pull/1106>`_.
* Clarified to license by moving the licensing of contributions from
``LICENSE`` into ``CONTRIBUTING.md``: the licensing of contributions is not
actually part of the software license as distributed. This isn't meant to be
a substantial change in the licensing of the project, but addresses concerns
that the clause made the license non-standard.
`#1109 <https://github.com/pybind/pybind11/issues/1109>`_.
* Fixed a regression introduced in 2.1 that broke binding functions with lvalue
character literal arguments.
`#1128 <https://github.com/pybind/pybind11/pull/1128>`_.
* MSVC: fix for compilation failures under /permissive-, and added the flag to
the appveyor test suite.
`#1155 <https://github.com/pybind/pybind11/pull/1155>`_.
* Fixed ``__qualname__`` generation, and in turn, fixes how class names
(especially nested class names) are shown in generated docstrings.
`#1171 <https://github.com/pybind/pybind11/pull/1171>`_.
* Updated the FAQ with a suggested project citation reference.
`#1189 <https://github.com/pybind/pybind11/pull/1189>`_.
* Added fixes for deprecation warnings when compiled under C++17 with
``-Wdeprecated`` turned on, and add ``-Wdeprecated`` to the test suite
compilation flags.
`#1191 <https://github.com/pybind/pybind11/pull/1191>`_.
* Fixed outdated PyPI URLs in ``setup.py``.
`#1213 <https://github.com/pybind/pybind11/pull/1213>`_.
* Fixed a refcount leak for arguments that end up in a ``py::args`` argument
for functions with both fixed positional and ``py::args`` arguments.
`#1216 <https://github.com/pybind/pybind11/pull/1216>`_.
* Fixed a potential segfault resulting from possible premature destruction of
``py::args``/``py::kwargs`` arguments with overloaded functions.
`#1223 <https://github.com/pybind/pybind11/pull/1223>`_.
* Fixed ``del map[item]`` for a ``stl_bind.h`` bound stl map.
`#1229 <https://github.com/pybind/pybind11/pull/1229>`_.
* Fixed a regression from v2.1.x where the aggregate initialization could
unintentionally end up at a constructor taking a templated
``std::initializer_list<T>`` argument.
`#1249 <https://github.com/pybind/pybind11/pull/1249>`_.
* Fixed an issue where calling a function with a keep_alive policy on the same
nurse/patient pair would cause the internal patient storage to needlessly
grow (unboundedly, if the nurse is long-lived).
`#1251 <https://github.com/pybind/pybind11/issues/1251>`_.
* Various other minor fixes.
v2.2.1 (September 14, 2017)
-----------------------------------------------------
......
......@@ -63,7 +63,7 @@ author = 'Wenzel Jakob'
# The short X.Y version.
version = '2.2'
# The full version, including alpha/beta/rc tags.
release = '2.2.1'
release = '2.2.2'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
......
......@@ -271,3 +271,19 @@ Common gotchas to watch out for involve not ``free()``-ing memory region
that that were ``malloc()``-ed in another shared library, using data
structures with incompatible ABIs, and so on. pybind11 is very careful not
to make these types of mistakes.
How to cite this project?
=========================
We suggest the following BibTeX template to cite pybind11 in scientific
discourse:
.. code-block:: bash
@misc{pybind11,
author = {Wenzel Jakob and Jason Rhinelander and Dean Moldovan},
year = {2017},
note = {https://github.com/pybind/pybind11},
title = {pybind11 -- Seamless operability between C++11 and Python}
}
......@@ -1216,6 +1216,7 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
using StringCaster = type_caster<StringType>;
StringCaster str_caster;
bool none = false;
CharT one_char = 0;
public:
bool load(handle src, bool convert) {
if (!src) return false;
......@@ -1243,7 +1244,7 @@ public:
}
operator CharT*() { return none ? nullptr : const_cast<CharT *>(static_cast<StringType &>(str_caster).c_str()); }
operator CharT() {
operator CharT&() {
if (none)
throw value_error("Cannot convert None to a character");
......@@ -1267,7 +1268,8 @@ public:
if (char0_bytes == str_len) {
// If we have a 128-255 value, we can decode it into a single char:
if (char0_bytes == 2 && (v0 & 0xFC) == 0xC0) { // 0x110000xx 0x10xxxxxx
return static_cast<CharT>(((v0 & 3) << 6) + (static_cast<unsigned char>(value[1]) & 0x3F));
one_char = static_cast<CharT>(((v0 & 3) << 6) + (static_cast<unsigned char>(value[1]) & 0x3F));
return one_char;
}
// Otherwise we have a single character, but it's > U+00FF
throw value_error("Character code point not in range(0x100)");
......@@ -1278,19 +1280,20 @@ public:
// surrogate pair with total length 2 instantly indicates a range error (but not a "your
// string was too long" error).
else if (StringCaster::UTF_N == 16 && str_len == 2) {
char16_t v0 = static_cast<char16_t>(value[0]);
if (v0 >= 0xD800 && v0 < 0xE000)
one_char = static_cast<CharT>(value[0]);
if (one_char >= 0xD800 && one_char < 0xE000)
throw value_error("Character code point not in range(0x10000)");
}
if (str_len != 1)
throw value_error("Expected a character, but multi-character string found");
return value[0];
one_char = value[0];
return one_char;
}
static PYBIND11_DESCR name() { return type_descr(_(PYBIND11_STRING_NAME)); }
template <typename _T> using cast_op_type = remove_reference_t<pybind11::detail::cast_op_type<_T>>;
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
// Base implementation for std::tuple and std::pair
......@@ -1414,7 +1417,7 @@ protected:
bool load_value(value_and_holder &&v_h) {
if (v_h.holder_constructed()) {
value = v_h.value_ptr();
holder = v_h.holder<holder_type>();
holder = v_h.template holder<holder_type>();
return true;
} else {
throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
......@@ -1574,7 +1577,7 @@ template <typename T, typename SFINAE> type_caster<T, SFINAE> &load_type(type_ca
throw cast_error("Unable to cast Python instance to C++ type (compile in debug mode for details)");
#else
throw cast_error("Unable to cast Python instance of type " +
(std::string) str(handle.get_type()) + " to C++ type '" + type_id<T>() + "''");
(std::string) str(handle.get_type()) + " to C++ type '" + type_id<T>() + "'");
#endif
}
return conv;
......@@ -1683,6 +1686,9 @@ template <> inline void cast_safe<void>(object &&) {}
NAMESPACE_END(detail)
template <return_value_policy policy = return_value_policy::automatic_reference>
tuple make_tuple() { return tuple(0); }
template <return_value_policy policy = return_value_policy::automatic_reference,
typename... Args> tuple make_tuple(Args&&... args_) {
constexpr size_t size = sizeof...(Args);
......@@ -1799,6 +1805,10 @@ struct function_call {
/// The `convert` value the arguments should be loaded with
std::vector<bool> args_convert;
/// Extra references for the optional `py::args` and/or `py::kwargs` arguments (which, if
/// present, are also in `args` but without a reference).
object args_ref, kwargs_ref;
/// The parent, if any
handle parent;
......
......@@ -14,6 +14,15 @@
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
NAMESPACE_BEGIN(detail)
#if PY_VERSION_HEX >= 0x03030000
# define PYBIND11_BUILTIN_QUALNAME
# define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
#else
// In pre-3.3 Python, we still set __qualname__ so that we can produce reliable function type
// signatures; in 3.3+ this macro expands to nothing:
# define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) setattr((PyObject *) obj, "__qualname__", nameobj)
#endif
inline PyTypeObject *type_incref(PyTypeObject *type) {
Py_INCREF(type);
return type;
......@@ -48,7 +57,7 @@ inline PyTypeObject *make_static_property_type() {
pybind11_fail("make_static_property_type(): error allocating type!");
heap_type->ht_name = name_obj.inc_ref().ptr();
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
#ifdef PYBIND11_BUILTIN_QUALNAME
heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif
......@@ -63,6 +72,7 @@ inline PyTypeObject *make_static_property_type() {
pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
return type;
}
......@@ -161,7 +171,7 @@ inline PyTypeObject* make_default_metaclass() {
pybind11_fail("make_default_metaclass(): error allocating metaclass!");
heap_type->ht_name = name_obj.inc_ref().ptr();
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
#ifdef PYBIND11_BUILTIN_QUALNAME
heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif
......@@ -179,6 +189,7 @@ inline PyTypeObject* make_default_metaclass() {
pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
return type;
}
......@@ -278,9 +289,13 @@ extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject
inline void add_patient(PyObject *nurse, PyObject *patient) {
auto &internals = get_internals();
auto instance = reinterpret_cast<detail::instance *>(nurse);
auto &current_patients = internals.patients[nurse];
instance->has_patients = true;
for (auto &p : current_patients)
if (p == patient)
return;
Py_INCREF(patient);
internals.patients[nurse].push_back(patient);
current_patients.push_back(patient);
}
inline void clear_patients(PyObject *self) {
......@@ -363,7 +378,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
pybind11_fail("make_object_base_type(): error allocating type!");
heap_type->ht_name = name_obj.inc_ref().ptr();
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
#ifdef PYBIND11_BUILTIN_QUALNAME
heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif
......@@ -384,6 +399,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
pybind11_fail("PyType_Ready failed in make_object_base_type():" + error_string());
setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
return (PyObject *) heap_type;
......@@ -504,13 +520,15 @@ inline void enable_buffer_protocol(PyHeapTypeObject *heap_type) {
inline PyObject* make_new_python_type(const type_record &rec) {
auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec.name));
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
auto ht_qualname = name;
if (rec.scope && hasattr(rec.scope, "__qualname__")) {
ht_qualname = reinterpret_steal<object>(
auto qualname = name;
if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) {
#if PY_MAJOR_VERSION >= 3
qualname = reinterpret_steal<object>(
PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr()));
}
#else
qualname = str(rec.scope.attr("__qualname__").cast<std::string>() + "." + rec.name);
#endif
}
object module;
if (rec.scope) {
......@@ -552,8 +570,8 @@ inline PyObject* make_new_python_type(const type_record &rec) {
pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
heap_type->ht_name = name.release().ptr();
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
heap_type->ht_qualname = ht_qualname.release().ptr();
#ifdef PYBIND11_BUILTIN_QUALNAME
heap_type->ht_qualname = qualname.inc_ref().ptr();
#endif
auto type = &heap_type->ht_type;
......@@ -599,6 +617,8 @@ inline PyObject* make_new_python_type(const type_record &rec) {
if (module) // Needed by pydoc
setattr((PyObject *) type, "__module__", module);
PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
return (PyObject *) type;
}
......
......@@ -93,7 +93,7 @@
#define PYBIND11_VERSION_MAJOR 2
#define PYBIND11_VERSION_MINOR 2
#define PYBIND11_VERSION_PATCH 1
#define PYBIND11_VERSION_PATCH 2
/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode
#if defined(_MSC_VER)
......@@ -377,16 +377,18 @@ constexpr size_t instance_simple_holder_in_ptrs() {
struct type_info;
struct value_and_holder;
struct nonsimple_values_and_holders {
void **values_and_holders;
uint8_t *status;
};
/// The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
struct instance {
PyObject_HEAD
/// Storage for pointers and holder; see simple_layout, below, for a description
union {
void *simple_value_holder[1 + instance_simple_holder_in_ptrs()];
struct {
void **values_and_holders;
uint8_t *status;
} nonsimple;
nonsimple_values_and_holders nonsimple;
};
/// Weak references (needed for keep alive):
PyObject *weakrefs;
......@@ -471,7 +473,7 @@ template <size_t... IPrev, size_t I, bool B, bool... Bs> struct select_indices_i
: select_indices_impl<conditional_t<B, index_sequence<IPrev..., I>, index_sequence<IPrev...>>, I + 1, Bs...> {};
template <bool... Bs> using select_indices = typename select_indices_impl<index_sequence<>, 0, Bs...>::type;
/// Backports of std::bool_constant and std::negation to accomodate older compilers
/// Backports of std::bool_constant and std::negation to accommodate older compilers
template <bool B> using bool_constant = std::integral_constant<bool, B>;
template <typename T> struct negation : bool_constant<!T::value> { };
......
......@@ -52,6 +52,16 @@ bool is_alias(Cpp<Class> *ptr) {
template <typename /*Class*/>
constexpr bool is_alias(void *) { return false; }
// Constructs and returns a new object; if the given arguments don't map to a constructor, we fall
// back to brace aggregate initiailization so that for aggregate initialization can be used with
// py::init, e.g. `py::init<int, int>` to initialize a `struct T { int a; int b; }`. For
// non-aggregate types, we need to use an ordinary T(...) constructor (invoking as `T{...}` usually
// works, but will not do the expected thing when `T` has an `initializer_list<T>` constructor).
template <typename Class, typename... Args, detail::enable_if_t<std::is_constructible<Class, Args...>::value, int> = 0>
inline Class *construct_or_initialize(Args &&...args) { return new Class(std::forward<Args>(args)...); }
template <typename Class, typename... Args, detail::enable_if_t<!std::is_constructible<Class, Args...>::value, int> = 0>
inline Class *construct_or_initialize(Args &&...args) { return new Class{std::forward<Args>(args)...}; }
// Attempts to constructs an alias using a `Alias(Cpp &&)` constructor. This allows types with
// an alias to provide only a single Cpp factory function as long as the Alias can be
// constructed from an rvalue reference of the base Cpp type. This means that Alias classes
......@@ -161,7 +171,7 @@ struct constructor {
template <typename Class, typename... Extra, enable_if_t<!Class::has_alias, int> = 0>
static void execute(Class &cl, const Extra&... extra) {
cl.def("__init__", [](value_and_holder &v_h, Args... args) {
v_h.value_ptr() = new Cpp<Class>{std::forward<Args>(args)...};
v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
}, is_new_style_constructor(), extra...);
}
......@@ -171,9 +181,9 @@ struct constructor {
static void execute(Class &cl, const Extra&... extra) {
cl.def("__init__", [](value_and_holder &v_h, Args... args) {
if (Py_TYPE(v_h.inst) == v_h.type->type)
v_h.value_ptr() = new Cpp<Class>{std::forward<Args>(args)...};
v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
else
v_h.value_ptr() = new Alias<Class>{std::forward<Args>(args)...};
v_h.value_ptr() = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
}, is_new_style_constructor(), extra...);
}
......@@ -182,7 +192,7 @@ struct constructor {
!std::is_constructible<Cpp<Class>, Args...>::value, int> = 0>
static void execute(Class &cl, const Extra&... extra) {
cl.def("__init__", [](value_and_holder &v_h, Args... args) {
v_h.value_ptr() = new Alias<Class>{std::forward<Args>(args)...};
v_h.value_ptr() = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
}, is_new_style_constructor(), extra...);
}
};
......@@ -193,7 +203,7 @@ template <typename... Args> struct alias_constructor {
enable_if_t<Class::has_alias && std::is_constructible<Alias<Class>, Args...>::value, int> = 0>
static void execute(Class &cl, const Extra&... extra) {
cl.def("__init__", [](value_and_holder &v_h, Args... args) {
v_h.value_ptr() = new Alias<Class>{std::forward<Args>(args)...};
v_h.value_ptr() = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
}, is_new_style_constructor(), extra...);
}
};
......
......@@ -127,21 +127,21 @@ struct type_info {
/// Each module locally stores a pointer to the `internals` data. The data
/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
inline internals *&get_internals_ptr() {
static internals *internals_ptr = nullptr;
return internals_ptr;
inline internals **&get_internals_pp() {
static internals **internals_pp = nullptr;
return internals_pp;
}
/// Return a reference to the current `internals` data
PYBIND11_NOINLINE inline internals &get_internals() {
auto *&internals_ptr = get_internals_ptr();
if (internals_ptr)
return *internals_ptr;
auto **&internals_pp = get_internals_pp();
if (internals_pp && *internals_pp)
return **internals_pp;
constexpr auto *id = PYBIND11_INTERNALS_ID;
auto builtins = handle(PyEval_GetBuiltins());
if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
internals_ptr = *static_cast<internals **>(capsule(builtins[id]));
internals_pp = static_cast<internals **>(capsule(builtins[id]));
// We loaded builtins through python's builtins, which means that our `error_already_set`
// and `builtin_exception` may be different local classes than the ones set up in the
......@@ -149,7 +149,7 @@ PYBIND11_NOINLINE inline internals &get_internals() {
//
// libstdc++ doesn't require this (types there are identified only by name)
#if !defined(__GLIBCXX__)
internals_ptr->registered_exception_translators.push_front(
(*internals_pp)->registered_exception_translators.push_front(
[](std::exception_ptr p) -> void {
try {
if (p) std::rethrow_exception(p);
......@@ -160,6 +160,8 @@ PYBIND11_NOINLINE inline internals &get_internals() {
);
#endif
} else {
if (!internals_pp) internals_pp = new internals*();</