1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/converter/from_python.hpp>
#include <boost/python/converter/registrations.hpp>
#include <boost/python/converter/rvalue_from_python_data.hpp>
#include <boost/python/object/find_instance.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/detail/raw_pyobject.hpp>
#include <boost/python/cast.hpp>
#include <vector>
#include <algorithm>
namespace boost { namespace python { namespace converter {
// rvalue_from_python_stage1 -- do the first stage of a conversion
// from a Python object to a C++ rvalue.
//
// source - the Python object to be converted
// converters - the registry entry for the target type T
//
// Postcondition: where x is the result, one of:
//
// 1. x.convertible == 0, indicating failure
//
// 2. x.construct == 0, x.convertible is the address of an object of
// type T. Indicates a successful lvalue conversion
//
// 3. where y is of type rvalue_from_python_data<T>,
// x.construct(source, y) constructs an object of type T
// in y.storage.bytes and then sets y.convertible == y.storage.bytes,
// or else throws an exception and has no effect.
BOOST_PYTHON_DECL rvalue_from_python_stage1_data rvalue_from_python_stage1(
PyObject* source
, registration const& converters)
{
rvalue_from_python_stage1_data data;
// First check to see if it's embedded in an extension class
// instance, as a special case.
data.convertible = objects::find_instance_impl(source, converters.target_type, converters.is_shared_ptr);
data.construct = 0;
if (!data.convertible)
{
for (rvalue_from_python_chain const* chain = converters.rvalue_chain;
chain != 0;
chain = chain->next)
{
void* r = chain->convertible(source);
if (r != 0)
{
data.convertible = r;
data.construct = chain->construct;
break;
}
}
}
return data;
}
// rvalue_result_from_python -- return the address of a C++ object which
// can be used as the result of calling a Python function.
//
// src - the Python object to be converted
//
// data - a reference to the base part of a
// rvalue_from_python_data<T> object, where T is the
// target type of the conversion.
//
// Requires: data.convertible == ®istered<T>::converters
//
BOOST_PYTHON_DECL void* rvalue_result_from_python(
PyObject* src, rvalue_from_python_stage1_data& data)
{
// Retrieve the registration
// Cast in two steps for less-capable compilers
void const* converters_ = data.convertible;
registration const& converters = *static_cast<registration const*>(converters_);
// Look for an eligible converter
data = rvalue_from_python_stage1(src, converters);
return rvalue_from_python_stage2(src, data, converters);
}
BOOST_PYTHON_DECL void* rvalue_from_python_stage2(
PyObject* source, rvalue_from_python_stage1_data& data, registration const& converters)
{
if (!data.convertible)
{
handle<> msg(
#if PY_VERSION_HEX >= 0x03000000
::PyUnicode_FromFormat
#else
::PyString_FromFormat
#endif
(
"No registered converter was able to produce a C++ rvalue of type %s from this Python object of type %s"
, converters.target_type.name()
, source->ob_type->tp_name
));
PyErr_SetObject(PyExc_TypeError, msg.get());
throw_error_already_set();
}
// If a construct function was registered (i.e. we found an
// rvalue conversion), call it now.
if (data.construct != 0)
data.construct(source, &data);
// Return the address of the resulting C++ object
return data.convertible;
}
BOOST_PYTHON_DECL void* get_lvalue_from_python(
PyObject* source
, registration const& converters)
{
// Check to see if it's embedded in a class instance
void* x = objects::find_instance_impl(source, converters.target_type);
if (x)
return x;
lvalue_from_python_chain const* chain = converters.lvalue_chain;
for (;chain != 0; chain = chain->next)
{
void* r = chain->convert(source);
if (r != 0)
return r;
}
return 0;
}
namespace
{
// Prevent looping in implicit conversions. This could/should be
// much more efficient, but will work for now.
typedef std::vector<rvalue_from_python_chain const*> visited_t;
static visited_t visited;
inline bool visit(rvalue_from_python_chain const* chain)
{
visited_t::iterator const p = std::lower_bound(visited.begin(), visited.end(), chain);
if (p != visited.end() && *p == chain)
return false;
visited.insert(p, chain);
return true;
}
// RAII class for managing global visited marks.
struct unvisit
{
unvisit(rvalue_from_python_chain const* chain)
: chain(chain) {}
~unvisit()
{
visited_t::iterator const p = std::lower_bound(visited.begin(), visited.end(), chain);
assert(p != visited.end());
visited.erase(p);
}
private:
rvalue_from_python_chain const* chain;
};
}
BOOST_PYTHON_DECL bool implicit_rvalue_convertible_from_python(
PyObject* source
, registration const& converters)
{
if (objects::find_instance_impl(source, converters.target_type))
return true;
rvalue_from_python_chain const* chain = converters.rvalue_chain;
if (!visit(chain))
return false;
unvisit protect(chain);
for (;chain != 0; chain = chain->next)
{
if (chain->convertible(source))
return true;
}
return false;
}
namespace
{
void throw_no_lvalue_from_python(PyObject* source, registration const& converters, char const* ref_type)
{
handle<> msg(
#if PY_VERSION_HEX >= 0x03000000
::PyUnicode_FromFormat
#else
::PyString_FromFormat
#endif
(
"No registered converter was able to extract a C++ %s to type %s"
" from this Python object of type %s"
, ref_type
, converters.target_type.name()
, source->ob_type->tp_name
));
PyErr_SetObject(PyExc_TypeError, msg.get());
throw_error_already_set();
}
void* lvalue_result_from_python(
PyObject* source
, registration const& converters
, char const* ref_type)
{
handle<> holder(source);
if (source->ob_refcnt <= 1)
{
handle<> msg(
#if PY_VERSION_HEX >= 0x3000000
::PyUnicode_FromFormat
#else
::PyString_FromFormat
#endif
(
"Attempt to return dangling %s to object of type: %s"
, ref_type
, converters.target_type.name()));
PyErr_SetObject(PyExc_ReferenceError, msg.get());
throw_error_already_set();
}
void* result = get_lvalue_from_python(source, converters);
if (!result)
(throw_no_lvalue_from_python)(source, converters, ref_type);
return result;
}
}
BOOST_PYTHON_DECL void throw_no_pointer_from_python(PyObject* source, registration const& converters)
{
(throw_no_lvalue_from_python)(source, converters, "pointer");
}
BOOST_PYTHON_DECL void throw_no_reference_from_python(PyObject* source, registration const& converters)
{
(throw_no_lvalue_from_python)(source, converters, "reference");
}
BOOST_PYTHON_DECL void* reference_result_from_python(
PyObject* source
, registration const& converters)
{
return (lvalue_result_from_python)(source, converters, "reference");
}
BOOST_PYTHON_DECL void* pointer_result_from_python(
PyObject* source
, registration const& converters)
{
if (source == Py_None)
{
Py_DECREF(source);
return 0;
}
return (lvalue_result_from_python)(source, converters, "pointer");
}
BOOST_PYTHON_DECL void void_result_from_python(PyObject* o)
{
Py_DECREF(expect_non_null(o));
}
} // namespace boost::python::converter
BOOST_PYTHON_DECL PyObject*
pytype_check(PyTypeObject* type_, PyObject* source)
{
if (!PyObject_IsInstance(source, python::upcast<PyObject>(type_)))
{
::PyErr_Format(
PyExc_TypeError
, "Expecting an object of type %s; got an object of type %s instead"
, type_->tp_name
, source->ob_type->tp_name
);
throw_error_already_set();
}
return source;
}
}} // namespace boost::python
|