Skip to content

Commit

Permalink
Merge branch 'sh_merge_master' into pybind11k_merge_sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jun 26, 2024
2 parents b21f37e + fb4c618 commit c4651ff
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 6 deletions.
101 changes: 101 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,107 @@ IN DEVELOPMENT

Changes will be summarized here periodically.

Version 2.13.0 (June 25, 2024)
------------------------------

New Features:

* Support free-threaded CPython (3.13t). Add ``py::mod_gil_not_used()`` tag to
indicate if a module supports running with the GIL disabled.
`#5148 <https://github.com/pybind/pybind11/pull/5148>`_

* Support for Python 3.6 was removed. (Official end-of-life: 2021-12-23).
`#5177 <https://github.com/pybind/pybind11/pull/5177>`_

* ``py::list`` gained a ``.clear()`` method.
`#5153 <https://github.com/pybind/pybind11/pull/5153>`_


.. feat(types)
* Support for ``Union``, ``Optional``, ``type[T]``, ``typing.TypeGuard``,
``typing.TypeIs``, ``typing.Never``, ``typing.NoReturn`` and
``typing.Literal`` was added to ``pybind11/typing.h``.
`#5166 <https://github.com/pybind/pybind11/pull/5166>`_
`#5165 <https://github.com/pybind/pybind11/pull/5165>`_
`#5194 <https://github.com/pybind/pybind11/pull/5194>`_
`#5193 <https://github.com/pybind/pybind11/pull/5193>`_
`#5192 <https://github.com/pybind/pybind11/pull/5192>`_


.. feat(cmake)
* In CMake, if ``PYBIND11_USE_CROSSCOMPILING`` is enabled, then
``CMAKE_CROSSCOMPILING`` will be respected and will keep pybind11 from
accessing the interpreter during configuration. Several CMake variables will
be required in this case, but can be deduced from the environment variable
``SETUPTOOLS_EXT_SUFFIX``. The default (currently ``OFF``) may be changed in
the future.
`#5083 <https://github.com/pybind/pybind11/pull/5083>`_


Bug fixes:

* A refcount bug (leading to heap-use-after-free) involving trampoline
functions with ``PyObject *`` return type was fixed.
`#5156 <https://github.com/pybind/pybind11/pull/5156>`_

* Return ``py::ssize_t`` from ``.ref_count()`` instead of ``int``.
`#5139 <https://github.com/pybind/pybind11/pull/5139>`_

* A subtle bug involving C++ types with unusual ``operator&`` overrides
was fixed.
`#5189 <https://github.com/pybind/pybind11/pull/5189>`_

* Support Python 3.13 with minor fix, add to CI.
`#5127 <https://github.com/pybind/pybind11/pull/5127>`_


.. fix(cmake)
* Fix mistake affecting old cmake and old boost.
`#5149 <https://github.com/pybind/pybind11/pull/5149>`_


Documentation:

* Build docs updated to feature scikit-build-core and meson-python, and updated
setuptools instructions.
`#5168 <https://github.com/pybind/pybind11/pull/5168>`_


Tests:

* Avoid immortal objects in tests.
`#5150 <https://github.com/pybind/pybind11/pull/5150>`_


CI:

* Compile against Python 3.13t in CI.

* Use ``macos-13`` (Intel) for CI jobs for now (will drop Python 3.7 soon).
`#5109 <https://github.com/pybind/pybind11/pull/5109>`_

* Releases now have artifact attestations, visible at
https://github.com/pybind/pybind11/attestations.
`#5196 <https://github.com/pybind/pybind11/pull/5196>`_

Other:

* Some cleanup in preparation for 3.13 support.
`#5137 <https://github.com/pybind/pybind11/pull/5137>`_

* Avoid a warning by ensuring an iterator end check is included in release mode.
`#5129 <https://github.com/pybind/pybind11/pull/5129>`_

* Bump max cmake to 3.29.
`#5075 <https://github.com/pybind/pybind11/pull/5075>`_

* Update docs and noxfile.
`#5071 <https://github.com/pybind/pybind11/pull/5071>`_


Version 2.12.0 (March 27, 2024)
-------------------------------

Expand Down
4 changes: 2 additions & 2 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 13
#define PYBIND11_VERSION_MINOR 14
#define PYBIND11_VERSION_PATCH 0.dev1

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

// Define some generic pybind11 helper macros for warning management.
//
Expand Down
74 changes: 74 additions & 0 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,55 @@ class Type : public type {

template <typename... Types>
class Union : public object {
PYBIND11_OBJECT_DEFAULT(Union, object, PyObject_Type)
using object::object;
};

template <typename T>
class Optional : public object {
PYBIND11_OBJECT_DEFAULT(Optional, object, PyObject_Type)
using object::object;
};

template <typename T>
class TypeGuard : public bool_ {
using bool_::bool_;
};

template <typename T>
class TypeIs : public bool_ {
using bool_::bool_;
};

class NoReturn : public none {
using none::none;
};

class Never : public none {
using none::none;
};

#if defined(__cpp_nontype_template_parameter_class)
template <size_t N>
struct StringLiteral {
constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); }
char name[N];
};

template <StringLiteral... StrLits>
class Literal : public object {
PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type)
};

