Skip to content

Commit 0d0a0e2

Browse files
PyBind11 Upstreamvicentebolea
PyBind11 Upstream
authored andcommitted
pybind11 2024-09-13 (58c382a8)
Code extracted from: https://github.com/pybind/pybind11.git at commit 58c382a8e3d7081364d2f5c62e7f429f0412743b (stable). Upstream Shortlog ----------------- Aaron Gokaslan (7): 6f01c60a Improve Python 3.11 support (ornladios#3694) 45219c6b fix: potential memory leak in pypy (ornladios#3774) dd617dec fix: missing move in eval.h (ornladios#3775) 251516bc Cleanup casters to release none() to avoid ref counting (ornladios#4269) 305c4711 fix: Revert pfect args make iterator (ornladios#4234) 5b395c9b fix: improve bytes to str decoding error handling (ornladios#4294) a491af61 bugfix: delete proper ctors in gil.h (ornladios#4490) Chekov2k (1): 15fde1de Add `PYBIND11_SIMPLE_GIL_MANAGEMENT` option (cmake, C++ define) (ornladios#4216) Chris Ohk (1): 5327c199 docs: Correct minor typos (ornladios#3721) DWesl (1): a4f6627d docs: clarify requirements for including pybind11 (#5326) Eli Schwartz (1): 882cb769 add --version option to pybind11-config (#4526) Eric Cousineau (1): 93d68dd9 cast: Qualify symbol usage in PYBIND11_TYPE_CASTER (ornladios#3758) Ethan Steinberg (2): 2de6e398 [v2.10] Revert the addition of the GIL check feature (ornladios#4432) e414c4bd fix: improve the error reporting for inc_ref GIL failures (ornladios#4427) Henry Schreiner (37): 8c859e48 fix: minor CMake warning fix for unused variable (ornladios#3718) 80589625 ci: fix PyPy (ornladios#3768) 5c2b53b5 chore: bump changelog for 2.9.2 (ornladios#3834) 914c06fb chore: set to version 2.9.2 412918d1 feat: add entrypoint for cmake modules dir (ornladios#4258) a8f21107 docs: update changelog (ornladios#4265) eaa5f7bd Revert "feat: add entrypoint for cmake modules dir" (ornladios#4270) 0e82c360 fix: add flag for overriding classic Python search values (ornladios#4195) 738a6f83 ci: move to final release of 3.11 (ornladios#4286) f2ee641e docs: prepare for 2.10.1 release (ornladios#4279) 80dc998e chore: bump versions for 2.10.1 0bd8896a chore: prepare for 2.10.3 (ornladios#4437) 993eb2b6 chore: update to black 23 (ornladios#4482) 1cae8dc0 fix: tests dir has started to show up in packaging (#4510) f5cff4f2 fix: nicer stack level for warning (#4516) 3f5a7e55 docs: changelog for 2.10.4 (#4532) 5b0a6fc2 chore: bump version to 3.10.4 63020d33 docs: prepare for 2.13.1 (#5203) 941f45bc chore: prepare for 2.13.1 f50830ea tests: run on pyodide (#4745) d8fcfe34 fix(cmake): add required emscripten flags (#5298) 6d5704cd docs: prepare for 2.13.2 (#5299) 07f30430 chore: prepare for 2.13.2 835139f5 fix: emscripten cmake issue (#5301) 45eaee91 fix: quote paths from pybind11-config (#5302) 7662af69 docs: prepare for 2.13.3 bd676436 chore: prepare for 2.13.3 75c11769 Revert "fix: quote paths from pybind11-config (#5302)" (#5309) 63b0d146 docs: prepare for 2.13.4 (#5312) c6239a8a chore: version 2.13.4 b0050f30 fix: never use `..` in a header include (#5321) 0d21cadc fix: allow -Wpedantic in C++20 mode (#5322) b3f5f2e7 docs: prepare for 2.13.5 (#5327) 7c33cdc2 chore: prepare for 2.13.5 7b67d8e9 docs: update changelog for 2.13.6 (#5372) e445ca2b ci: PyPI attestations (#5374) a2e59f0e chore: bump to 2.13.6 Lalaland (1): ce63bcb9 Fix casts to void* (ornladios#4275) Markus Bauer (1): 973a16e9 fix: escape paths with spaces in pybind11-config (#4874) Michael Carlstrom (3): dd0e4a0b feat(types): add support for Typing.Callable Special Case (#5202) 65afa13e fix: add guard for GCC <10.3 on C++20 (#5205) a4dd41a1 feat(types) Adds special Case for empty C++ tuple type annotation (#5214) Mike Essenmacher (1): 1f4cf8fe Replace "whitelist" with "allowlist" (#4506) ObeliskGate (2): ff3ca786 fix: `<ranges>` support for `py::tuple` and `py::list` (#5314) b9f85757 fix: using `__cpp_nontype_template_args` instead of `__cpp_nontype_template_parameter_class` (#5330) Ralf Gommers (1): b4307453 docs: extend `PYBIND11_MODULE` documentation, mention `mod_gil_not_used` (#5250) Ralf W. Grosse-Kunstleve (16): 895fc663 ci: update PGI build (old one no longer signed) (ornladios#4260) 3fb36a99 fix: unicode surrogate character in Python exception message. (ornladios#4297) 0abe64c5 Fix `detail::obj_class_name()` to work correctly for meta classes. (ornladios#4436) 050de893 ci: remove clang 10 C++20 (it broke recently) (ornladios#4438) f14bb03d Add clang15 C++20 job (ornladios#4443) 4f6183cf Ensure `import pybind11_tests` traceback is shown. (ornladios#4455) 5ece09ad Resolve new flake8 error (ornladios#4462) c0e2eeba Bump isort version to 5.12.0 (ornladios#4480) c773a02a Appease new flake8 B028 error: (#4513) 9a1eeed0 Make warning suppressions MINGW-specific again. (#4515) 2965fa8d Preparation for v2.11.1 patch release (#4752) 8a099e44 Fix version number mishap: actually update 0 to 1 (#4756) 129934ad Small cleanup/refactoring in support of PR #5213 (#5251) 042c3cfd clang-tidy upgrade (to version 18) (#5272) 570d323b Add `while True` & `top` method to FAQ. (#5340) a5fcc560 Enable type-safe interoperability between different independent Python/C++ bindings systems. (#5296) Sam Gross (3): 3b47b464 fix: use manual padding of instance_map_shard (#5200) 8443d084 Use PyMutex instead of std::mutex in free-threaded build. (#5219) f3a6d414 fix: make gil_safe_call_once thread-safe in free-threaded CPython (#5246) StarQTius (1): 42455b5e fix: clear local internals after finalizing interpreter ornladios#2101 (ornladios#3744) Stefano Rivera (1): bdec5737 Use sysconfig in Python >= 3.10 (ornladios#3764) Theodore Tsirpanis (1): 667563dd docs: remove outdated known limitation. (#5263) Varun Agrawal (1): 3074608e fix(cmake): remove extra = in flto assignment (#5207) Vasily Litvinov (1): 9e6a67d5 Properly translate C++ exception to Python exception when creating Python buffer from wrapped object (#5324) Vemund Handeland (1): 1f187d9a Fix char8_t support (ornladios#4278) Xiaofei Wang (1): 78e26321 Add `type_caster_std_function_specializations` feature. (#4597) albanD (1): da780a00 Make sure to properly untrack gc objects before freeing them (ornladios#4461) cyy (1): 9d6a79c0 fix: issuses detected by static analyzer (ornladios#4440) dependabot[bot] (9): c06f324c chore(deps): bump ilammy/msvc-dev-cmd from 1.12.0 to 1.12.1 (ornladios#4493) ec3f6e24 chore(deps): bump pypa/gh-action-pypi-publish from 1.6.4 to 1.8.1 (#4576) ea10a69d chore(deps): bump actions/attest-build-provenance in the actions group (#5216) 4b2f7cd6 chore(deps): bump certifi from 2024.2.2 to 2024.7.4 in /docs (#5226) d699e99c chore(deps): bump actions/attest-build-provenance in the actions group (#5243) fe808a01 chore(deps): bump the actions group with 2 updates (#5287) 6ee574fa chore(deps): bump actions/attest-build-provenance in the actions group (#5297) 0a96ff7e chore(deps): bump actions/attest-build-provenance in the actions group (#5335) 54ab4249 chore(deps): bump the actions group with 2 updates (#5361) fred-sch (1): f9ae715d fix: typo in documentation (#5284) kajananchinniah (1): 0ed64a04 docs: fixed typo in spelling of first (ornladios#4428) pre-commit-ci[bot] (5): d78de295 chore(deps): update pre-commit hooks (ornladios#4439) 3ea37d04 chore(deps): update pre-commit hooks (ornladios#4495) 41726b64 chore(deps): update pre-commit hooks (#5220) 44d0d9a4 chore(deps): update pre-commit hooks (#5288) 36ee4674 chore(deps): update pre-commit hooks (#5350) pwdcd (1): 6685547e chore: remove repetitive words (#5308) wenqing (1): 639ca6a7 Fixed a compilation error with gcc 14 (#5208) xkszltl (1): b596235f Inconsistent comments between 2 templates of `unchecked()`. (#4519)
1 parent b76ecb3 commit 0d0a0e2

