mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 22:52:01 +00:00
Added support for list insertion. (#1888)
This commit is contained in:
parent
19189b4c2c
commit
12e8774bc9
@ -1265,6 +1265,10 @@ public:
|
|||||||
template <typename T> void append(T &&val) const {
|
template <typename T> void append(T &&val) const {
|
||||||
PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr());
|
PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr());
|
||||||
}
|
}
|
||||||
|
template <typename T> void insert(size_t index, T &&val) const {
|
||||||
|
PyList_Insert(m_ptr, static_cast<ssize_t>(index),
|
||||||
|
detail::object_or_cast(std::forward<T>(val)).ptr());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class args : public tuple { PYBIND11_OBJECT_DEFAULT(args, tuple, PyTuple_Check) };
|
class args : public tuple { PYBIND11_OBJECT_DEFAULT(args, tuple, PyTuple_Check) };
|
||||||
|
@ -17,6 +17,8 @@ TEST_SUBMODULE(pytypes, m) {
|
|||||||
list.append("value");
|
list.append("value");
|
||||||
py::print("Entry at position 0:", list[0]);
|
py::print("Entry at position 0:", list[0]);
|
||||||
list[0] = py::str("overwritten");
|
list[0] = py::str("overwritten");
|
||||||
|
list.insert(0, "inserted-0");
|
||||||
|
list.insert(2, "inserted-2");
|
||||||
return list;
|
return list;
|
||||||
});
|
});
|
||||||
m.def("print_list", [](py::list list) {
|
m.def("print_list", [](py::list list) {
|
||||||
|
@ -9,14 +9,16 @@ from pybind11_tests import debug_enabled
|
|||||||
def test_list(capture, doc):
|
def test_list(capture, doc):
|
||||||
with capture:
|
with capture:
|
||||||
lst = m.get_list()
|
lst = m.get_list()
|
||||||
assert lst == ["overwritten"]
|
assert lst == ["inserted-0", "overwritten", "inserted-2"]
|
||||||
|
|
||||||
lst.append("value2")
|
lst.append("value2")
|
||||||
m.print_list(lst)
|
m.print_list(lst)
|
||||||
assert capture.unordered == """
|
assert capture.unordered == """
|
||||||
Entry at position 0: value
|
Entry at position 0: value
|
||||||
list item 0: overwritten
|
list item 0: inserted-0
|
||||||
list item 1: value2
|
list item 1: overwritten
|
||||||
|
list item 2: inserted-2
|
||||||
|
list item 3: value2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert doc(m.get_list) == "get_list() -> list"
|
assert doc(m.get_list) == "get_list() -> list"
|
||||||
|
Loading…
Reference in New Issue
Block a user