// Example syntax for creating a TypeVar.
// typedef typing::TypeVar<"T"> TypeVarT;
template <StringLiteral>
class TypeVar : public object {
PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type)
using object::object;
};
#endif

PYBIND11_NAMESPACE_END(typing)

PYBIND11_NAMESPACE_BEGIN(detail)
Expand Down Expand Up @@ -153,5 +194,38 @@ struct handle_type_name<typing::Optional<T>> {
static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::TypeGuard<T>> {
static constexpr auto name = const_name("TypeGuard[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::TypeIs<T>> {
static constexpr auto name = const_name("TypeIs[") + make_caster<T>::name + const_name("]");
};

template <>
struct handle_type_name<typing::NoReturn> {
static constexpr auto name = const_name("NoReturn");
};

template <>
struct handle_type_name<typing::Never> {
static constexpr auto name = const_name("Never");
};

#if defined(__cpp_nontype_template_parameter_class)
template <typing::StringLiteral... Literals>
struct handle_type_name<typing::Literal<Literals...>> {
static constexpr auto name = const_name("Literal[")
+ pybind11::detail::concat(const_name(Literals.name)...)
+ const_name("]");
};
template <typing::StringLiteral StrLit>
struct handle_type_name<typing::TypeVar<StrLit>> {
static constexpr auto name = const_name(StrLit.name);
};
#endif

PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def tests_packaging(session: nox.Session) -> None:
Run the packaging tests.
"""

session.install("-r", "tests/requirements.txt")
session.install("-r", "tests/requirements.txt", "pip")
session.run("pytest", "tests/extra_python_package", *session.posargs)


Expand Down
2 changes: 1 addition & 1 deletion pybind11/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ def _to_int(s: str) -> int | str:
return s


__version__ = "2.13.0.dev1"
__version__ = "2.14.0.dev1"
version_info = tuple(_to_int(s) for s in __version__.split("."))
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ classifiers =
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
Programming Language :: Python :: 3.13
License :: OSI Approved :: BSD License
Programming Language :: Python :: Implementation :: PyPy
Programming Language :: Python :: Implementation :: CPython
Expand Down
57 changes: 56 additions & 1 deletion tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ void m_defs(py::module_ &m) {

} // namespace handle_from_move_only_type_with_operator_PyObject

#if defined(__cpp_nontype_template_parameter_class)
namespace literals {
enum Color { RED = 0, BLUE = 1 };

typedef py::typing::Literal<"26",
"0x1A",
"\"hello world\"",
"b\"hello world\"",
"u\"hello world\"",
"True",
"Color.RED",
"None">
LiteralFoo;
} // namespace literals
namespace typevar {
typedef py::typing::TypeVar<"T"> TypeVarT;
typedef py::typing::TypeVar<"V"> TypeVarV;
} // namespace typevar
#endif

TEST_SUBMODULE(pytypes, m) {
m.def("obj_class_name", [](py::handle obj) { return py::detail::obj_class_name(obj.ptr()); });

Expand Down Expand Up @@ -844,7 +864,8 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {});
m.def("annotate_fn",
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});
m.def("annotate_type", [](const py::typing::Type<int> &) {});

m.def("annotate_type", [](const py::typing::Type<int> &t) -> py::type { return t; });

m.def("annotate_union",
[](py::typing::List<py::typing::Union<py::str, py::int_, py::object>> l,
Expand All @@ -861,10 +882,44 @@ TEST_SUBMODULE(pytypes, m) {
[](py::typing::List<py::typing::Union<py::str>> &l)
-> py::typing::List<py::typing::Union<py::int_>> { return l; });

m.def("annotate_union_to_object",
[](py::typing::Union<int, py::str> &o) -> py::object { return o; });

m.def("annotate_optional",
[](py::list &list) -> py::typing::List<py::typing::Optional<py::str>> {
list.append(py::str("hi"));
list.append(py::none());
return list;
});

m.def("annotate_type_guard", [](py::object &o) -> py::typing::TypeGuard<py::str> {
return py::isinstance<py::str>(o);
});
m.def("annotate_type_is",
[](py::object &o) -> py::typing::TypeIs<py::str> { return py::isinstance<py::str>(o); });

m.def("annotate_no_return", []() -> py::typing::NoReturn { throw 0; });
m.def("annotate_never", []() -> py::typing::Never { throw 0; });

m.def("annotate_optional_to_object",
[](py::typing::Optional<int> &o) -> py::object { return o; });

#if defined(__cpp_nontype_template_parameter_class)
py::enum_<literals::Color>(m, "Color")
.value("RED", literals::Color::RED)
.value("BLUE", literals::Color::BLUE);

m.def("annotate_literal", [](literals::LiteralFoo &o) -> py::object { return o; });
m.def("annotate_generic_containers",
[](const py::typing::List<typevar::TypeVarT> &l) -> py::typing::List<typevar::TypeVarV> {
return l;
});

m.def("annotate_listT_to_T",
[](const py::typing::List<typevar::TypeVarT> &l) -> typevar::TypeVarT { return l[0]; });
m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; });
m.attr("if_defined__cpp_nontype_template_parameter_class") = true;
#else
m.attr("if_defined__cpp_nontype_template_parameter_class") = false;
#endif
}
Loading

0 comments on commit c4651ff

Please sign in to comment.