Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create sequences using initializer list #2451

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "detail/common.h"
#include "buffer_info.h"
#include <initializer_list>
#include <utility>
#include <type_traits>

Expand Down Expand Up @@ -1216,6 +1217,11 @@ class tuple : public object {
explicit tuple(size_t size = 0) : object(PyTuple_New((ssize_t) size), stolen_t{}) {
if (!m_ptr) pybind11_fail("Could not allocate tuple object!");
}
explicit tuple(std::initializer_list<object> init_list) : tuple(init_list.size()) {
size_t index {0};
for (const pybind11::object& item : init_list)
detail::tuple_accessor(*this, index++) = item;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, we could think about deprecating setting elements of a tuple, since tuples are supposed to be immutable once constructed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing for this PR ofc :-)

size_t size() const { return (size_t) PyTuple_Size(m_ptr); }
bool empty() const { return size() == 0; }
detail::tuple_accessor operator[](size_t index) const { return {*this, index}; }
Expand Down Expand Up @@ -1276,6 +1282,11 @@ class list : public object {
explicit list(size_t size = 0) : object(PyList_New((ssize_t) size), stolen_t{}) {
if (!m_ptr) pybind11_fail("Could not allocate list object!");
}
explicit list(std::initializer_list<object> init_list) : list(init_list.size()) {
size_t index {0};
for (const pybind11::object& item : init_list)
detail::list_accessor(*this, index++) = item;
}
size_t size() const { return (size_t) PyList_Size(m_ptr); }
bool empty() const { return size() == 0; }
detail::list_accessor operator[](size_t index) const { return {*this, index}; }
Expand Down
10 changes: 10 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ TEST_SUBMODULE(pytypes, m) {
);
});

// Tuples and lists can also be constructed using an initializer list of pybind11::object subclasses
m.def("initializer_list", []() {
return py::dict(
"tuple_ints"_a = py::tuple({py::int_(1), py::int_(2), py::int_(3)}),
"tuple_floats"_a = py::tuple({py::float_(2.2), py::float_(3.1), py::float_(4.5), py::float_(5.4)}),
"list_ints"_a = py::list({py::int_(1), py::int_(2), py::int_(3)}),
"list_floats"_a = py::list({py::float_(2.2), py::float_(3.1), py::float_(4.5), py::float_(5.4)})
cowo78 marked this conversation as resolved.
Show resolved Hide resolved
);
});

m.def("convert_to_pybind11_str", [](py::object o) { return py::str(o); });

m.def("get_implicit_casting", []() {
Expand Down
20 changes: 20 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,26 @@ def test_constructors():
for k in noconv2:
assert noconv2[k] is expected[k]

init_list = m.initializer_list()
expected = ("tuple_ints", tuple), ("list_ints", list)
for key, type_ in expected:
ints = init_list[key]
assert(isinstance(ints, type_))
assert(len(ints) == 3)
assert(ints[0] == 1)
assert(ints[1] == 2)
assert(ints[2] == 3)

expected = ("tuple_floats", tuple), ("list_floats", list)
for key, type_ in expected:
floats = init_list[key]
assert(isinstance(floats, type_))
assert(len(floats) == 4)
assert(floats[0] == 2.2)
assert(floats[1] == 3.1)
assert(floats[2] == 4.5)
assert(floats[3] == 5.4)
cowo78 marked this conversation as resolved.
Show resolved Hide resolved


def test_pybind11_str_raw_str():
# specifically to exercise pybind11::str::raw_str
Expand Down