Skip to content

Commit 1f0677a

Browse files
committed
Add debug output
1 parent 429ec5d commit 1f0677a

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

include/pybind11/stl.h

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "detail/common.h"
1414

1515
#include <deque>
16+
#include <iostream>
1617
#include <list>
1718
#include <map>
1819
#include <ostream>
@@ -116,6 +117,10 @@ struct map_caster {
116117
void reserve_maybe(const dict &, void *) {}
117118

118119
public:
120+
#define DEBUG(value) std::cerr << __func__ << ": " << &value << " #: " << value.size() << "\n"
121+
122+
map_caster() { DEBUG(value); }
123+
~map_caster() { DEBUG(value); }
119124
bool load(handle src, bool convert) {
120125
if (!isinstance<dict>(src)) {
121126
return false;
@@ -131,11 +136,13 @@ struct map_caster {
131136
}
132137
value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
133138
}
139+
DEBUG(value);
134140
return true;
135141
}
136142

137143
template <typename T>
138144
static handle cast(T &&src, return_value_policy policy, handle parent) {
145+
DEBUG(src);
139146
dict d;
140147
return_value_policy policy_key = policy;
141148
return_value_policy policy_value = policy;
@@ -156,9 +163,40 @@ struct map_caster {
156163
return d.release();
157164
}
158165

159-
PYBIND11_TYPE_CASTER(Type,
160-
const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name
161-
+ const_name("]"));
166+
protected:
167+
Type value;
168+
169+
public:
170+
static constexpr auto name = _("Dict[") + key_conv::name + _(", ") + value_conv::name + _("]");
171+
template <typename T, enable_if_t<std::is_same<Type, remove_cv_t<T>>::value, int> = 0>
172+
static handle cast(T *src, return_value_policy policy, handle parent) {
173+
if (!src)
174+
return none().release();
175+
if (policy == return_value_policy::take_ownership) {
176+
auto h = cast(std::move(*src), policy, parent);
177+
delete src;
178+
return h;
179+
} else {
180+
return cast(*src, policy, parent);
181+
}
182+
}
183+
operator Type *() {
184+
DEBUG(value);
185+
return &value;
186+
}
187+
operator Type &() {
188+
DEBUG(value);
189+
return value;
190+
}
191+
operator Type &&() && {
192+
DEBUG(value);
193+
return std::move(value);
194+
}
195+
196+
template <typename T>
197+
using cast_op_type = pybind11::detail::movable_cast_op_type<T>;
198+
199+
#undef DEBUG
162200
};
163201

164202
template <typename Type, typename Value>

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ set(PYBIND11_TEST_PREFIX_COMMAND
526526
# A single command to compile and run the tests
527527
add_custom_target(
528528
pytest
529-
COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest
529+
COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest -v
530530
${PYBIND11_ABS_PYTEST_FILES}
531531
DEPENDS ${test_targets}
532532
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"

tests/test_stl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ TEST_SUBMODULE(stl, m) {
204204
m.def("load_map", [](const std::map<std::string, std::string> &map) {
205205
return map.at("key") == "value" && map.at("key2") == "value2";
206206
});
207-
m.def("copy_map", [](std::map<std::string, double> map) { auto m = std::move(map); });
207+
m.def("copy_map", [](std::map<std::string, double> map) {
208+
std::cerr << "copy_map: " << &map << " #: " << map.size() << std::endl;
209+
auto m = std::move(map);
210+
});
208211

209212
// test_set
210213
m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; });

0 commit comments

Comments
 (0)