18 files changed

+405
-188
lines changed

include/pybind11/cast.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,11 @@ struct copyable_holder_caster : public type_caster_base<type> {
794794
}
795795
}
796796

797-
bool load_value(value_and_holder &&v_h) {
797+
void load_value(value_and_holder &&v_h) {
798798
if (v_h.holder_constructed()) {
799799
value = v_h.value_ptr();
800800
holder = v_h.template holder<holder_type>();
801-
return true;
801+
return;
802802
}
803803
throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
804804
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)

include/pybind11/detail/class.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
#pragma once
1111

12-
#include "../attr.h"
13-
#include "../options.h"
12+
#include <pybind11/attr.h>
13+
#include <pybind11/options.h>
14+
15+
#include "exception_translation.h"
1416

1517
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1618
PYBIND11_NAMESPACE_BEGIN(detail)
@@ -591,7 +593,18 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla
591593
return -1;
592594
}
593595
std::memset(view, 0, sizeof(Py_buffer));
594-
buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
596+
buffer_info *info = nullptr;
597+
try {
598+
info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
599+
} catch (...) {
600+
try_translate_exceptions();
601+
raise_from(PyExc_BufferError, "Error getting buffer");
602+
return -1;
603+
}
604+
if (info == nullptr) {
605+
pybind11_fail("FATAL UNEXPECTED SITUATION: tinfo->get_buffer() returned nullptr.");
606+
}
607+
595608
if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) {
596609
delete info;
597610
// view->obj = nullptr; // Was just memset to 0, so not necessary

include/pybind11/detail/common.h

+23-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#pragma once
1111

1212
#define PYBIND11_VERSION_MAJOR 2
13-
#define PYBIND11_VERSION_MINOR 14
14-
#define PYBIND11_VERSION_PATCH 0.dev1
13+
#define PYBIND11_VERSION_MINOR 13
14+
#define PYBIND11_VERSION_PATCH 6
1515

1616
// Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html
1717
// Additional convention: 0xD = dev
18-
#define PYBIND11_VERSION_HEX 0x020E00D1
18+
#define PYBIND11_VERSION_HEX 0x020D0600
1919

2020
// Define some generic pybind11 helper macros for warning management.
2121
//
@@ -462,7 +462,25 @@ PYBIND11_WARNING_POP
462462
return "Hello, World!";
463463
});
464464
}
465+
466+
The third macro argument is optional (available since 2.13.0), and can be used to
467+
mark the extension module as safe to run without the GIL under a free-threaded CPython
468+
interpreter. Passing this argument has no effect on other interpreters.
469+
470+
.. code-block:: cpp
471+
472+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
473+
m.doc() = "pybind11 example module safe to run without the GIL";
474+
475+
// Add bindings here
476+
m.def("foo", []() {
477+
return "Hello, Free-threaded World!";
478+
});
479+
}
480+
465481
\endrst */
482+
PYBIND11_WARNING_PUSH
483+
PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
466484
#define PYBIND11_MODULE(name, variable, ...) \
467485
static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name) \
468486
PYBIND11_MAYBE_UNUSED; \
@@ -483,6 +501,7 @@ PYBIND11_WARNING_POP
483501
PYBIND11_CATCH_INIT_EXCEPTIONS \
484502
} \
485503
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ & (variable))
504+
PYBIND11_WARNING_POP
486505

