Skip to content

Commit

Permalink
pybind11 2024-09-13 (58c382a8)
Browse files Browse the repository at this point in the history
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 (#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 (#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 (#4443)
      4f6183cf Ensure `import pybind11_tests` traceback is shown. (#4455)
      5ece09ad Resolve new flake8 error (#4462)
      c0e2eeba Bump isort version to 5.12.0 (#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 (#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 (#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 (#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)
  • Loading branch information
PyBind11 Upstream authored and vicentebolea committed Jan 7, 2025
1 parent b76ecb3 commit 0d0a0e2
Show file tree
Hide file tree
Showing 18 changed files with 405 additions and 188 deletions.
4 changes: 2 additions & 2 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,11 @@ struct copyable_holder_caster : public type_caster_base<type> {
}
}

bool load_value(value_and_holder &&v_h) {
void load_value(value_and_holder &&v_h) {
if (v_h.holder_constructed()) {
value = v_h.value_ptr();
holder = v_h.template holder<holder_type>();
return true;
return;
}
throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
Expand Down
19 changes: 16 additions & 3 deletions include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

#pragma once

#include "../attr.h"
#include "../options.h"
#include <pybind11/attr.h>
#include <pybind11/options.h>

#include "exception_translation.h"

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
Expand Down Expand Up @@ -591,7 +593,18 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla
return -1;
}
std::memset(view, 0, sizeof(Py_buffer));
buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
buffer_info *info = nullptr;
try {
info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
} catch (...) {
try_translate_exceptions();
raise_from(PyExc_BufferError, "Error getting buffer");
return -1;
}
if (info == nullptr) {
pybind11_fail("FATAL UNEXPECTED SITUATION: tinfo->get_buffer() returned nullptr.");
}

if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) {
delete info;
// view->obj = nullptr; // Was just memset to 0, so not necessary
Expand Down
27 changes: 23 additions & 4 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#pragma once

#define PYBIND11_VERSION_MAJOR 2
#define PYBIND11_VERSION_MINOR 14
#define PYBIND11_VERSION_PATCH 0.dev1
#define PYBIND11_VERSION_MINOR 13
#define PYBIND11_VERSION_PATCH 6

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

// Define some generic pybind11 helper macros for warning management.
//
Expand Down Expand Up @@ -462,7 +462,25 @@ PYBIND11_WARNING_POP
return "Hello, World!";
});
}
The third macro argument is optional (available since 2.13.0), and can be used to
mark the extension module as safe to run without the GIL under a free-threaded CPython
interpreter. Passing this argument has no effect on other interpreters.
.. code-block:: cpp
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
m.doc() = "pybind11 example module safe to run without the GIL";
// Add bindings here
m.def("foo", []() {
return "Hello, Free-threaded World!";
});
}
\endrst */
PYBIND11_WARNING_PUSH
PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
#define PYBIND11_MODULE(name, variable, ...) \
static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name) \
PYBIND11_MAYBE_UNUSED; \
Expand All @@ -483,6 +501,7 @@ PYBIND11_WARNING_POP
PYBIND11_CATCH_INIT_EXCEPTIONS \
} \
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ & (variable))
PYBIND11_WARNING_POP

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

Expand Down Expand Up @@ -538,7 +557,7 @@ enum class return_value_policy : uint8_t {
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).
considered to be 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
Expand Down
77 changes: 77 additions & 0 deletions include/pybind11/detail/cpp_conduit.h
Original file line number Diff line number Diff line change
@@ -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)
71 changes: 71 additions & 0 deletions include/pybind11/detail/exception_translation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
pybind11/detail/exception_translation.h: means to translate C++ exceptions to Python exceptions
Copyright (c) 2024 The Pybind Development Team.
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 "common.h"
#include "internals.h"

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)

// Apply all the extensions translators from a list
// Return true if one of the translators completed without raising an exception
// itself. Return of false indicates that if there are other translators
// available, they should be tried.
inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) {
auto last_exception = std::current_exception();

for (auto &translator : translators) {
try {
translator(last_exception);
return true;
} catch (...) {
last_exception = std::current_exception();
}
}
return false;
}

inline void try_translate_exceptions() {
/* When an exception is caught, give each registered exception
translator a chance to translate it to a Python exception. First
all module-local translators will be tried in reverse order of
registration. If none of the module-locale translators handle
the exception (or there are no module-locale translators) then
the global translators will be tried, also in reverse order of
registration.
A translator may choose to do one of the following:
- catch the exception and call py::set_error()
to set a standard (or custom) Python exception, or
- do nothing and let the exception fall through to the next translator, or
- 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;
});

if (!handled) {
set_error(PyExc_SystemError, "Exception escaped from default exception translator!");
}
}

PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
6 changes: 4 additions & 2 deletions include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ void construct(value_and_holder &v_h, Cpp<Class> *ptr, bool need_alias) {
// the holder and destruction happens when we leave the C++ scope, and the holder
// class gets to handle the destruction however it likes.
v_h.value_ptr() = ptr;
v_h.set_instance_registered(true); // To prevent init_instance from registering it
v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder
v_h.set_instance_registered(true); // Trick to prevent init_instance from registering it
// DANGER ZONE BEGIN: exceptions will leave v_h in an invalid state.
v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder
Holder<Class> temp_holder(std::move(v_h.holder<Holder<Class>>())); // Steal the holder
v_h.type->dealloc(v_h); // Destroys the moved-out holder remains, resets value ptr to null
v_h.set_instance_registered(false);
// DANGER ZONE END.

construct_alias_from_cpp<Class>(is_alias_constructible<Class>{}, v_h, std::move(*ptr));
} else {
Expand Down
16 changes: 9 additions & 7 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "common.h"

#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# include "../gil.h"
# include <pybind11/gil.h>
#endif

#include "../pytypes.h"
#include <pybind11/pytypes.h>

#include <exception>
#include <mutex>
Expand Down Expand Up @@ -321,15 +321,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`.
Expand Down Expand Up @@ -553,7 +555,7 @@ PYBIND11_NOINLINE internals &get_internals() {
}
#endif
internals_ptr->istate = tstate->interp;
state_dict[PYBIND11_INTERNALS_ID] = capsule(internals_pp);
state_dict[PYBIND11_INTERNALS_ID] = capsule(reinterpret_cast<void *>(internals_pp));
internals_ptr->registered_exception_translators.push_front(&translate_exception);
internals_ptr->static_property_type = make_static_property_type();
internals_ptr->default_metaclass = make_default_metaclass();
Expand Down
Loading

0 comments on commit 0d0a0e2

Please sign in to comment.