Compare commits

...

9 Commits

Author SHA1 Message Date
Ashley Whetter e5121d726e
Merge d12fef38ae into 1f8b4a7f1a 2024-09-20 23:44:49 -07:00
Hintay 1f8b4a7f1a
fix(cmake): `NO_EXTRAS` in `pybind11_add_module` function partially working (#5378) 2024-09-19 11:24:35 -04:00
dependabot[bot] ad9fd39e14
chore(deps): bump pypa/cibuildwheel in the actions group (#5376)
Bumps the actions group with 1 update: [pypa/cibuildwheel](https://github.com/pypa/cibuildwheel).


Updates `pypa/cibuildwheel` from 2.20 to 2.21
- [Release notes](https://github.com/pypa/cibuildwheel/releases)
- [Changelog](https://github.com/pypa/cibuildwheel/blob/main/docs/changelog.md)
- [Commits](https://github.com/pypa/cibuildwheel/compare/v2.20...v2.21)

---
updated-dependencies:
- dependency-name: pypa/cibuildwheel
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-17 10:19:17 -07:00
vfdev 1d9483ff73
Added exception translator specific mutex used with try_translate_exceptions (#5362)
* Added exception translator specific mutex used with try_translate_exceptions
Fixes #5346

* - Replaced with_internals_for_exception_translator by with_exception_translators
- Incremented PYBIND11_INTERNALS_VERSION
- Added a test

* style: pre-commit fixes

* Fixed formatting and added explicit to ctors

* Addressed PR review comments

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-09-17 09:47:20 -07:00
Bobby R. Bruce a7910be630
Add warn disable for GGC 12 bound checking error (#5355)
Issue: #5224

The `internals.registered_types_py...` line in pybind11.h triggers a
false-positive bounds checking warning in GCC 12.

This is discussed in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115824.

The workaround implemented is taken from suggestions then refactored to
use the `PYBIND11_WARNING_PUSH` and `PYBIND11_WARNING_POP` MACROS.
2024-09-14 23:51:50 -07:00
Henry Schreiner 0cf3a0f7b5
ci: PyPI attestations (#5374) 2024-09-13 20:21:43 -04:00
Henry Schreiner 5b7c0b04b9
docs: update changelog for 2.13.6 (#5372)
* docs: update changelog for 2.13.6

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* docs: mention supported versions

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2024-09-13 16:59:50 -04:00
Ralf W. Grosse-Kunstleve ef5a9560bb
Enable type-safe interoperability between different independent Python/C++ bindings systems. (#5296)
* `self.__cpp_transporter__()` proof of concept: Enable passing C++ pointers across extensions even if the `PYBIND11_INTERNALS_VERSION`s do not match.

* Include cleanup (mainly to resolve PyPy build failures).

* Fix clang-tidy errors.

* Resolve `error: extra

* factor out platform_abi_id.h from internals.h (no functional changes)

* factor out internals_version.h from internals.h (no functional changes)

* Update CMakeLists.txt, tests/extra_python_package/test_files.py

* Revert "factor out internals_version.h from internals.h (no functional changes)"

This reverts commit 3ccea8cd43.

* Remove internals_version.h from CMakeLists.txt, tests/extra_python_package/test_files.py

* `.__cpp_transporter__()` implementation: compare `pybind11_platform_abi_id`, `cpp_typeid_name`

* Add PremiumTraveler

* Rename test_cpp_transporter_traveler_type.h -> test_cpp_transporter_traveler_types.h

* Expand tests: `PremiumTraveler`, `get_points()`

* Shuffle order of tests (no real changes).

* Move `__cpp_transporter__` lambda to `py::cpp_transporter()` regular function.

* Use `type_caster_generic::load(self)` instead of `cast<T *>(self)`

* Pass `const std::type_info *` via `py::capsule` (instead of `cpp_typeid_name`).

* Make platform_abi_id.h completely stand-alone.

* rename exo_planet.cpp -> exo_planet_pybind11.cpp

* Add exo_planet_c_api.cpp (incomplete).

* Fix silly oversight (wrong filename in `#include`).

* Resolve clang-tidy errors:

```
/__w/pybind11/pybind11/tests/exo_planet_c_api.cpp:10:18: error: 'wrapGetLuggage' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace,-warnings-as-errors]
   10 | static PyObject *wrapGetLuggage(PyObject *, PyObject *) { return PyUnicode_FromString("TODO"); }
      | ~~~~~~           ^
/__w/pybind11/pybind11/tests/exo_planet_c_api.cpp:14:20: error: 'ThisMethodDef' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace,-warnings-as-errors]
   14 | static PyMethodDef ThisMethodDef[]
      | ~~~~~~             ^
/__w/pybind11/pybind11/tests/exo_planet_c_api.cpp:17:27: error: 'ThisModuleDef' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace,-warnings-as-errors]
   17 | static struct PyModuleDef ThisModuleDef = {
      | ~~~~~~                    ^
```

* Implement exo_planet_c_api GetLuggage(), GetPoints()

* Move new code from test_cpp_transporter_traveler_bindings.h to pybind11/detail/type_caster_base.h, under the name `class_dunder_cpp_transporter()`

* Fix oversight.

* Unconditionally add `__cpp_transporter__` method to all `py::class_` objects, but do not include that magic method in docstring signatures.

* Back out pybind11/detail/platform_abi_id.h for now. Maximizing reusability can be handled separately, later.

* Small cleanup.

* Restore and add to `test_call_cpp_transporter_*()`

* Ensure https://github.com/pybind/pybind11/issues/3788 does not bite again.

* `class_dunder_cpp_transporter()`: replace `obj.cast<std::string>()` with `std::string(obj)`

* Add (simple) copyright notices in all newly added files.

* Globally replace cpp_transporter with cpp_conduit

* style: pre-commit fixes

* IWYU fixes

* Rename `class_dunder_cpp_conduit()` -> `cpp_conduit_method()`

* Change `pybind11_platform_abi_id`, `pointer_kind` argument types from `str` to `bytes`.

This avoids the unicode decode/encode roundtrips:

* More robust (no decode/encode errors).

* Minor runtime optimization.

* Systematically rename `cap_cpp_type_info` -> `cpp_type_info_capsule` (no functional changes).

* Systematically replace `cpp_type_info_capsule` `name`: `"const std::type_info *"` -> `typeid(std::type_info).name()` (this IS a functional change).

This provides an extra layer of protection against C++ ABI mismatches:

* The first and most important layer is that the `PYBIND11_PLATFORM_ABI_ID`s must match between extensions.

* The second layer is that the `typeid(std::type_info).name()`s must match between extensions.

* Fix sort order accident in tests/CMakeLists.txt

* Apply suggestions from code review

Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>

* style: pre-commit fixes

* refactor: rename to _pybind_conduit_v1_

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Add test_home_planet_wrap_very_lonely_traveler(), test_exo_planet_pybind11_wrap_very_lonely_traveler()

* Resolve clang-tidy errors:

```
/__w/pybind11/pybind11/tests/test_cpp_conduit_traveler_bindings.h:39:32: error: parameter 'm' is passed by value and only copied once; consider moving it to avoid unnecessary copies [performance-unnecessary-value-param,-warnings-as-errors]
   10 |     py::class_<LonelyTraveler>(m, "LonelyTraveler");
      |                                ^
      |                                std::move( )
/__w/pybind11/pybind11/tests/test_cpp_conduit_traveler_bindings.h:43:52: error: parameter 'm' is passed by value and only copied once; consider moving it to avoid unnecessary copies [performance-unnecessary-value-param,-warnings-as-errors]
   43 |     py::class_<VeryLonelyTraveler, LonelyTraveler>(m, "VeryLonelyTraveler");
      |                                                    ^
      |                                                    std::move( )
```

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
2024-09-13 00:18:29 -04:00
Ashley Whetter d12fef38ae Standard library enum support via an optional include 2020-11-29 20:22:12 -08:00
23 changed files with 852 additions and 34 deletions

View File

@ -22,7 +22,7 @@ jobs:
submodules: true
fetch-depth: 0
- uses: pypa/cibuildwheel@v2.20
- uses: pypa/cibuildwheel@v2.21
env:
PYODIDE_BUILD_EXPORTS: whole_archive
with:

View File

@ -91,11 +91,12 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'published'
needs: [packaging]
environment: pypi
environment:
name: pypi
url: https://pypi.org/p/pybind11
permissions:
id-token: write
attestations: write
contents: read
steps:
# Downloads all to directories matching the artifact names
@ -110,8 +111,10 @@ jobs:
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: standard/
attestations: true
- name: Publish global package
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: global/
attestations: true

View File

@ -129,6 +129,7 @@ endif()
set(PYBIND11_HEADERS
include/pybind11/detail/class.h
include/pybind11/detail/common.h
include/pybind11/detail/cpp_conduit.h
include/pybind11/detail/descr.h
include/pybind11/detail/init.h
include/pybind11/detail/internals.h

View File

@ -31,6 +31,37 @@ New Features:
* The ``array_caster`` in pybind11/stl.h was enhanced to support value types that are not default-constructible.
`#5305 <https://github.com/pybind/pybind11/pull/5305>`_
* Added ``py::warnings`` namespace with ``py::warnings::warn`` and ``py::warnings::new_warning_type`` that provides the interface for Python warnings.
`#5291 <https://github.com/pybind/pybind11/pull/5291>`_
Version 2.13.6 (September 13, 2024)
-----------------------------------
New Features:
* A new ``self._pybind11_conduit_v1_()`` method is automatically added to all
``py::class_``-wrapped types, to enable type-safe interoperability between
different independent Python/C++ bindings systems, including pybind11
versions with different ``PYBIND11_INTERNALS_VERSION``'s. Supported on
pybind11 2.11.2, 2.12.1, and 2.13.6+.
`#5296 <https://github.com/pybind/pybind11/pull/5296>`_
Bug fixes:
* Using ``__cpp_nontype_template_args`` instead of ``__cpp_nontype_template_parameter_class``.
`#5330 <https://github.com/pybind/pybind11/pull/5330>`_
* Properly translate C++ exception to Python exception when creating Python buffer from wrapped object.
`#5324 <https://github.com/pybind/pybind11/pull/5324>`_
Documentation:
* Adds an answer (FAQ) for "What is a highly conclusive and simple way to find memory leaks?".
`#5340 <https://github.com/pybind/pybind11/pull/5340>`_
Version 2.13.5 (August 22, 2024)
--------------------------------
@ -238,6 +269,18 @@ Other:
* Update docs and noxfile.
`#5071 <https://github.com/pybind/pybind11/pull/5071>`_
Version 2.12.1 (September 13, 2024)
-----------------------------------
New Features:
* A new ``self._pybind11_conduit_v1_()`` method is automatically added to all
``py::class_``-wrapped types, to enable type-safe interoperability between
different independent Python/C++ bindings systems, including pybind11
versions with different ``PYBIND11_INTERNALS_VERSION``'s. Supported on
pybind11 2.11.2, 2.12.1, and 2.13.6+.
`#5296 <https://github.com/pybind/pybind11/pull/5296>`_
Version 2.12.0 (March 27, 2024)
-------------------------------
@ -413,6 +456,18 @@ Other:
* An ``assert()`` was added to help Coverty avoid generating a false positive.
`#4817 <https://github.com/pybind/pybind11/pull/4817>`_
Version 2.11.2 (September 13, 2024)
-----------------------------------
New Features:
* A new ``self._pybind11_conduit_v1_()`` method is automatically added to all
``py::class_``-wrapped types, to enable type-safe interoperability between
different independent Python/C++ bindings systems, including pybind11
versions with different ``PYBIND11_INTERNALS_VERSION``'s. Supported on
pybind11 2.11.2, 2.12.1, and 2.13.6+.
`#5296 <https://github.com/pybind/pybind11/pull/5296>`_
Version 2.11.1 (July 17, 2023)
------------------------------

View File

@ -0,0 +1,77 @@
// Copyright (c) 2024 The pybind Community.
#pragma once
#include <pybind11/pytypes.h>
#include "common.h"
#include "internals.h"
#include <typeinfo>
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
// Forward declaration needed here: Refactoring opportunity.
extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *);
inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) {
#if defined(PYPY_VERSION)
auto &internals = get_internals();
return bool(internals.registered_types_py.find(type_obj)
!= internals.registered_types_py.end());
#else
return bool(type_obj->tp_new == pybind11_object_new);
#endif
}
inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
PyObject *descr = _PyType_Lookup(type_obj, attr_name);
return bool((descr != nullptr) && PyInstanceMethod_Check(descr));
}
inline object try_get_cpp_conduit_method(PyObject *obj) {
if (PyType_Check(obj)) {
return object();
}
PyTypeObject *type_obj = Py_TYPE(obj);
str attr_name("_pybind11_conduit_v1_");
bool assumed_to_be_callable = false;
if (type_is_managed_by_our_internals(type_obj)) {
if (!is_instance_method_of_type(type_obj, attr_name.ptr())) {
return object();
}
assumed_to_be_callable = true;
}
PyObject *method = PyObject_GetAttr(obj, attr_name.ptr());
if (method == nullptr) {
PyErr_Clear();
return object();
}
if (!assumed_to_be_callable && PyCallable_Check(method) == 0) {
Py_DECREF(method);
return object();
}
return reinterpret_steal<object>(method);
}
inline void *try_raw_pointer_ephemeral_from_cpp_conduit(handle src,
const std::type_info *cpp_type_info) {
object method = try_get_cpp_conduit_method(src.ptr());
if (method) {
capsule cpp_type_info_capsule(const_cast<void *>(static_cast<const void *>(cpp_type_info)),
typeid(std::type_info).name());
object cpp_conduit = method(bytes(PYBIND11_PLATFORM_ABI_ID),
cpp_type_info_capsule,
bytes("raw_pointer_ephemeral"));
if (isinstance<capsule>(cpp_conduit)) {
return reinterpret_borrow<capsule>(cpp_conduit).get_pointer();
}
}
return nullptr;
}
#define PYBIND11_HAS_CPP_CONDUIT 1
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@ -50,17 +50,17 @@ inline void try_translate_exceptions() {
- delegate translation to the next translator by throwing a new type of exception.
*/
bool handled = with_internals([&](internals &internals) {
auto &local_exception_translators = get_local_internals().registered_exception_translators;
if (detail::apply_exception_translators(local_exception_translators)) {
return true;
}
auto &exception_translators = internals.registered_exception_translators;
if (detail::apply_exception_translators(exception_translators)) {
return true;
}
return false;
});
bool handled = with_exception_translators(
[&](std::forward_list<ExceptionTranslator> &exception_translators,
std::forward_list<ExceptionTranslator> &local_exception_translators) {
if (detail::apply_exception_translators(local_exception_translators)) {
return true;
}
if (detail::apply_exception_translators(exception_translators)) {
return true;
}
return false;
});
if (!handled) {
set_error(PyExc_SystemError, "Exception escaped from default exception translator!");

View File

@ -39,7 +39,11 @@
# if PY_VERSION_HEX >= 0x030C0000 || defined(_MSC_VER)
// Version bump for Python 3.12+, before first 3.12 beta release.
// Version bump for MSVC piggy-backed on PR #4779. See comments there.
# define PYBIND11_INTERNALS_VERSION 5
# ifdef Py_GIL_DISABLED
# define PYBIND11_INTERNALS_VERSION 6
# else
# define PYBIND11_INTERNALS_VERSION 5
# endif
# else
# define PYBIND11_INTERNALS_VERSION 4
# endif
@ -177,6 +181,7 @@ static_assert(sizeof(instance_map_shard) % 64 == 0,
struct internals {
#ifdef Py_GIL_DISABLED
pymutex mutex;
pymutex exception_translator_mutex;
#endif
// std::type_index -> pybind11's type information
type_map<type_info *> registered_types_cpp;
@ -321,15 +326,17 @@ struct type_info {
# define PYBIND11_INTERNALS_KIND ""
#endif
#define PYBIND11_PLATFORM_ABI_ID \
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
PYBIND11_BUILD_TYPE
#define PYBIND11_INTERNALS_ID \
"__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB \
PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
PYBIND11_PLATFORM_ABI_ID "__"
#define PYBIND11_MODULE_LOCAL_ID \
"__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB \
PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
PYBIND11_PLATFORM_ABI_ID "__"
/// Each module locally stores a pointer to the `internals` data. The data
/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
@ -641,6 +648,19 @@ inline auto with_internals(const F &cb) -> decltype(cb(get_internals())) {
return cb(internals);
}
template <typename F>
inline auto with_exception_translators(const F &cb)
-> decltype(cb(get_internals().registered_exception_translators,
get_local_internals().registered_exception_translators)) {
auto &internals = get_internals();
#ifdef Py_GIL_DISABLED
std::unique_lock<pymutex> lock((internals).exception_translator_mutex);
#endif
auto &local_internals = get_local_internals();
return cb(internals.registered_exception_translators,
local_internals.registered_exception_translators);
}
inline std::uint64_t mix64(std::uint64_t z) {
// David Stafford's variant 13 of the MurmurHash3 finalizer popularized
// by the SplitMix PRNG.

View File

@ -12,14 +12,17 @@
#include <pybind11/pytypes.h>
#include "common.h"
#include "cpp_conduit.h"
#include "descr.h"
#include "internals.h"
#include "typeid.h"
#include "value_and_holder.h"
#include <cstdint>
#include <cstring>
#include <iterator>
#include <new>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <typeindex>
@ -611,6 +614,13 @@ public:
}
return false;
}
bool try_cpp_conduit(handle src) {
value = try_raw_pointer_ephemeral_from_cpp_conduit(src, cpptype);
if (value != nullptr) {
return true;
}
return false;
}
void check_holder_compat() {}
PYBIND11_NOINLINE static void *local_load(PyObject *src, const type_info *ti) {
@ -742,6 +752,10 @@ public:
return true;
}
if (convert && cpptype && this_.try_cpp_conduit(src)) {
return true;
}
return false;
}
@ -769,6 +783,32 @@ public:
void *value = nullptr;
};
inline object cpp_conduit_method(handle self,
const bytes &pybind11_platform_abi_id,
const capsule &cpp_type_info_capsule,
const bytes &pointer_kind) {
#ifdef PYBIND11_HAS_STRING_VIEW
using cpp_str = std::string_view;
#else
using cpp_str = std::string;
#endif
if (cpp_str(pybind11_platform_abi_id) != PYBIND11_PLATFORM_ABI_ID) {
return none();
}
if (std::strcmp(cpp_type_info_capsule.name(), typeid(std::type_info).name()) != 0) {
return none();
}
if (cpp_str(pointer_kind) != "raw_pointer_ephemeral") {
throw std::runtime_error("Invalid pointer_kind: \"" + std::string(pointer_kind) + "\"");
}
const auto *cpp_type_info = cpp_type_info_capsule.get_pointer<const std::type_info>();
type_caster_generic caster(*cpp_type_info);
if (!caster.load(self, false)) {
return none();
}
return capsule(caster.value, cpp_type_info->name());
}
/**
* Determine suitable casting operator for pointer-or-lvalue-casting type casters. The type caster
* needs to provide `operator T*()` and `operator T&()` operators.

View File

@ -592,7 +592,8 @@ protected:
int index = 0;
/* Create a nice pydoc rec including all signatures and
docstrings of the functions in the overload chain */
if (chain && options::show_function_signatures()) {
if (chain && options::show_function_signatures()
&& std::strcmp(rec->name, "_pybind11_conduit_v1_") != 0) {
// First a generic signature
signatures += rec->name;
signatures += "(*args, **kwargs)\n";
@ -601,7 +602,8 @@ protected:
// Then specific overload signatures
bool first_user_def = true;
for (auto *it = chain_start; it != nullptr; it = it->next) {
if (options::show_function_signatures()) {
if (options::show_function_signatures()
&& std::strcmp(rec->name, "_pybind11_conduit_v1_") != 0) {
if (index > 0) {
signatures += '\n';
}
@ -1380,7 +1382,17 @@ protected:
} else {
internals.registered_types_cpp[tindex] = tinfo;
}
PYBIND11_WARNING_PUSH
#if defined(__GNUC__) && __GNUC__ == 12
// When using GCC 12 these warnings are disabled as they trigger
// false positive warnings. Discussed here:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115824.
PYBIND11_WARNING_DISABLE_GCC("-Warray-bounds")
PYBIND11_WARNING_DISABLE_GCC("-Wstringop-overread")
#endif
internals.registered_types_py[(PyTypeObject *) m_ptr] = {tinfo};
PYBIND11_WARNING_POP
});
if (rec.bases.size() > 1 || rec.multiple_inheritance) {
@ -1601,6 +1613,7 @@ public:
= instances[std::type_index(typeid(type))];
});
}
def("_pybind11_conduit_v1_", cpp_conduit_method);
}
template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0>
@ -2573,10 +2586,12 @@ void implicitly_convertible() {
}
inline void register_exception_translator(ExceptionTranslator &&translator) {
detail::with_internals([&](detail::internals &internals) {
internals.registered_exception_translators.push_front(
std::forward<ExceptionTranslator>(translator));
});
detail::with_exception_translators(
[&](std::forward_list<ExceptionTranslator> &exception_translators,
std::forward_list<ExceptionTranslator> &local_exception_translators) {
(void) local_exception_translators;
exception_translators.push_front(std::forward<ExceptionTranslator>(translator));
});
}
/**
@ -2586,11 +2601,12 @@ inline void register_exception_translator(ExceptionTranslator &&translator) {
* the exception.
*/
inline void register_local_exception_translator(ExceptionTranslator &&translator) {
detail::with_internals([&](detail::internals &internals) {
(void) internals;
detail::get_local_internals().registered_exception_translators.push_front(
std::forward<ExceptionTranslator>(translator));
});
detail::with_exception_translators(
[&](std::forward_list<ExceptionTranslator> &exception_translators,
std::forward_list<ExceptionTranslator> &local_exception_translators) {
(void) exception_translators;
local_exception_translators.push_front(std::forward<ExceptionTranslator>(translator));
});
}
/**

View File

@ -0,0 +1,161 @@
/*
pybind11/stdlib_enum.h: Declaration and conversion enums as Enum objects.
Copyright (c) 2020 Ashley Whetter <ashley@awhetter.co.uk>
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
#pragma once
#include "detail/common.h"
#include "pybind11.h"
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
template<typename U>
struct enum_mapper {
handle type = {};
std::unordered_map<U, handle> values = {};
enum_mapper(handle type, const dict& values) : type(type) {
for (auto item : values) {
this->values[item.second.cast<U>()] = type.attr(item.first);
}
}
};
template<typename T>
struct type_caster<T, enable_if_t<std::is_enum<T>::value>> {
using underlying_type = typename std::underlying_type<T>::type;
private:
using base_caster = type_caster_base<T>;
using shared_info_type = typename std::unordered_map<std::type_index, void*>;
static enum_mapper<underlying_type>* enum_info() {
auto shared_enum_info = reinterpret_cast<shared_info_type*>(
get_shared_data("_stdlib_enum_internals")
);
if (shared_enum_info) {
auto it = shared_enum_info->find(std::type_index(typeid(T)));
if (it != shared_enum_info->end()) {
return reinterpret_cast<enum_mapper<underlying_type>*>(it->second);
}
}
return nullptr;
}
base_caster caster;
T value;
public:
template<typename U> using cast_op_type = pybind11::detail::cast_op_type<U>;
operator T*() { return enum_info() ? &value: static_cast<T*>(caster); }
operator T&() { return enum_info() ? value: static_cast<T&>(caster); }
static constexpr auto name = base_caster::name;
static handle cast(const T& src, return_value_policy policy, handle parent) {
enum_mapper<underlying_type>* info = enum_info();
if (info) {
auto it = info->values.find(static_cast<underlying_type>(src));
if (it != info->values.end()) {
return it->second.inc_ref();
}
}
return base_caster::cast(src, policy, parent);
}
bool load(handle src, bool convert) {
if (!src) {
return false;
}
enum_mapper<underlying_type>* info = enum_info();
if (info) {
if (!isinstance(src, info->type)) {
return false;
}
value = static_cast<T>(src.attr("value").cast<underlying_type>());
return true;
}
return caster.load(src, convert);
}
static void bind(handle type, const dict& values) {
enum_mapper<underlying_type>* info = enum_info();
delete info;
auto shared_enum_info = &get_or_create_shared_data<shared_info_type>("_stdlib_enum_internals");
(*shared_enum_info)[std::type_index(typeid(T))] = reinterpret_cast<void*>(
new enum_mapper<underlying_type>(type, values)
);
set_shared_data("_stdlib_enum_internals", shared_enum_info);
}
};
PYBIND11_NAMESPACE_END(detail)
template<typename T>
class stdlib_enum {
public:
using underlying_type = typename std::underlying_type<T>::type;
stdlib_enum(handle scope, const char* name)
: scope(scope), name(name)
{
kwargs["value"] = cast(name);
kwargs["names"] = entries;
if (scope) {
if (hasattr(scope, "__module__")) {
kwargs["module"] = scope.attr("__module__");
}
else if (hasattr(scope, "__name__")) {
kwargs["module"] = scope.attr("__name__");
}
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
if (hasattr(scope, "__qualname__")) {
kwargs["qualname"] = scope.attr("__qualname__").cast<std::string>() + "." + name;
}
#endif
}
}
~stdlib_enum() {
object ctor = module::import("enum").attr("Enum");
object unique = module::import("enum").attr("unique");
object type = unique(ctor(**kwargs));
setattr(scope, name, type);
detail::type_caster<T>::bind(type, entries);
}
stdlib_enum& value(const char* name, T value) & {
add_entry(name, value);
return *this;
}
stdlib_enum&& value(const char* name, T value) && {
add_entry(name, value);
return std::move(*this);
}
private:
handle scope;
const char* name;
dict entries;
dict kwargs;
void add_entry(const char* name, T value) {
entries[name] = cast(static_cast<underlying_type>(value));
}
};
PYBIND11_NAMESPACE_END(pybind11)

View File

@ -118,6 +118,7 @@ set(PYBIND11_TEST_FILES
test_const_name
test_constants_and_functions
test_copy_move
test_cpp_conduit
test_custom_type_casters
test_custom_type_setup
test_docstring_options
@ -218,6 +219,8 @@ tests_extra_targets("test_exceptions.py;test_local_bindings.py;test_stl.py;test_
# And add additional targets for other tests.
tests_extra_targets("test_exceptions.py" "cross_module_interleaved_error_already_set")
tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils")
tests_extra_targets("test_cpp_conduit.py"
"exo_planet_pybind11;exo_planet_c_api;home_planet_very_lonely_traveler")
set(PYBIND11_EIGEN_REPO
"https://gitlab.com/libeigen/eigen.git"

View File

@ -0,0 +1,10 @@
from __future__ import annotations
class PythonMyException7(Exception):
def __init__(self, message):
self.message = message
super().__init__(message)
def __str__(self):
return "[PythonMyException7]: " + self.message.a

103
tests/exo_planet_c_api.cpp Normal file
View File

@ -0,0 +1,103 @@
// Copyright (c) 2024 The pybind Community.
// THIS MUST STAY AT THE TOP!
#include <pybind11/pybind11.h> // EXCLUSIVELY for PYBIND11_PLATFORM_ABI_ID
// Potential future direction to maximize reusability:
// (e.g. for use from SWIG, Cython, PyCLIF, nanobind):
// #include <pybind11/compat/platform_abi_id.h>
// This would only depend on:
// 1. A C++ compiler, WITHOUT requiring -fexceptions.
// 2. Python.h
#include "test_cpp_conduit_traveler_types.h"
#include <Python.h>
#include <typeinfo>
namespace {
void *get_cpp_conduit_void_ptr(PyObject *py_obj, const std::type_info *cpp_type_info) {
PyObject *cpp_type_info_capsule
= PyCapsule_New(const_cast<void *>(static_cast<const void *>(cpp_type_info)),
typeid(std::type_info).name(),
nullptr);
if (cpp_type_info_capsule == nullptr) {
return nullptr;
}
PyObject *cpp_conduit = PyObject_CallMethod(py_obj,
"_pybind11_conduit_v1_",
"yOy",
PYBIND11_PLATFORM_ABI_ID,
cpp_type_info_capsule,
"raw_pointer_ephemeral");
Py_DECREF(cpp_type_info_capsule);
if (cpp_conduit == nullptr) {
return nullptr;
}
void *void_ptr = PyCapsule_GetPointer(cpp_conduit, cpp_type_info->name());
Py_DECREF(cpp_conduit);
if (PyErr_Occurred()) {
return nullptr;
}
return void_ptr;
}
template <typename T>
T *get_cpp_conduit_type_ptr(PyObject *py_obj) {
void *void_ptr = get_cpp_conduit_void_ptr(py_obj, &typeid(T));
if (void_ptr == nullptr) {
return nullptr;
}
return static_cast<T *>(void_ptr);
}
extern "C" PyObject *wrapGetLuggage(PyObject * /*self*/, PyObject *traveler) {
const auto *cpp_traveler
= get_cpp_conduit_type_ptr<pybind11_tests::test_cpp_conduit::Traveler>(traveler);
if (cpp_traveler == nullptr) {
return nullptr;
}
return PyUnicode_FromString(cpp_traveler->luggage.c_str());
}
extern "C" PyObject *wrapGetPoints(PyObject * /*self*/, PyObject *premium_traveler) {
const auto *cpp_premium_traveler
= get_cpp_conduit_type_ptr<pybind11_tests::test_cpp_conduit::PremiumTraveler>(
premium_traveler);
if (cpp_premium_traveler == nullptr) {
return nullptr;
}
return PyLong_FromLong(static_cast<long>(cpp_premium_traveler->points));
}
PyMethodDef ThisMethodDef[] = {{"GetLuggage", wrapGetLuggage, METH_O, nullptr},
{"GetPoints", wrapGetPoints, METH_O, nullptr},
{nullptr, nullptr, 0, nullptr}};
struct PyModuleDef ThisModuleDef = {
PyModuleDef_HEAD_INIT, // m_base
"exo_planet_c_api", // m_name
nullptr, // m_doc
-1, // m_size
ThisMethodDef, // m_methods
nullptr, // m_slots
nullptr, // m_traverse
nullptr, // m_clear
nullptr // m_free
};
} // namespace
#if defined(WIN32) || defined(_WIN32)
# define EXO_PLANET_C_API_EXPORT __declspec(dllexport)
#else
# define EXO_PLANET_C_API_EXPORT __attribute__((visibility("default")))
#endif
extern "C" EXO_PLANET_C_API_EXPORT PyObject *PyInit_exo_planet_c_api() {
PyObject *m = PyModule_Create(&ThisModuleDef);
if (m == nullptr) {
return nullptr;
}
return m;
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2024 The pybind Community.
#if defined(PYBIND11_INTERNALS_VERSION)
# undef PYBIND11_INTERNALS_VERSION
#endif
#define PYBIND11_INTERNALS_VERSION 900000001
#include "test_cpp_conduit_traveler_bindings.h"
namespace pybind11_tests {
namespace test_cpp_conduit {
PYBIND11_MODULE(exo_planet_pybind11, m) {
wrap_traveler(m);
m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); });
}
} // namespace test_cpp_conduit
} // namespace pybind11_tests

View File

@ -53,6 +53,7 @@ main_headers = {
detail_headers = {
"include/pybind11/detail/class.h",
"include/pybind11/detail/common.h",
"include/pybind11/detail/cpp_conduit.h",
"include/pybind11/detail/descr.h",
"include/pybind11/detail/init.h",
"include/pybind11/detail/internals.h",

View File

@ -0,0 +1,13 @@
// Copyright (c) 2024 The pybind Community.
#include "test_cpp_conduit_traveler_bindings.h"
namespace pybind11_tests {
namespace test_cpp_conduit {
PYBIND11_MODULE(home_planet_very_lonely_traveler, m) {
m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); });
}
} // namespace test_cpp_conduit
} // namespace pybind11_tests

View File

@ -0,0 +1,22 @@
// Copyright (c) 2024 The pybind Community.
#include "pybind11_tests.h"
#include "test_cpp_conduit_traveler_bindings.h"
#include <typeinfo>
namespace pybind11_tests {
namespace test_cpp_conduit {
TEST_SUBMODULE(cpp_conduit, m) {
m.attr("PYBIND11_PLATFORM_ABI_ID") = py::bytes(PYBIND11_PLATFORM_ABI_ID);
m.attr("cpp_type_info_capsule_Traveler")
= py::capsule(&typeid(Traveler), typeid(std::type_info).name());
m.attr("cpp_type_info_capsule_int") = py::capsule(&typeid(int), typeid(std::type_info).name());
wrap_traveler(m);
wrap_lonely_traveler(m);
}
} // namespace test_cpp_conduit
} // namespace pybind11_tests

162
tests/test_cpp_conduit.py Normal file
View File

@ -0,0 +1,162 @@
# Copyright (c) 2024 The pybind Community.
from __future__ import annotations
import exo_planet_c_api
import exo_planet_pybind11
import home_planet_very_lonely_traveler
import pytest
from pybind11_tests import cpp_conduit as home_planet
def test_traveler_getattr_actually_exists():
t_h = home_planet.Traveler("home")
assert t_h.any_name == "Traveler GetAttr: any_name luggage: home"
def test_premium_traveler_getattr_actually_exists():
t_h = home_planet.PremiumTraveler("home", 7)
assert t_h.secret_name == "PremiumTraveler GetAttr: secret_name points: 7"
def test_call_cpp_conduit_success():
t_h = home_planet.Traveler("home")
cap = t_h._pybind11_conduit_v1_(
home_planet.PYBIND11_PLATFORM_ABI_ID,
home_planet.cpp_type_info_capsule_Traveler,
b"raw_pointer_ephemeral",
)
assert cap.__class__.__name__ == "PyCapsule"
def test_call_cpp_conduit_platform_abi_id_mismatch():
t_h = home_planet.Traveler("home")
cap = t_h._pybind11_conduit_v1_(
home_planet.PYBIND11_PLATFORM_ABI_ID + b"MISMATCH",
home_planet.cpp_type_info_capsule_Traveler,
b"raw_pointer_ephemeral",
)
assert cap is None
def test_call_cpp_conduit_cpp_type_info_capsule_mismatch():
t_h = home_planet.Traveler("home")
cap = t_h._pybind11_conduit_v1_(
home_planet.PYBIND11_PLATFORM_ABI_ID,
home_planet.cpp_type_info_capsule_int,
b"raw_pointer_ephemeral",
)
assert cap is None
def test_call_cpp_conduit_pointer_kind_invalid():
t_h = home_planet.Traveler("home")
with pytest.raises(
RuntimeError, match='^Invalid pointer_kind: "raw_pointer_ephemreal"$'
):
t_h._pybind11_conduit_v1_(
home_planet.PYBIND11_PLATFORM_ABI_ID,
home_planet.cpp_type_info_capsule_Traveler,
b"raw_pointer_ephemreal",
)
def test_home_only_basic():
t_h = home_planet.Traveler("home")
assert t_h.luggage == "home"
assert home_planet.get_luggage(t_h) == "home"
def test_home_only_premium():
p_h = home_planet.PremiumTraveler("home", 2)
assert p_h.luggage == "home"
assert home_planet.get_luggage(p_h) == "home"
assert home_planet.get_points(p_h) == 2
def test_exo_only_basic():
t_e = exo_planet_pybind11.Traveler("exo")
assert t_e.luggage == "exo"
assert exo_planet_pybind11.get_luggage(t_e) == "exo"
def test_exo_only_premium():
p_e = exo_planet_pybind11.PremiumTraveler("exo", 3)
assert p_e.luggage == "exo"
assert exo_planet_pybind11.get_luggage(p_e) == "exo"
assert exo_planet_pybind11.get_points(p_e) == 3
def test_home_passed_to_exo_basic():
t_h = home_planet.Traveler("home")
assert exo_planet_pybind11.get_luggage(t_h) == "home"
def test_exo_passed_to_home_basic():
t_e = exo_planet_pybind11.Traveler("exo")
assert home_planet.get_luggage(t_e) == "exo"
def test_home_passed_to_exo_premium():
p_h = home_planet.PremiumTraveler("home", 2)
assert exo_planet_pybind11.get_luggage(p_h) == "home"
assert exo_planet_pybind11.get_points(p_h) == 2
def test_exo_passed_to_home_premium():
p_e = exo_planet_pybind11.PremiumTraveler("exo", 3)
assert home_planet.get_luggage(p_e) == "exo"
assert home_planet.get_points(p_e) == 3
@pytest.mark.parametrize(
"traveler_type", [home_planet.Traveler, exo_planet_pybind11.Traveler]
)
def test_exo_planet_c_api_traveler(traveler_type):
t = traveler_type("socks")
assert exo_planet_c_api.GetLuggage(t) == "socks"
@pytest.mark.parametrize(
"premium_traveler_type",
[home_planet.PremiumTraveler, exo_planet_pybind11.PremiumTraveler],
)
def test_exo_planet_c_api_premium_traveler(premium_traveler_type):
pt = premium_traveler_type("gucci", 5)
assert exo_planet_c_api.GetLuggage(pt) == "gucci"
assert exo_planet_c_api.GetPoints(pt) == 5
def test_home_planet_wrap_very_lonely_traveler():
# This does not exercise the cpp_conduit feature, but is here to
# demonstrate that the cpp_conduit feature does not solve all
# cross-extension interoperability issues.
# Here is the proof that the following works for extensions with
# matching `PYBIND11_INTERNALS_ID`s:
# test_cpp_conduit.cpp:
# py::class_<LonelyTraveler>
# home_planet_very_lonely_traveler.cpp:
# py::class_<VeryLonelyTraveler, LonelyTraveler>
# See test_exo_planet_pybind11_wrap_very_lonely_traveler() for the negative
# test.
assert home_planet.LonelyTraveler is not None # Verify that the base class exists.
home_planet_very_lonely_traveler.wrap_very_lonely_traveler()
# Ensure that the derived class exists.
assert home_planet_very_lonely_traveler.VeryLonelyTraveler is not None
def test_exo_planet_pybind11_wrap_very_lonely_traveler():
# See comment under test_home_planet_wrap_very_lonely_traveler() first.
# Here the `PYBIND11_INTERNALS_ID`s don't match between:
# test_cpp_conduit.cpp:
# py::class_<LonelyTraveler>
# exo_planet_pybind11.cpp:
# py::class_<VeryLonelyTraveler, LonelyTraveler>
assert home_planet.LonelyTraveler is not None # Verify that the base class exists.
with pytest.raises(
RuntimeError,
match='^generic_type: type "VeryLonelyTraveler" referenced unknown base type '
'"pybind11_tests::test_cpp_conduit::LonelyTraveler"$',
):
exo_planet_pybind11.wrap_very_lonely_traveler()

View File

@ -0,0 +1,47 @@
// Copyright (c) 2024 The pybind Community.
#pragma once
#include <pybind11/pybind11.h>
#include "test_cpp_conduit_traveler_types.h"
#include <string>
namespace pybind11_tests {
namespace test_cpp_conduit {
namespace py = pybind11;
inline void wrap_traveler(py::module_ m) {
py::class_<Traveler>(m, "Traveler")
.def(py::init<std::string>())
.def_readwrite("luggage", &Traveler::luggage)
// See issue #3788:
.def("__getattr__", [](const Traveler &self, const std::string &key) {
return "Traveler GetAttr: " + key + " luggage: " + self.luggage;
});
m.def("get_luggage", [](const Traveler &person) { return person.luggage; });
py::class_<PremiumTraveler, Traveler>(m, "PremiumTraveler")
.def(py::init<std::string, int>())
.def_readwrite("points", &PremiumTraveler::points)
// See issue #3788:
.def("__getattr__", [](const PremiumTraveler &self, const std::string &key) {
return "PremiumTraveler GetAttr: " + key + " points: " + std::to_string(self.points);
});
m.def("get_points", [](const PremiumTraveler &person) { return person.points; });
}
inline void wrap_lonely_traveler(py::module_ m) {
py::class_<LonelyTraveler>(std::move(m), "LonelyTraveler");
}
inline void wrap_very_lonely_traveler(py::module_ m) {
py::class_<VeryLonelyTraveler, LonelyTraveler>(std::move(m), "VeryLonelyTraveler");
}
} // namespace test_cpp_conduit
} // namespace pybind11_tests

View File

@ -0,0 +1,25 @@
// Copyright (c) 2024 The pybind Community.
#pragma once
#include <string>
namespace pybind11_tests {
namespace test_cpp_conduit {
struct Traveler {
explicit Traveler(const std::string &luggage) : luggage(luggage) {}
std::string luggage;
};
struct PremiumTraveler : Traveler {
explicit PremiumTraveler(const std::string &luggage, int points)
: Traveler(luggage), points(points) {}
int points;
};
struct LonelyTraveler {};
struct VeryLonelyTraveler : LonelyTraveler {};
} // namespace test_cpp_conduit
} // namespace pybind11_tests

View File

@ -111,6 +111,16 @@ struct PythonAlreadySetInDestructor {
py::str s;
};
struct CustomData {
explicit CustomData(const std::string &a) : a(a) {}
std::string a;
};
struct MyException7 {
explicit MyException7(const CustomData &message) : message(message) {}
CustomData message;
};
TEST_SUBMODULE(exceptions, m) {
m.def("throw_std_exception",
[]() { throw std::runtime_error("This exception was intentionally thrown."); });
@ -385,4 +395,33 @@ TEST_SUBMODULE(exceptions, m) {
// m.def("pass_exception_void", [](const py::exception<void>&) {}); // Does not compile.
m.def("return_exception_void", []() { return py::exception<void>(); });
m.def("throws7", []() {
auto data = CustomData("abc");
throw MyException7(data);
});
py::class_<CustomData>(m, "CustomData", py::module_local())
.def(py::init<const std::string &>())
.def_readwrite("a", &CustomData::a);
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object>
PythonMyException7_storage;
PythonMyException7_storage.call_once_and_store_result([&]() {
auto mod = py::module_::import("custom_exceptions");
py::object obj = mod.attr("PythonMyException7");
return obj;
});
py::register_local_exception_translator([](std::exception_ptr p) {
try {
if (p) {
std::rethrow_exception(p);
}
} catch (const MyException7 &e) {
auto exc_type = PythonMyException7_storage.get_stored();
py::object exc_inst = exc_type(e.message);
PyErr_SetObject(PyExc_Exception, exc_inst.ptr());
}
});
}

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import sys
import pytest
from custom_exceptions import PythonMyException7
import env
import pybind11_cross_module_tests as cm
@ -195,6 +196,10 @@ def test_custom(msg):
raise RuntimeError("Exception error: caught child from parent") from err
assert msg(excinfo.value) == "this is a helper-defined translated exception"
with pytest.raises(PythonMyException7) as excinfo:
m.throws7()
assert msg(excinfo.value) == "[PythonMyException7]: abc"
def test_nested_throws(capture):
"""Tests nested (e.g. C++ -> Python -> C++) exception handling"""

View File

@ -274,10 +274,6 @@ function(pybind11_add_module target_name)
target_link_libraries(${target_name} PRIVATE pybind11::embed)
endif()
if(MSVC)
target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
endif()
# -fvisibility=hidden is required to allow multiple modules compiled against
# different pybind versions to work properly, and for some features (e.g.
# py::module_local). We force it on everything inside the `pybind11`