487506
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
488507

@@ -538,7 +557,7 @@ enum class return_value_policy : uint8_t {
538557
object without taking ownership similar to the above
539558
return_value_policy::reference policy. In contrast to that policy, the
540559
function or property's implicit this argument (called the parent) is
541-
considered to be the the owner of the return value (the child).
560+
considered to be the owner of the return value (the child).
542561
pybind11 then couples the lifetime of the parent to the child via a
543562
reference relationship that ensures that the parent cannot be garbage
544563
collected while Python is still using the child. More advanced

include/pybind11/detail/cpp_conduit.h

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2024 The pybind Community.
2+
3+
#pragma once
4+
5+
#include <pybind11/pytypes.h>
6+
7+
#include "common.h"
8+
#include "internals.h"
9+
10+
#include <typeinfo>
11+
12+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
13+
PYBIND11_NAMESPACE_BEGIN(detail)
14+
15+
// Forward declaration needed here: Refactoring opportunity.
16+
extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *);
17+
18+
inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) {
19+
#if defined(PYPY_VERSION)
20+
auto &internals = get_internals();
21+
return bool(internals.registered_types_py.find(type_obj)
22+
!= internals.registered_types_py.end());
23+
#else
24+
return bool(type_obj->tp_new == pybind11_object_new);
25+
#endif
26+
}
27+
28+
inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
29+
PyObject *descr = _PyType_Lookup(type_obj, attr_name);
30+
return bool((descr != nullptr) && PyInstanceMethod_Check(descr));
31+
}
32+
33+
inline object try_get_cpp_conduit_method(PyObject *obj) {
34+
if (PyType_Check(obj)) {
35+
return object();
36+
}
37+
PyTypeObject *type_obj = Py_TYPE(obj);
38+
str attr_name("_pybind11_conduit_v1_");
39+
bool assumed_to_be_callable = false;
40+
if (type_is_managed_by_our_internals(type_obj)) {
41+
if (!is_instance_method_of_type(type_obj, attr_name.ptr())) {
42+
return object();
43+
}
44+
assumed_to_be_callable = true;
45+
}
46+
PyObject *method = PyObject_GetAttr(obj, attr_name.ptr());
47+
if (method == nullptr) {
48+
PyErr_Clear();
49+
return object();
50+
}
51+
if (!assumed_to_be_callable && PyCallable_Check(method) == 0) {
52+
Py_DECREF(method);
53+
return object();
54+
}
55+
return reinterpret_steal<object>(method);
56+
}
57+
58+
inline void *try_raw_pointer_ephemeral_from_cpp_conduit(handle src,
59+
const std::type_info *cpp_type_info) {
60+
object method = try_get_cpp_conduit_method(src.ptr());
61+
if (method) {
62+
capsule cpp_type_info_capsule(const_cast<void *>(static_cast<const void *>(cpp_type_info)),
63+
typeid(std::type_info).name());
64+
object cpp_conduit = method(bytes(PYBIND11_PLATFORM_ABI_ID),
65+
cpp_type_info_capsule,
66+
bytes("raw_pointer_ephemeral"));
67+
if (isinstance<capsule>(cpp_conduit)) {
68+
return reinterpret_borrow<capsule>(cpp_conduit).get_pointer();
69+
}
70+
}
71+
return nullptr;
72+
}
73+
74+
#define PYBIND11_HAS_CPP_CONDUIT 1
75+
76+
PYBIND11_NAMESPACE_END(detail)
77+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
pybind11/detail/exception_translation.h: means to translate C++ exceptions to Python exceptions
3+
4+
Copyright (c) 2024 The Pybind Development Team.
5+
6+
All rights reserved. Use of this source code is governed by a
7+
BSD-style license that can be found in the LICENSE file.
8+
*/
9+
10+
#pragma once
11+
12+
#include "common.h"
13+
#include "internals.h"
14+
15+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
16+
PYBIND11_NAMESPACE_BEGIN(detail)
17+
18+
// Apply all the extensions translators from a list
19+
// Return true if one of the translators completed without raising an exception
20+
// itself. Return of false indicates that if there are other translators
21+
// available, they should be tried.
22+
inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) {
23+
auto last_exception = std::current_exception();
24+
25+
for (auto &translator : translators) {
26+
try {
27+
translator(last_exception);
28+
return true;
29+
} catch (...) {
30+
last_exception = std::current_exception();
31+
}
32+
}
33+
return false;
34+
}
35+
36+
inline void try_translate_exceptions() {
37+
/* When an exception is caught, give each registered exception
38+
translator a chance to translate it to a Python exception. First
39+
all module-local translators will be tried in reverse order of
40+
registration. If none of the module-locale translators handle
41+
the exception (or there are no module-locale translators) then
42+
the global translators will be tried, also in reverse order of
43+
registration.
44+
45+
A translator may choose to do one of the following:
46+
47+
- catch the exception and call py::set_error()
48+
to set a standard (or custom) Python exception, or
49+
- do nothing and let the exception fall through to the next translator, or
50+
- delegate translation to the next translator by throwing a new type of exception.
51+
*/
52+
53+
bool handled = with_internals([&](internals &internals) {
54+
auto &local_exception_translators = get_local_internals().registered_exception_translators;
55+
if (detail::apply_exception_translators(local_exception_translators)) {
56+
return true;
57+
}
58+
auto &exception_translators = internals.registered_exception_translators;
59+
if (detail::apply_exception_translators(exception_translators)) {
60+
return true;
61+
}
62+
return false;
63+
});
64+
65+
if (!handled) {
66+
set_error(PyExc_SystemError, "Exception escaped from default exception translator!");
67+
}
68+
}
69+
70+
PYBIND11_NAMESPACE_END(detail)
71+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/detail/init.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ void construct(value_and_holder &v_h, Cpp<Class> *ptr, bool need_alias) {
128128
// the holder and destruction happens when we leave the C++ scope, and the holder
129129
// class gets to handle the destruction however it likes.
130130
v_h.value_ptr() = ptr;
131-
v_h.set_instance_registered(true); // To prevent init_instance from registering it
132-
v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder
131+
v_h.set_instance_registered(true); // Trick to prevent init_instance from registering it
132+
// DANGER ZONE BEGIN: exceptions will leave v_h in an invalid state.
133+
v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder
133134
Holder<Class> temp_holder(std::move(v_h.holder<Holder<Class>>())); // Steal the holder
134135
v_h.type->dealloc(v_h); // Destroys the moved-out holder remains, resets value ptr to null
135136
v_h.set_instance_registered(false);
137+
// DANGER ZONE END.
136138

137139
construct_alias_from_cpp<Class>(is_alias_constructible<Class>{}, v_h, std::move(*ptr));
138140
} else {

include/pybind11/detail/internals.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include "common.h"
1313

1414
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
15-
# include "../gil.h"
15+
# include <pybind11/gil.h>
1616
#endif
1717

18-
#include "../pytypes.h"
18+
#include <pybind11/pytypes.h>
1919

2020
#include <exception>
2121
#include <mutex>
@@ -321,15 +321,17 @@ struct type_info {
321321
# define PYBIND11_INTERNALS_KIND ""
322322
#endif
323323

324+
#define PYBIND11_PLATFORM_ABI_ID \
325+
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
326+
PYBIND11_BUILD_TYPE
327+
324328
#define PYBIND11_INTERNALS_ID \
325329
"__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
326-
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB \
327-
PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
330+
PYBIND11_PLATFORM_ABI_ID "__"
328331

329332
#define PYBIND11_MODULE_LOCAL_ID \
330333
"__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
331-
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB \
332-
PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
334+
PYBIND11_PLATFORM_ABI_ID "__"
333335

334336
/// Each module locally stores a pointer to the `internals` data. The data
335337
/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
@@ -553,7 +555,7 @@ PYBIND11_NOINLINE internals &get_internals() {
553555
}
554556
#endif
555557
internals_ptr->istate = tstate->interp;
556-
state_dict[PYBIND11_INTERNALS_ID] = capsule(internals_pp);
558+
state_dict[PYBIND11_INTERNALS_ID] = capsule(reinterpret_cast<void *>(internals_pp));
557559
internals_ptr->registered_exception_translators.push_front(&translate_exception);
558560
internals_ptr->static_property_type = make_static_property_type();
559561
internals_ptr->default_metaclass = make_default_metaclass();

0 commit comments

Comments
 (0)