Version 2.7.0
New features:
- Enable
py::implicitly_convertible<py::none, ...>forpy::class_-wrapped types. #3059 - Allow function pointer extraction from overloaded functions. #2944
- NumPy: added
.char_()to type which gives the NumPy publiccharresult, which also distinguishes types by bit length (unlike.kind()). #2864 - Add
pybind11::bytearrayto manipulatebytearraysimilar tobytes. #2799 pybind11/stl/filesystem.hregisters a type caster that, on C++17/Python 3.6+, convertsstd::filesystem::pathtopathlib.Pathand anyos.PathLiketostd::filesystem::path. #2730- A
PYBIND11_VERSION_HEXdefine was added, similar toPY_VERSION_HEX. #3120
Changes:
py::strchanged to exclusively holdPyUnicodeObject. Previouslypy::strcould
also holdbytes, which is probably surprising, was never documented, and can mask bugs (e.g. accidental use ofpy::strinstead ofpy::bytes). #2409- Add a safety guard to ensure that the Python GIL is held when C++ calls back into Python via
object_api<>::operator()(e.g.py::function__call__). (This feature is available for Python 3.6+ only.) #2919 - Catch a missing
selfargument in calls to__init__(). #2914 - Use
std::string_viewif available to avoid a copy when passing an object to astd::ostream. #3042 - An important warning about thread safety was added to the
iostream.hdocumentation; attempts to makepy::scoped_ostream_redirectthread safe have been removed, as it was only partially effective. #2995
Fixes:
- Performance: avoid unnecessary strlen calls. #3058
- Fix auto-generated documentation string when using
const Tinpyarray_t. #3020 - Unify error messages thrown by
simple_collector/unpacking_collector. #3013 pybind11::builtin_exceptionis 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- Fixed exception when printing UTF-8 to a
scoped_ostream_redirect. #2982 - Pickle support enhancement:
setstateimplementation will attempt tosetattr__dict__only if the unpickleddictobject is not empty, to not force use ofpy::dynamic_attr()unnecessarily. #2972 - Allow negative timedelta values to roundtrip. #2870
- Fix unchecked errors could potentially swallow signals/other exceptions. #2863
- Add null pointer check with
std::localtime. #2846 - Fix the
weakrefconstructor frompy::objectto create a newweakrefon conversion. #2832 - Avoid relying on exceptions in C++17 when getting a
shared_ptrholder from ashared_from_thisclass. #2819 - Allow the codec's exception to be raised instead of
RuntimeErrorwhen casting frompy::strtostd::string. #2903
Build system improvements:
- In
setup_helpers.py, test for platforms that have some multiprocessing features but lack semaphores, whichParallelCompilerequires. #3043 - Fix
pybind11_INCLUDE_DIRin caseCMAKE_INSTALL_INCLUDEDIRis absolute. #3005 - Fix bug not respecting
WITH_SOABIorWITHOUT_SOABIto CMake. #2938 - Fix the default
Pybind11Extensioncompilation flags with a Mingw64 python. #2921 - Clang on Windows: do not pass
/MP(ignored flag). #2824 pybind11.setup_helpers.intree_extensionscan be used to generatePybind11Extensioninstances from cpp files placed in the Python
package source tree. #2831
Backend and tidying up:
- Enable clang-tidy performance, readability, and modernization checks throughout the codebase to enforce best coding practices. #3046, #3049, #3051, #3052, #3080, and #3094
- Checks for common misspellings were added to the pre-commit hooks. #3076
- Changed
Werrorto stricterWerror-allfor Intel compiler and fixed minor issues. #2948 - Fixed compilation with GCC < 5 when the user defines
_GLIBCXX_USE_CXX11_ABI. #2956 - Added nox support for easier local testing and linting of contributions. #3101 and #3121
- Avoid RTD style issue with docutils 0.17+. #3119
- Support pipx run, such as
pipx run pybind11 --includefor a quick compile. #3117