Seamless operability between C++11 and Python
This is the last version to support Python 2.7 and 3.5.
New Features:
py::args
to be followed by other arguments; the remaining arguments are implicitly keyword-only, as if a py::kw_only{}
annotation had been used. #3402
Changes:
std::string_view
. #3521
_
with const_name
in internals, avoid defining pybind::_
if _
defined as macro (common gettext usage) #3423
Bug fixes:
std::forward
calls to some cpp_function
overloads. #3443
python dev
label. #3419
Eigen::MappedSparseMatrix
with Eigen::Map<Eigen::SparseMatrix<...>>
for Eigen 3.3+. #3499
Build system improvements:
-g
in $CFLAGS
and $CPPFLAGS
are no longer overridden by .Pybind11Extension
. #3436
setup_helpers
. #3548
mips64
and ppc64le
(reported broken). #3557
Changes and additions:
py::module_::import("types").attr("SimpleNamespace")
. #3374
AttributeError
. Useful for defining custom __setattr__
and __getattr__
methods. #3387
Fixes:
std::optional
types. #3376
PyCodeObject
on Python 3.9+ (moving toward support for Python 3.11a1) #3368
eigen.h
was fixed (originally PR #3343). The bug was unmasked by newly added static_assert
's in the Eigen 3.4.0 release. #3352
make_key_iterator
/make_value_iterator
if dereferencing the iterator returned a temporary value instead of a reference. #3348
New features:
py::raise_from
to enable chaining exceptions. #3215
register_local_exception_translator(ExceptionTranslator&& translator)
instead of register_exception_translator(ExceptionTranslator&& translator)
to keep your exception remapping code local to the module. #2650
make_simple_namespace
function for instantiating Python SimpleNamespace
objects. #2840
pybind11::scoped_interpreter
and initialize_interpreter
have new arguments to allow sys.argv
initialization. #2341
view
to view arrays with a different datatype. #987
reshape
on arrays. #984
__new__
methods on classes by fixing bug preventing overriding methods if they have non-pybind11 siblings. #3265
make_value_iterator()
, and fix make_key_iterator()
to return references instead of copies. #3293
bind_map
: #3310
.items
from an iterator to a dictionary view..keys
and .values
(both dictionary views).__contains__
to take any object.pybind11::custom_type_setup
was added, for customizing the PyHeapTypeObject
corresponding to a class, which may be useful for enabling garbage collection support, among other things. #3287
Changes:
__file__
constant when running eval_file
in an embedded interpreter. #3233
std::optional
now accepted in py::slice
constructor. #1101
str
, bytes
, bytearray
, tuple
, list
now consistently support passing ssize_t
values for sizes and indexes. Previously, only size_t
was accepted in several interfaces. #3219
PYBIND11_TLS_REPLACE_VALUE
arguments more than once. #3290
Fixes:
__int__
returning non-int when underlying type is bool or of char type #1334
py::cpp_function::initialize
was fixed. #3229
pybind11::type_caster
which require lifetime extension, such as for std::string_view
. #3237
Build system improvements:
Backend and tidying up:
std::string
and std::string_view
now avoids making an extra copy of the data on Python >= 3.3. #3257
list
, set
, dict
) such as (clear()
, append()
, insert()
, etc...) and annotated them with py-non-const
.clang-tidy-const-return
and remove useless consts. #3254 #3194
google-explicit-constructor
option was enabled. #3250
clang-tidy-readability
rules to make boolean casts explicit improving code readability. Also enabled other misc and readability clang-tidy checks. #3148
.pop()
for list. #3116
Minor missing functionality added:
Bug fixes:
.pop()
for list. #3116
Backend and tidying up:
New features:
py::implicitly_convertible<py::none, ...>
for py::class_
-wrapped types. #3059
.char_()
to type which gives the NumPy public char
result, which also distinguishes types by bit length (unlike .kind()
). #2864
pybind11::bytearray
to manipulate bytearray
similar to bytes
. #2799
pybind11/stl/filesystem.h
registers a type caster that, on C++17/Python 3.6+, converts std::filesystem::path
to pathlib.Path
and any os.PathLike
to std::filesystem::path
. #2730
PYBIND11_VERSION_HEX
define was added, similar to PY_VERSION_HEX
. #3120
Changes:
py::str
changed to exclusively hold PyUnicodeObject
. Previously py::str
could
also hold bytes
, which is probably surprising, was never documented, and can mask bugs (e.g. accidental use of py::str
instead of py::bytes
). #2409
object_api<>::operator()
(e.g. py::function
__call__
). (This feature is available for Python 3.6+ only.) #2919
self
argument in calls to __init__()
. #2914
std::string_view
if available to avoid a copy when passing an object to a std::ostream
. #3042
iostream.h
documentation; attempts to make py::scoped_ostream_redirect
thread safe have been removed, as it was only partially effective. #2995
Fixes:
const T
in pyarray_t
. #3020
simple_collector
/unpacking_collector
. #3013
pybind11::builtin_exception
is now explicitly exported, which means the types included/defined in different modules are identical, and exceptions raised in different modules can be caught correctly. The documentation was updated to explain that custom exceptions that are used across module boundaries need to be explicitly exported as well. #2999
scoped_ostream_redirect
. #2982
setstate
implementation will attempt to setattr
__dict__
only if the unpickled dict
object is not empty, to not force use of py::dynamic_attr()
unnecessarily. #2972
std::localtime
. #2846
weakref
constructor from py::object
to create a new weakref
on conversion. #2832
shared_ptr
holder from a shared_from_this
class. #2819
RuntimeError
when casting from py::str
to std::string
. #2903
Build system improvements:
setup_helpers.py
, test for platforms that have some multiprocessing features but lack semaphores, which ParallelCompile
requires. #3043
pybind11_INCLUDE_DIR
in case CMAKE_INSTALL_INCLUDEDIR
is absolute. #3005
WITH_SOABI
or WITHOUT_SOABI
to CMake. #2938
Pybind11Extension
compilation flags with a Mingw64 python. #2921
/MP
(ignored flag). #2824
pybind11.setup_helpers.intree_extensions
can be used to generate Pybind11Extension
instances from cpp files placed in the Python
package source tree. #2831
Backend and tidying up:
Werror
to stricter Werror-all
for Intel compiler and fixed minor issues. #2948
_GLIBCXX_USE_CXX11_ABI
. #2956
pipx run pybind11 --include
for a quick compile. #3117
Minor missing functionality added:
.disarm
for gil_scoped_acquire
/gil_scoped_release
. #2657
Fixed or improved behavior in a few special cases:
object
subclasses would not throw on being passed a Python object of the wrong type. #2701
type_caster
for integers does not convert Python objects with __int__
anymore with noconvert
or during the first round of trying overloads. #2698
__index__
is always called and not considered as conversion, consistent with Python 3.8+. #2801
Build improvements:
extra_compile_args
and extra_link_args
automatically set by Pybind11Extension are now prepended, which allows them to be overridden by user-set extra_compile_args
and extra_link_args
. #2808
--warn-uninitialized
active. #2806
STATIC
/ SHARED
being ignored in FindPython mode. #2796
CMAKE_CXX_VISIBILITY_PRESET
if defined. #2793
pybind11::embed
. #2662
Bug fixes:
scoped_ostream_redirect
. #2675
py::gil_scoped_acquire
assert with CPython 3.9 debug build. #2683
Warning fixes:
[-Wshadow-field-in-constructor-modified]
. #2780
__init__
/__setstate__
in the tests. #2759
Valgrind work:
__init__
on a non-pybind11 class instance. #2755
Compiler support:
py::exec
, py::eval
, and py::eval_file
now add the builtins module as "__builtins__"
to their globals
argument, better matching exec
and eval
in pure Python. #2616
setup_helpers
will no longer set a minimum macOS version higher than the current version. #2622
def_buffer
, cleaning up the capture
object after the class_
object goes out of scope.
#2634
pybind11_INCLUDE_DIRS
was incorrect, potentially causing a regression if it was expected to include PYTHON_INCLUDE_DIRS
(please use targets instead). #2636
py::enum_
constructor and methods, avoiding arg0
in the generated docstrings. #2637
needs_recompile
optional function to the ParallelCompiler
helper, to allow a recompile to be skipped based on a user-defined function. #2643
New features:
py::kw_only()
. #2100
py::pos_only()
. #2459
py::is_final()
class modifier to block subclassing (CPython only). #2151
py::prepend()
, allowing a function to be placed at the beginning of the overload chain. #1131
py::type::of<T>()
and py::type::of(h)
. #2364
py::error_already_set::discard_as_unraisable()
. #2372
py::hash
is now public. #2217
py::class_<union_type>
is now supported. Note that writing to one data member of the union and reading another (type punning) is UB in C++. Thus pybind11-bound enums should never be used for such conversions. #2320.Code correctness features:
__init__
is forgotten on subclasses. #2152
py::bytes(o)
when py::object o
isn't a bytes instance. #2349
str
fails. #2477
API changes:
py::module
was renamed py::module_
to avoid issues with C++20 when used unqualified, but an alias py::module
is provided for backward compatibility. #2489
py::module_
have been deprecated; please use pybind11::module_::create_extension_module
if you were using the public constructor (fairly rare after PYBIND11_MODULE
was introduced). #2552
PYBIND11_OVERLOAD*
macros and get_overload
function replaced by correctly-named PYBIND11_OVERRIDE*
and get_override
, fixing inconsistencies in the presence of a closing ;
in these macros. get_type_overload
is deprecated. #2325
Packaging / building improvements:
build-setuptools
is easier thanks to a new pybind11.setup_helpers
module, which provides utilities to use setuptools with pybind11. It can be used via PEP 518, setup_requires
, or by directly importing or copying setup_helpers.py
into your project.pybind11.get_cmake_dir()
or python -m pybind11 --cmakedir
to get the directory with the CMake configuration files, or include the site-packages location in your CMAKE_MODULE_PATH
. Or you can use the new pybind11[global]
extra when you install pybind11
, which installs the CMake files and headers into your base environment in the standard location.pybind11-config
is another way to write python -m pybind11
if you have your PATH set up.import pybind11
can now be type checked. #2588
PYBIND11_CPP_STANDARD
.PYTHON_EXECUTABLE
is not set (venv
, virtualenv
, and conda
) (similar to the new FindPython mode).CMAKE_INTERPROCEDURAL_OPTIMIZATION
, set(CMAKE_CXX_VISIBILITY_PRESET hidden)
.CUDA
as a language is now supported.pybind11_strip
, pybind11_extension
, pybind11_find_import
added, see cmake/index
.find-python-mode
and nopython-mode
with CMake. #2370
pybind11_add_module()
now accepts an optional OPT_SIZE
flag that switches the binding target to size-based optimization if the global build type can not always be fixed to MinSizeRel
(except in debug mode, where optimizations remain disabled). MinSizeRel
or this flag reduces binary size quite substantially (~25% on some platforms). #2463
Smaller or developer focused features and fixes:
mkdoc.py
to a new repo, pybind11-mkdoc. There are no longer submodules in the main repo.py::memoryview
segfault fix and update, with new py::memoryview::from_memory
in Python 3, and documentation. #2223
buffer_info
on Python 2. #2503
__eq__
defined but not __hash__
, __hash__
is now set to None
. #2291
py::ellipsis
now also works on Python 2. #2360
std::tuple
& std::pair
supported in cast. #2334
py::array
now uses py::ssize_t
as first argument type. #2293
py::array
. #2363
unchecked_mutable_reference
has access to operator ()
and []
when const. #2514
py::vectorize
is now supported on functions that return void. #1969
py::capsule
supports get_pointer
and set_pointer
. #1131
py::len
not clearing Python's error state when it fails and throws. #2575
std::chrono::time_point
now works when the resolution is not the same as the system. #2481
py::array_t
could accept arrays that did not match the requested ordering. #2484
py::arg::none()
is now also respected when passing keyword arguments. #2611
Next release candidate.
Next release candidate. Changes since RC 1:
py::len
leaving error state setRelWithDebInfo
(use CMAKE_INTERPROCEDURAL_OPTIMIZATION
for more control)setup_helpers
py::module_
constructor are no longer provisional.import pybind11