14 std::vector<PyObject *> vec_obj;
15 for (
int i = 1;
i < 3;
i++) {
23 #if defined(__clang_major__) && __clang_major__ < 4
40 #if !defined(PYPY_VERSION) // It is not worth the trouble doing something special for PyPy.
41 if (Py_REFCNT(returned_obj) != 1) {
45 auto ret_val =
py::repr(returned_obj).
cast<std::string>();
46 Py_DECREF(returned_obj);
55 m.def(
"cast_from_pyobject_ptr", []() {
56 PyObject *ptr = PyLong_FromLongLong(6758
L);
57 return py::cast(ptr, py::return_value_policy::take_ownership);
59 m.def(
"cast_handle_to_pyobject_ptr", [](py::handle obj) {
60 auto rc1 = obj.ref_count();
61 auto *ptr = py::cast<PyObject *>(obj);
62 auto rc2 = obj.ref_count();
66 return 100 - py::reinterpret_steal<py::object>(ptr).attr(
"value").cast<
int>();
68 m.def(
"cast_object_to_pyobject_ptr", [](py::object obj) {
70 auto rc1 = hdl.ref_count();
71 auto *ptr = py::cast<PyObject *>(std::move(obj));
72 auto rc2 = hdl.ref_count();
76 return 300 - py::reinterpret_steal<py::object>(ptr).attr(
"value").cast<
int>();
78 m.def(
"cast_list_to_pyobject_ptr", [](py::list lst) {
81 auto rc1 = hdl.ref_count();
82 auto *ptr = py::cast<PyObject *>(std::move(lst));
83 auto rc2 = hdl.ref_count();
87 return 400 -
static_cast<int>(
py::len(py::reinterpret_steal<py::list>(ptr)));
91 "return_pyobject_ptr",
92 []() {
return PyLong_FromLongLong(2314
L); },
93 py::return_value_policy::take_ownership);
94 m.def(
"pass_pyobject_ptr", [](PyObject *ptr) {
95 return 200 - py::reinterpret_borrow<py::object>(ptr).attr(
"value").cast<
int>();
98 m.def(
"call_callback_with_object_return",
99 [](
const std::function<py::object(
int)> &cb,
int value) {
return cb(
value); });
101 "call_callback_with_pyobject_ptr_return",
102 [](
const std::function<PyObject *(
int)> &cb,
int value) {
return cb(
value); },
103 py::return_value_policy::take_ownership);
105 "call_callback_with_pyobject_ptr_arg",
106 [](
const std::function<
int(PyObject *)> &cb, py::handle obj) {
return cb(obj.ptr()); },
110 m.def(
"cast_to_pyobject_ptr_nullptr", [](
bool set_error) {
112 py::set_error(PyExc_RuntimeError,
"Reflective of healthy error handling.");
114 PyObject *ptr =
nullptr;
118 m.def(
"cast_to_pyobject_ptr_non_nullptr_with_error_set", []() {
119 py::set_error(PyExc_RuntimeError,
"Reflective of unhealthy error handling.");
123 m.def(
"pass_list_pyobject_ptr", [](
const std::vector<PyObject *> &vec_obj) {
125 for (
const auto &ptr : vec_obj) {
126 acc = acc * 1000 + py::reinterpret_borrow<py::object>(ptr).attr(
"value").cast<
int>();
131 m.def(
"return_list_pyobject_ptr_take_ownership",
134 py::return_value_policy::take_ownership);
136 m.def(
"return_list_pyobject_ptr_reference",
139 py::return_value_policy::reference);
141 m.def(
"dec_ref_each_pyobject_ptr", [](
const std::vector<PyObject *> &vec_obj) {
143 for (;
i < vec_obj.size();
i++) {
144 py::handle
h(vec_obj[
i]);
153 m.def(
"pass_pyobject_ptr_and_int", [](PyObject *,
int) {});
155 #ifdef PYBIND11_NO_COMPILE_SECTION // Change to ifndef for manual testing.
157 PyObject *ptr =
nullptr;
162 py::class_<WithPyObjectPtrReturn, WithPyObjectPtrReturnTrampoline>(
m,
"WithPyObjectPtrReturn")