Skip to content

Commit 28920ce

Browse files
author
Vincent Palancher
committed
types: Handles conversion of dictionary views. #45275
We handle the conversion by treating values/items/keys dictionary views as simple lists. Change-Id: Icb20bd9252785ac495ab76bd93d10eebb43d91ee Reviewed-on: http://gerrit2.aldebaran.lan/1235 Reviewed-by: philippe.martin <philippe.martin@softbankrobotics.com> Reviewed-by: jmonnon <jmonnon@aldebaran.com> Tested-by: vincent.palancher <vincent.palancher@softbankrobotics.com>
1 parent f32e32d commit 28920ce

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/pytypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ AnyReference unwrapAsRef(pybind11::object& obj)
842842
::py::set(obj).size()),
843843
pybindObjPtr);
844844

845-
if (PyList_CheckExact(pyObjPtr))
845+
if (PyList_CheckExact(pyObjPtr) || PyDictViewSet_Check(pyObjPtr) || PyDictValues_Check(pyObjPtr))
846846
return AnyReference(instance<types::ListInterface<::py::object, ::py::list>>(), pybindObjPtr);
847847

848848
if (PyDict_CheckExact(pyObjPtr))

tests/test_types.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,39 @@ TEST_F(TypePassing, LogLevel)
412412
registerService();
413413
EXPECT_EQ(qi::LogLevel_Info, getService().call<int>("func"));
414414
}
415+
416+
TEST_F(TypePassing, DictViews)
417+
{
418+
exec(
419+
"class TestService:\n"
420+
" def func(self, kind):\n"
421+
" d = dict(one=1, two=2)\n"
422+
" if kind == 0:\n"
423+
" return d.keys()\n"
424+
" if kind == 1:\n"
425+
" return d.items()\n"
426+
" return d.values()\n"
427+
);
428+
registerService();
429+
430+
{ // Keys
431+
using Keys = std::set<std::string>;
432+
const Keys expected{ "one", "two" };
433+
EXPECT_EQ(expected, getService().call<Keys>("func", 0));
434+
}
435+
436+
{ // Items
437+
using Items = std::set<std::pair<std::string, int>>;
438+
const Items expected{ { "one", 1 }, { "two", 2 } };
439+
EXPECT_EQ(expected, getService().call<Items>("func", 1));
440+
}
441+
442+
443+
{ // Values
444+
using Values = std::vector<int>;
445+
const Values expected{ 1, 2 };
446+
auto values = getService().call<Values>("func", 2);
447+
std::sort(values.begin(), values.end());
448+
EXPECT_EQ(expected, values);
449+
}
450+
}

0 commit comments

Comments
 (0)