File indexing completed on 2025-01-18 10:06:18
0001
0002
0003 #pragma once
0004
0005 #include "detail/common.h"
0006 #include "detail/descr.h"
0007 #include "cast.h"
0008 #include "pytypes.h"
0009
0010 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0011 PYBIND11_NAMESPACE_BEGIN(detail)
0012
0013 template <>
0014 class type_caster<PyObject> {
0015 public:
0016 static constexpr auto name = const_name("object");
0017
0018
0019 template <typename T,
0020 detail::enable_if_t<!is_same_ignoring_cvref<T, PyObject *>::value, int> = 0>
0021 static handle cast(T &&, return_value_policy, handle ) {
0022 static_assert(is_same_ignoring_cvref<T, PyObject *>::value,
0023 "Invalid C++ type T for to-Python conversion (type_caster<PyObject>).");
0024 return nullptr;
0025 }
0026
0027 static handle cast(PyObject *src, return_value_policy policy, handle ) {
0028 if (src == nullptr) {
0029 throw error_already_set();
0030 }
0031 if (PyErr_Occurred()) {
0032 raise_from(PyExc_SystemError, "src != nullptr but PyErr_Occurred()");
0033 throw error_already_set();
0034 }
0035 if (policy == return_value_policy::take_ownership) {
0036 return src;
0037 }
0038 if (policy == return_value_policy::reference
0039 || policy == return_value_policy::automatic_reference) {
0040 return handle(src).inc_ref();
0041 }
0042 pybind11_fail("type_caster<PyObject>::cast(): unsupported return_value_policy: "
0043 + std::to_string(static_cast<int>(policy)));
0044 }
0045
0046 bool load(handle src, bool) {
0047 value = reinterpret_borrow<object>(src);
0048 return true;
0049 }
0050
0051 template <typename T>
0052 using cast_op_type = PyObject *;
0053
0054 explicit operator PyObject *() { return value.ptr(); }
0055
0056 private:
0057 object value;
0058 };
0059
0060 PYBIND11_NAMESPACE_END(detail)
0061 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)