type_caster_pyobject_ptr.h
Go to the documentation of this file.
1 // Copyright (c) 2023 The pybind Community.
2 
3 #pragma once
4 
5 #include "detail/common.h"
6 #include "detail/descr.h"
7 #include "cast.h"
8 #include "pytypes.h"
9 
12 
13 template <>
14 class type_caster<PyObject> {
15 public:
16  static constexpr auto name = const_name("object"); // See discussion under PR #4601.
17 
18  // This overload is purely to guard against accidents.
19  template <typename T,
21  static handle cast(T &&, return_value_policy, handle /*parent*/) {
23  "Invalid C++ type T for to-Python conversion (type_caster<PyObject>).");
24  return nullptr; // Unreachable.
25  }
26 
27  static handle cast(PyObject *src, return_value_policy policy, handle /*parent*/) {
28  if (src == nullptr) {
29  throw error_already_set();
30  }
31  if (PyErr_Occurred()) {
32  raise_from(PyExc_SystemError, "src != nullptr but PyErr_Occurred()");
33  throw error_already_set();
34  }
36  return src;
37  }
38  if (policy == return_value_policy::reference
40  return handle(src).inc_ref();
41  }
42  pybind11_fail("type_caster<PyObject>::cast(): unsupported return_value_policy: "
43  + std::to_string(static_cast<int>(policy)));
44  }
45 
46  bool load(handle src, bool) {
47  value = reinterpret_borrow<object>(src);
48  return true;
49  }
50 
51  template <typename T>
52  using cast_op_type = PyObject *;
53 
54  explicit operator PyObject *() { return value.ptr(); }
55 
56 private:
57  object value;
58 };
59 
name
Annotation for function names.
Definition: attr.h:51
cast.h
type_caster< PyObject >::cast
static handle cast(T &&, return_value_policy, handle)
Definition: type_caster_pyobject_ptr.h:21
error_already_set
Definition: pytypes.h:722
const_name
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: wrap/pybind11/include/pybind11/detail/common.h:485
type_caster_generic::value
void * value
Definition: type_caster_base.h:795
handle::inc_ref
const handle & inc_ref() const &
Definition: pytypes.h:249
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
detail
Definition: testSerializationNonlinear.cpp:70
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
raise_from
void raise_from(PyObject *type, const char *message)
Definition: pytypes.h:780
handle
Definition: pytypes.h:217
type_caster
Definition: cast.h:38
return_value_policy::reference
@ reference
type_caster< PyObject >::value
object value
Definition: type_caster_pyobject_ptr.h:57
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
Eigen::Triplet< double >
common.h
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: wrap/pybind11/include/pybind11/detail/common.h:1013
pytypes.h
descr.h
is_same_ignoring_cvref
std::is_same< detail::remove_cvref_t< T >, U > is_same_ignoring_cvref
Example usage: is_same_ignoring_cvref<T, PyObject *>::value.
Definition: wrap/pybind11/include/pybind11/detail/common.h:663
return_value_policy::take_ownership
@ take_ownership
type_caster< PyObject >::cast_op_type
PyObject * cast_op_type
Definition: type_caster_pyobject_ptr.h:52
type_caster< PyObject >::cast
static handle cast(PyObject *src, return_value_policy policy, handle)
Definition: type_caster_pyobject_ptr.h:27
type_caster< PyObject >::load
bool load(handle src, bool)
Definition: type_caster_pyobject_ptr.h:46
return_value_policy::automatic_reference
@ automatic_reference
test_callbacks.value
value
Definition: test_callbacks.py:158


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:11:27