Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python_tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,8 @@ def test_append_to_random_lists(db0_autocommit_fixture):
print(f"Prefix size = {db0.get_storage_stats()['prefix_size']} bytes")

db0.commit()


def test_list_tuple_indexed_access(db0_fixture):
db0_list = db0.list([1, 2, 3, 5, 6, 7, 8])
assert db0_list[(1, 3, 4)] == [2, 5, 6]
42 changes: 42 additions & 0 deletions src/dbzero/bindings/python/collections/PyList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,48 @@ namespace db0::python
return py_src_list->ext().getItem(index).steal();
}

// check for tuple of indices
if (PyTuple_Check(elem)) {
Py_ssize_t num_items = PyTuple_Size(elem);
std::vector<std::uint64_t> indices;
indices.reserve(num_items);

// Extract indices from tuple
for (Py_ssize_t i = 0; i < num_items; ++i) {
PyObject *py_item = PyTuple_GetItem(elem, i);
if (!PyLong_Check(py_item)) {
THROWF(db0::InputException) << "Expected integer indexes in the tuple";
}
auto index = PyLong_AsLongLong(py_item);
// Handle negative indices
if (index < 0) {
index += py_src_list->ext().size();
}
if (index < 0 || static_cast<std::size_t>(index) >= py_src_list->ext().size()) {
PyErr_SetString(PyExc_IndexError, "list index out of range");
return nullptr;
}
indices.push_back(static_cast<std::uint64_t>(index));
}

// Create result list
auto py_result = Py_OWN(PyList_New(num_items));
if (!py_result) {
return nullptr;
}

// Fetch items at specified indices
for (std::size_t i = 0; i < indices.size(); ++i) {
auto item = py_src_list->ext().getItem(indices[i]);
if (!item) {
return nullptr;
}
PySafeList_SetItem(*py_result, i, item);
}

return py_result.steal();
}

// Check if the key is a slice object
if (PySlice_Check(elem)) {
// Parse slice object to get start, stop, step
Expand Down