]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/src/object_protocol.cpp
1 // Copyright David Abrahams 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/python/object_protocol.hpp>
7 #include <boost/python/errors.hpp>
8 #include <boost/python/object.hpp>
9 #include <boost/python/ssize_t.hpp>
11 namespace boost
{ namespace python
{ namespace api
{
13 BOOST_PYTHON_DECL object
getattr(object
const& target
, object
const& key
)
15 return object(detail::new_reference(PyObject_GetAttr(target
.ptr(), key
.ptr())));
18 BOOST_PYTHON_DECL object
getattr(object
const& target
, object
const& key
, object
const& default_
)
20 PyObject
* result
= PyObject_GetAttr(target
.ptr(), key
.ptr());
21 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
))
26 return object(detail::new_reference(result
));
29 BOOST_PYTHON_DECL
void setattr(object
const& target
, object
const& key
, object
const& value
)
31 if (PyObject_SetAttr(target
.ptr(), key
.ptr(), value
.ptr()) == -1)
32 throw_error_already_set();
35 BOOST_PYTHON_DECL
void delattr(object
const& target
, object
const& key
)
37 if (PyObject_DelAttr(target
.ptr(), key
.ptr()) == -1)
38 throw_error_already_set();
41 BOOST_PYTHON_DECL object
getattr(object
const& target
, char const* key
)
44 detail::new_reference(
45 PyObject_GetAttrString(target
.ptr(), const_cast<char*>(key
))
49 BOOST_PYTHON_DECL object
getattr(object
const& target
, char const* key
, object
const& default_
)
51 PyObject
* result
= PyObject_GetAttrString(target
.ptr(), const_cast<char*>(key
));
52 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
))
57 return object(detail::new_reference(result
));
60 BOOST_PYTHON_DECL
void setattr(object
const& target
, char const* key
, object
const& value
)
62 if (PyObject_SetAttrString(
63 target
.ptr(), const_cast<char*>(key
), value
.ptr()) == -1
66 throw_error_already_set();
70 BOOST_PYTHON_DECL
void delattr(object
const& target
, char const* key
)
72 if (PyObject_DelAttrString(
73 target
.ptr(), const_cast<char*>(key
)) == -1
76 throw_error_already_set();
80 BOOST_PYTHON_DECL object
getitem(object
const& target
, object
const& key
)
82 return object(detail::new_reference(
83 PyObject_GetItem(target
.ptr(), key
.ptr())));
86 BOOST_PYTHON_DECL
void setitem(object
const& target
, object
const& key
, object
const& value
)
88 if (PyObject_SetItem(target
.ptr(), key
.ptr(), value
.ptr()) == -1)
89 throw_error_already_set();
92 BOOST_PYTHON_DECL
void delitem(object
const& target
, object
const& key
)
94 if (PyObject_DelItem(target
.ptr(), key
.ptr()) == -1)
95 throw_error_already_set();
98 namespace // slicing code copied directly out of the Python implementation
101 #define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
104 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
106 #if PY_VERSION_HEX < 0x03000000
107 PyTypeObject
*tp
= u
->ob_type
;
108 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
110 if (sq
&& sq
->sq_slice
&& ISINT(v
) && ISINT(w
)) {
111 ssize_t ilow
= 0, ihigh
= ssize_t_max
;
112 if (!_PyEval_SliceIndex(v
, &ilow
))
114 if (!_PyEval_SliceIndex(w
, &ihigh
))
116 return PySequence_GetSlice(u
, ilow
, ihigh
);
121 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
123 PyObject
*res
= PyObject_GetItem(u
, slice
);
133 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
136 #if PY_VERSION_HEX < 0x03000000
137 PyTypeObject
*tp
= u
->ob_type
;
138 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
140 if (sq
&& sq
->sq_slice
&& ISINT(v
) && ISINT(w
)) {
141 ssize_t ilow
= 0, ihigh
= ssize_t_max
;
142 if (!_PyEval_SliceIndex(v
, &ilow
))
144 if (!_PyEval_SliceIndex(w
, &ihigh
))
147 return PySequence_DelSlice(u
, ilow
, ihigh
);
149 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
154 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
158 res
= PyObject_SetItem(u
, slice
, x
);
160 res
= PyObject_DelItem(u
, slice
);
170 BOOST_PYTHON_DECL object
getslice(object
const& target
, handle
<> const& begin
, handle
<> const& end
)
173 detail::new_reference(
174 apply_slice(target
.ptr(), begin
.get(), end
.get())));
177 BOOST_PYTHON_DECL
void setslice(object
const& target
, handle
<> const& begin
, handle
<> const& end
, object
const& value
)
180 target
.ptr(), begin
.get(), end
.get(), value
.ptr()) == -1
183 throw_error_already_set();
187 BOOST_PYTHON_DECL
void delslice(object
const& target
, handle
<> const& begin
, handle
<> const& end
)
190 target
.ptr(), begin
.get(), end
.get(), 0) == -1
193 throw_error_already_set();
197 }}} // namespace boost::python::api