2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 05:02:17 +00:00

Initial attempt to fix problems

[SVN r10158]
This commit is contained in:
Dave Abrahams
2001-05-19 23:29:04 +00:00
parent 9261e2a3d9
commit 4d3079293d
26 changed files with 339 additions and 316 deletions

View File

@@ -75,7 +75,7 @@ BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug workaround
from_python(x, boost::python::type<long>()));
}
PyObject* to_python(MyEnumType x)
PyObject* to_python(boost::python::semantics, MyEnumType x)
{
return to_python(static_cast<long>(x));
}
@@ -91,8 +91,8 @@ initialization. These bind the corresponding enum values to the appropriate
names so they can be used from Python:
<blockquote><pre>
mymodule.add(boost::python::to_python(enum_value_1), "enum_value_1");
mymodule.add(boost::python::to_python(enum_value_2), "enum_value_2");
mymodule.add(boost::python::to_python(boost::python::search_namespace, enum_value_1), "enum_value_1");
mymodule.add(boost::python::to_python(boost::python::search_namespace, enum_value_2), "enum_value_2");
...
</pre></blockquote>
@@ -100,8 +100,8 @@ You can also add these to an extension class definition, if your enum happens to
be local to a class and you want the analogous interface in Python:
<blockquote><pre>
my_class_builder.add(boost::python::to_python(enum_value_1), "enum_value_1");
my_class_builder.add(boost::python::to_python(enum_value_2), "enum_value_2");
my_class_builder.add(boost::python::to_python(boost::python::search_namespace, enum_value_1), "enum_value_1");
my_class_builder.add(boost::python::to_python(boost::python::search_namespace, enum_value_2), "enum_value_2");
...
</pre></blockquote>
<p>

View File

@@ -65,9 +65,9 @@ wrapped <code>T</code>, you may want to provide an automatic
thin wrappers. You can do this simply as follows:
<blockquote><pre>
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug workaround
PyObject* to_python(const Foo* p) {
return to_python(*p); // convert const Foo* in terms of const Foo&
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is an MSVC / gcc 2.95.2 bug workaround
PyObject* to_python(boost::python::semantics, const Foo* p) {
return to_python(boost::python::search_namespace, *p); // convert const Foo* in terms of const Foo&
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
</pre></blockquote>
@@ -83,12 +83,12 @@ code before the last Python reference to it disappears:
<blockquote><pre>
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug workaround
PyObject* to_python(Foo* p)
PyObject* to_python(boost::python::semantics, Foo* p)
{
return boost::python::python_extension_class_converters&ltFoo&gt::ptr_to_python(p);
}
PyObject* to_python(const Foo* p)
PyObject* to_python(boost::python::semantics, const Foo* p)
{
return to_python(const_cast&lt;Foo*&gt;(p));
}

View File

@@ -699,7 +699,7 @@ void throw_key_error_if_end(
{
if (p == m.end())
{
PyErr_SetObject(PyExc_KeyError, boost::python::converters::to_python(key));
PyErr_SetObject(PyExc_KeyError, to_python(boost::python::search_namespace, key));
throw boost::python::error_already_set();
}
}

View File

@@ -27,7 +27,7 @@
<a href="example1.html#Constructor_example">provide an argument list</a>
because they can't be named in C++). Then, it calls the appropriate
overloaded functions <code>PyObject*
to_python(</code><em>S</em><code>)</code> and <em>
to_python(boost::python::semantics, </code><em>S</em><code>)</code> and <em>
S'</em><code>from_python(PyObject*,
type&lt;</code><em>S</em><code>&gt;)</code> which convert between any C++
type <em>S</em> and a <code>PyObject*</code>, the type which represents a

View File

@@ -95,11 +95,11 @@ BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
// Convert a MillerIndex object to a Python tuple.
//
PyObject* to_python(const MillerIndex& hkl)
PyObject* to_python(python::semantics, const MillerIndex& hkl)
{
python::tuple result(3);
for (int i = 0; i < 3; i++)
result.set_item(i, python::ref(to_python(hkl.v[i])));
result.set_item(i, hkl.v[i]);
return result.reference().release();
}

View File

@@ -14,16 +14,14 @@ namespace vects {
{
std::vector<double>::iterator v_it = begin();
for (int i = 0; i < tuple.size(); i++)
v_it[i] = BOOST_PYTHON_CONVERSION::from_python(tuple[i].get(),
boost::python::type<double>());
v_it[i] = from_python(tuple[i].get(), boost::python::type<double>());
}
boost::python::tuple as_tuple() const
{
boost::python::tuple t(size());
for (int i = 0; i < size(); i++)
t.set_item(i,
boost::python::ref(BOOST_PYTHON_CONVERSION::to_python((*this)[i])));
t.set_item(i, (*this)[i]);
return t;
}
};

View File

@@ -14,16 +14,14 @@ namespace vects {
{
std::vector<int>::iterator v_it = begin();
for (int i = 0; i < tuple.size(); i++)
v_it[i] = BOOST_PYTHON_CONVERSION::from_python(tuple[i].get(),
boost::python::type<int>());
v_it[i] = from_python(tuple[i].get(), boost::python::type<int>());
}
boost::python::tuple as_tuple() const
{
boost::python::tuple t(size());
for (int i = 0; i < size(); i++)
t.set_item(i,
boost::python::ref(BOOST_PYTHON_CONVERSION::to_python((*this)[i])));
t.set_item(i, (*this)[i]);
return t;
}
};

View File

@@ -45,8 +45,6 @@ namespace { // Avoid cluttering the global namespace.
// Support for pickle.
using BOOST_PYTHON_CONVERSION::from_python;
python::ref world_getinitargs(const world& w) {
python::tuple result(1);
result.set_item(0, w.get_country());

View File

@@ -94,7 +94,6 @@ BOOST_PYTHON_MODULE_INIT(pickle3)
namespace {
using BOOST_PYTHON_CONVERSION::from_python;
using boost::python::type;
using boost::python::ref;
using boost::python::tuple;

View File

@@ -25,8 +25,7 @@ namespace { // Avoid cluttering the global namespace.
{
std::vector<double>::iterator vd = begin();
for (int i = 0; i < tuple.size(); i++)
vd[i] = BOOST_PYTHON_CONVERSION::from_python(tuple[i].get(),
python::type<double>());
vd[i] = from_python(tuple[i].get(), python::type<double>());
}
};
@@ -57,8 +56,8 @@ namespace { // Avoid cluttering the global namespace.
python::tuple as_tuple(const std::vector<double>& vd)
{
python::tuple t(vd.size());
for (int i = 0; i < vd.size(); i++) t.set_item(i,
python::ref(BOOST_PYTHON_CONVERSION::to_python(vd[i])));
for (int i = 0; i < vd.size(); i++)
t.set_item(i, vd[i]);
return t;
}

View File

@@ -19,117 +19,118 @@ class class_builder
: python_extension_class_converters<T, U> // Works around MSVC6.x/GCC2.95.2 bug described below
{
public:
/// Construct a class named name in the given module
class_builder(module_builder& module, const char* name)
: m_class(new detail::extension_class<T, U>(name))
{
module.add(ref(as_object(m_class.get()), ref::increment_count), name);
}
~class_builder()
{}
/// Tell Boost.Python that for the purposes of pickling, the state is
/// completely captured in the object's __dict__.
inline void dict_defines_state() {
add(ref(BOOST_PYTHON_CONVERSION::to_python(1)), "__dict_defines_state__");
}
inline void getstate_manages_dict() {
add(ref(BOOST_PYTHON_CONVERSION::to_python(1)), "__getstate_manages_dict__");
add(ref(to_python(search_namespace, 1)), "__dict_defines_state__");
}
// define constructors
inline void getstate_manages_dict() {
add(ref(to_python(search_namespace, 1)), "__getstate_manages_dict__");
}
/// define constructors
template <class signature>
void def(const signature& s)
{ m_class->def(s); }
// export heterogeneous reverse-argument operators
// (type of lhs: 'left', of rhs: 'right')
// usage: foo_class.def(boost::python::operators<(boost::python::op_add | boost::python::op_sub), Foo>(),
// boost::python::left_operand<int const &>());
/// export heterogeneous reverse-argument operators
/// (type of lhs: 'left', of rhs: 'right')
/// usage: foo_class.def(boost::python::operators<(boost::python::op_add | boost::python::op_sub), Foo>(),
/// boost::python::left_operand<int const &>());
template <long which, class left, class right>
void def(operators<which, right> o1, left_operand<left> o2)
{ m_class->def(o1, o2); }
// export heterogeneous operators (type of lhs: 'left', of rhs: 'right')
// usage: foo_class.def(boost::python::operators<(boost::python::op_add | boost::python::op_sub), Foo>(),
// boost::python::right_operand<int const &>());
/// export heterogeneous operators (type of lhs: 'left', of rhs: 'right')
/// usage: foo_class.def(boost::python::operators<(boost::python::op_add | boost::python::op_sub), Foo>(),
/// boost::python::right_operand<int const &>());
template <long which, class left, class right>
void def(operators<which, left> o1, right_operand<right> o2)
{ m_class->def(o1, o2); }
// define a function that passes Python arguments and keywords
// to C++ verbatim (as a 'tuple const &' and 'dictionary const &'
// respectively). This is useful for manual argument passing.
// It's also the only possibility to pass keyword arguments to C++.
// Fn must have a signatur that is compatible to
// PyObject * (*)(PyObject * aTuple, PyObject * aDictionary)
/// define a function that passes Python arguments and keywords
/// to C++ verbatim (as a 'tuple const &' and 'dictionary const &'
/// respectively). This is useful for manual argument passing.
/// It's also the only way to pass keyword arguments to C++.
/// Fn must have a signature that is compatible with
/// PyObject * (*)(PyObject * aTuple, PyObject * aDictionary)
template <class Fn>
void def_raw(Fn fn, const char* name)
{ m_class->def_raw(fn, name); }
// define member functions. In fact this works for free functions, too -
// they act like static member functions, or if they start with the
// appropriate self argument (as a pointer or reference), they can be used
// just like ordinary member functions -- just like Python!
/// define member functions. In fact this works for free functions, too -
/// they act like static member functions, or if they start with the
/// appropriate self argument (as a pointer or reference), they can be used
/// just like ordinary member functions -- just like Python!
template <class Fn>
void def(Fn fn, const char* name)
{ m_class->def(fn, name); }
// Define a virtual member function with a default implementation.
// default_fn should be a function which provides the default implementation.
// Be careful that default_fn does not in fact call fn virtually!
/// Define a virtual member function with a default implementation.
/// default_fn should be a function which provides the default implementation.
/// Be careful that default_fn does not in fact call fn virtually!
template <class Fn, class DefaultFn>
void def(Fn fn, const char* name, DefaultFn default_fn)
{ m_class->def(fn, name, default_fn); }
// Provide a function which implements x.<name>, reading from the given
// member (pm) of the T obj
/// Provide a function which implements x.<name>, reading from the given
/// member (pm) of the T obj
template <class MemberType>
void def_getter(MemberType T::*pm, const char* name)
{ m_class->def_getter(pm, name); }
// Provide a function which implements assignment to x.<name>, writing to
// the given member (pm) of the T obj
/// Provide a function which implements assignment to x.<name>, writing to
/// the given member (pm) of the T obj
template <class MemberType>
void def_setter(MemberType T::*pm, const char* name)
{ m_class->def_getter(pm, name); }
// Expose the given member (pm) of the T obj as a read-only attribute
/// Expose the given member (pm) of the T obj as a read-only attribute
template <class MemberType>
void def_readonly(MemberType T::*pm, const char* name)
{ m_class->def_readonly(pm, name); }
// Expose the given member (pm) of the T obj as a read/write attribute
/// Expose the given member (pm) of the T obj as a read/write attribute
template <class MemberType>
void def_read_write(MemberType T::*pm, const char* name)
{ m_class->def_read_write(pm, name); }
// define the standard coercion needed for operator overloading
/// define the standard coercion needed for operator overloading
void def_standard_coerce()
{ m_class->def_standard_coerce(); }
// declare the given class a base class of this one and register
// conversion functions
/// declare the given class a base class of this one and register
/// conversion functions
template <class S, class V>
void declare_base(class_builder<S, V> const & base)
{
m_class->declare_base(base.get_extension_class());
}
// declare the given class a base class of this one and register
// upcast conversion function
/// declare the given class a base class of this one and register
/// upcast conversion function
template <class S, class V>
void declare_base(class_builder<S, V> const & base, without_downcast_t)
{
m_class->declare_base(base.get_extension_class(), without_downcast);
}
// get the embedded ExtensioClass object
/// get the embedded ExtensioClass object
detail::extension_class<T, U> * get_extension_class() const
{
return m_class.get();
}
// set an arbitrary attribute. Useful for non-function class data members,
// e.g. enums
/// set an arbitrary attribute. Useful for non-function class data members,
/// e.g. enums
void add(PyObject* x, const char* name)
{ m_class->set_attribute(name, x); }
void add(ref x, const char* name)
@@ -143,8 +144,8 @@ class class_builder
m_class->declare_base(base);
}
// declare the given class a base class of this one and register
// upcast conversion function
/// declare the given class a base class of this one and register
/// upcast conversion function
template <class S, class V>
void declare_base(detail::extension_class<S, V> * base, without_downcast_t)
{

View File

@@ -284,14 +284,14 @@ PyObject* class_t<T>::instance_mapping_subscript(PyObject* obj, PyObject* key) c
template <class T>
PyObject* class_t<T>::instance_sequence_item(PyObject* obj, int n) const
{
ref key(to_python(n));
ref key(to_python(search_namespace, n));
return downcast<T>(obj)->get_subscript(key.get());
}
template <class T>
int class_t<T>::instance_sequence_ass_item(PyObject* obj, int n, PyObject* value) const
{
ref key(to_python(n));
ref key(to_python(search_namespace, n));
downcast<T>(obj)->set_subscript(key.get(), value);
return 0;
}

View File

@@ -52,9 +52,9 @@ class py_enum_as_int_converters
from_python(x, boost::python::type<long>()));
}
friend PyObject* to_python(EnumType x)
friend PyObject* to_python(boost::python::semantics, EnumType x)
{
return to_python(static_cast<long>(x));
return to_python(boost::python::search_namespace, static_cast<long>(x));
}
};
BOOST_PYTHON_END_CONVERSION_NAMESPACE
@@ -116,70 +116,70 @@ BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
//
// Converters
//
PyObject* to_python(long);
PyObject* to_python(boost::python::search_namespace, long);
long from_python(PyObject* p, boost::python::type<long>);
long from_python(PyObject* p, boost::python::type<const long&>);
PyObject* to_python(unsigned long);
PyObject* to_python(boost::python::search_namespace, unsigned long);
unsigned long from_python(PyObject* p, boost::python::type<unsigned long>);
unsigned long from_python(PyObject* p, boost::python::type<const unsigned long&>);
PyObject* to_python(int);
PyObject* to_python(boost::python::search_namespace, int);
int from_python(PyObject*, boost::python::type<int>);
int from_python(PyObject*, boost::python::type<const int&>);
PyObject* to_python(unsigned int);
PyObject* to_python(boost::python::search_namespace, unsigned int);
unsigned int from_python(PyObject*, boost::python::type<unsigned int>);
unsigned int from_python(PyObject*, boost::python::type<const unsigned int&>);
PyObject* to_python(short);
PyObject* to_python(boost::python::search_namespace, short);
short from_python(PyObject*, boost::python::type<short>);
short from_python(PyObject*, boost::python::type<const short&>);
PyObject* to_python(unsigned short);
PyObject* to_python(boost::python::search_namespace, unsigned short);
unsigned short from_python(PyObject*, boost::python::type<unsigned short>);
unsigned short from_python(PyObject*, boost::python::type<const unsigned short&>);
PyObject* to_python(char);
PyObject* to_python(boost::python::search_namespace, char);
char from_python(PyObject*, boost::python::type<char>);
char from_python(PyObject*, boost::python::type<const char&>);
PyObject* to_python(signed char);
PyObject* to_python(boost::python::search_namespace, signed char);
signed char from_python(PyObject*, boost::python::type<signed char>);
signed char from_python(PyObject*, boost::python::type<const signed char&>);
PyObject* to_python(unsigned char);
PyObject* to_python(boost::python::search_namespace, unsigned char);
unsigned char from_python(PyObject*, boost::python::type<unsigned char>);
unsigned char from_python(PyObject*, boost::python::type<const unsigned char&>);
PyObject* to_python(float);
PyObject* to_python(boost::python::search_namespace, float);
float from_python(PyObject*, boost::python::type<float>);
float from_python(PyObject*, boost::python::type<const float&>);
PyObject* to_python(double);
PyObject* to_python(boost::python::search_namespace, double);
double from_python(PyObject*, boost::python::type<double>);
double from_python(PyObject*, boost::python::type<const double&>);
PyObject* to_python(bool);
PyObject* to_python(boost::python::search_namespace, bool);
bool from_python(PyObject*, boost::python::type<bool>);
bool from_python(PyObject*, boost::python::type<const bool&>);
PyObject* to_python(void);
PyObject* to_python(boost::python::search_namespace, void);
void from_python(PyObject*, boost::python::type<void>);
PyObject* to_python(const char* s);
PyObject* to_python(boost::python::search_namespace, const char* s);
const char* from_python(PyObject*, boost::python::type<const char*>);
PyObject* to_python(const std::string& s);
PyObject* to_python(boost::python::search_namespace, const std::string& s);
std::string from_python(PyObject*, boost::python::type<std::string>);
std::string from_python(PyObject*, boost::python::type<const std::string&>);
inline PyObject* to_python(const std::complex<float>& x)
inline PyObject* to_python(boost::python::search_namespace, const std::complex<float>& x)
{
return boost::python::detail::complex_to_python<float>(x);
}
inline PyObject* to_python(const std::complex<double>& x)
inline PyObject* to_python(boost::python::search_namespace, const std::complex<double>& x)
{
return boost::python::detail::complex_to_python<double>(x);
}
@@ -205,7 +205,7 @@ inline std::complex<float> from_python(PyObject* p,
}
// For when your C++ function really wants to pass/return a PyObject*
PyObject* to_python(PyObject*);
PyObject* to_python(boost::python::search_namespace, PyObject*);
PyObject* from_python(PyObject*, boost::python::type<PyObject*>);
// Some standard conversions to/from smart pointer types. You can add your own
@@ -259,13 +259,13 @@ boost::shared_ptr<T> from_python(PyObject*p, boost::python::type<boost::shared_p
#if 0
template <class T>
PyObject* to_python(std::auto_ptr<T> p)
PyObject* to_python(boost::python::search_namespace, std::auto_ptr<T> p)
{
return new boost::python::wrapped_pointer<std::auto_ptr<T>, T>(p);
}
template <class T>
PyObject* to_python(boost::shared_ptr<T> p)
PyObject* to_python(boost::python::search_namespace, boost::shared_ptr<T> p)
{
return new boost::python::wrapped_pointer<boost::shared_ptr<T>, T>(p);
}
@@ -276,43 +276,43 @@ PyObject* to_python(boost::shared_ptr<T> p)
//
#ifndef BOOST_MSVC6_OR_EARLIER
inline PyObject* to_python(double d)
inline PyObject* to_python(boost::python::search_namespace, double d)
{
return PyFloat_FromDouble(d);
}
inline PyObject* to_python(float f)
inline PyObject* to_python(boost::python::search_namespace, float f)
{
return PyFloat_FromDouble(f);
}
#endif // BOOST_MSVC6_OR_EARLIER
inline PyObject* to_python(long l)
inline PyObject* to_python(boost::python::search_namespace, long l)
{
return PyInt_FromLong(l);
}
inline PyObject* to_python(int x)
inline PyObject* to_python(boost::python::search_namespace, int x)
{
return PyInt_FromLong(x);
}
inline PyObject* to_python(short x)
inline PyObject* to_python(boost::python::search_namespace, short x)
{
return PyInt_FromLong(x);
}
inline PyObject* to_python(bool b)
inline PyObject* to_python(boost::python::search_namespace, bool b)
{
return PyInt_FromLong(b);
}
inline PyObject* to_python(void)
inline PyObject* to_python(boost::python::search_namespace, void)
{
return boost::python::detail::none();
}
inline PyObject* to_python(const char* s)
inline PyObject* to_python(boost::python::search_namespace, const char* s)
{
return PyString_FromString(s);
}
@@ -322,7 +322,7 @@ inline std::string from_python(PyObject* p, boost::python::type<const std::strin
return from_python(p, boost::python::type<std::string>());
}
inline PyObject* to_python(PyObject* p)
inline PyObject* to_python(boost::python::search_namespace, PyObject* p)
{
Py_INCREF(p);
return p;

View File

@@ -66,8 +66,8 @@ class python_import_extension_class_converters
return python_import_extension_class_converters();
}
PyObject* to_python(const T& x) const {
return boost::python::detail::import_extension_class<T>::get_converters()->to_python(x);
PyObject* to_python(boost::python::semantics, const T& x) const {
return boost::python::detail::import_extension_class<T>::get_converters()->to_python(boost::python::search_namespace, x);
}
friend T* from_python(PyObject* p, boost::python::type<T*> t, bool sig = false) {
@@ -101,8 +101,8 @@ class python_import_extension_class_converters
friend const std::auto_ptr<T>& from_python(PyObject* p, boost::python::type<const std::auto_ptr<T>&> t, bool sig = false) {
return boost::python::detail::import_extension_class<T>::get_converters()->from_python_caTr(p, t);
}
friend PyObject* to_python(std::auto_ptr<T> x, bool sig = false) {
return boost::python::detail::import_extension_class<T>::get_converters()->to_python(x);
friend PyObject* to_python(boost::python::semantics, std::auto_ptr<T> x, bool sig = false) {
return boost::python::detail::import_extension_class<T>::get_converters()->to_python(boost::python::search_namespace, x);
}
friend boost::shared_ptr<T>& from_python(PyObject* p, boost::python::type<boost::shared_ptr<T>&> t, bool sig = false) {
@@ -114,7 +114,7 @@ class python_import_extension_class_converters
friend const boost::shared_ptr<T>& from_python(PyObject* p, boost::python::type<const boost::shared_ptr<T>&> t, bool sig = false) {
return boost::python::detail::import_extension_class<T>::get_converters()->from_python_csTr(p, t);
}
friend PyObject* to_python(boost::shared_ptr<T> x, bool sig = false) {
friend PyObject* to_python(boost::python::semantics, boost::shared_ptr<T> x, bool sig = false) {
return boost::python::detail::import_extension_class<T>::get_converters()->to_python(x);
}
};
@@ -142,22 +142,22 @@ struct export_converter_object_base
virtual PyObject* to_python(const T& x) = 0;
virtual T* from_python_Ts(PyObject* p, boost::python::type<T*> t) = 0;
virtual const T* from_python_cTs(PyObject* p, boost::python::type<const T*> t) = 0;
virtual const T* from_python_cTscr(PyObject* p, boost::python::type<const T*const&> t) = 0;
virtual T* from_python_Tscr(PyObject* p, boost::python::type<T* const&> t) = 0;
virtual T& from_python_Tr(PyObject* p, boost::python::type<T&> t) = 0;
virtual const T& from_python_cTr(PyObject* p, boost::python::type<const T&> t) = 0;
virtual const T& from_python_T(PyObject* p, boost::python::type<T> t) = 0;
virtual T* from_python_Ts(PyObject* p, type<T*> t) = 0;
virtual const T* from_python_cTs(PyObject* p, type<const T*> t) = 0;
virtual const T* from_python_cTscr(PyObject* p, type<const T*const&> t) = 0;
virtual T* from_python_Tscr(PyObject* p, type<T* const&> t) = 0;
virtual T& from_python_Tr(PyObject* p, type<T&> t) = 0;
virtual const T& from_python_cTr(PyObject* p, type<const T&> t) = 0;
virtual const T& from_python_T(PyObject* p, type<T> t) = 0;
virtual std::auto_ptr<T>& from_python_aTr(PyObject* p, boost::python::type<std::auto_ptr<T>&> t) = 0;
virtual std::auto_ptr<T> from_python_aT(PyObject* p, boost::python::type<std::auto_ptr<T> > t) = 0;
virtual const std::auto_ptr<T>& from_python_caTr(PyObject* p, boost::python::type<const std::auto_ptr<T>&> t) = 0;
virtual std::auto_ptr<T>& from_python_aTr(PyObject* p, type<std::auto_ptr<T>&> t) = 0;
virtual std::auto_ptr<T> from_python_aT(PyObject* p, type<std::auto_ptr<T> > t) = 0;
virtual const std::auto_ptr<T>& from_python_caTr(PyObject* p, type<const std::auto_ptr<T>&> t) = 0;
virtual PyObject* to_python(std::auto_ptr<T> x) = 0;
virtual boost::shared_ptr<T>& from_python_sTr(PyObject* p, boost::python::type<boost::shared_ptr<T>&> t) = 0;
virtual const boost::shared_ptr<T>& from_python_sT(PyObject* p, boost::python::type<boost::shared_ptr<T> > t) = 0;
virtual const boost::shared_ptr<T>& from_python_csTr(PyObject* p, boost::python::type<const boost::shared_ptr<T>&> t) = 0;
virtual boost::shared_ptr<T>& from_python_sTr(PyObject* p, type<boost::shared_ptr<T>&> t) = 0;
virtual const boost::shared_ptr<T>& from_python_sT(PyObject* p, type<boost::shared_ptr<T> > t) = 0;
virtual const boost::shared_ptr<T>& from_python_csTr(PyObject* p, type<const boost::shared_ptr<T>&> t) = 0;
virtual PyObject* to_python(boost::shared_ptr<T> x) = 0;
};
@@ -171,52 +171,52 @@ struct export_converter_object_noncopyable : export_converter_object_base<T>
throw import_error();
}
virtual T* from_python_Ts(PyObject* p, boost::python::type<T*> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual T* from_python_Ts(PyObject* p, type<T*> t) {
return from_python(p, t);
}
virtual const T* from_python_cTs(PyObject* p, boost::python::type<const T*> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual const T* from_python_cTs(PyObject* p, type<const T*> t) {
return from_python(p, t);
}
virtual const T* from_python_cTscr(PyObject* p, boost::python::type<const T*const&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual const T* from_python_cTscr(PyObject* p, type<const T*const&> t) {
return from_python(p, t);
}
virtual T* from_python_Tscr(PyObject* p, boost::python::type<T* const&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual T* from_python_Tscr(PyObject* p, type<T* const&> t) {
return from_python(p, t);
}
virtual T& from_python_Tr(PyObject* p, boost::python::type<T&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual T& from_python_Tr(PyObject* p, type<T&> t) {
return from_python(p, t);
}
virtual const T& from_python_cTr(PyObject* p, boost::python::type<const T&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual const T& from_python_cTr(PyObject* p, type<const T&> t) {
return from_python(p, t);
}
virtual const T& from_python_T(PyObject* p, boost::python::type<T> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual const T& from_python_T(PyObject* p, type<T> t) {
return from_python(p, t);
}
virtual std::auto_ptr<T>& from_python_aTr(PyObject* p, boost::python::type<std::auto_ptr<T>&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual std::auto_ptr<T>& from_python_aTr(PyObject* p, type<std::auto_ptr<T>&> t) {
return from_python(p, t);
}
virtual std::auto_ptr<T> from_python_aT(PyObject* p, boost::python::type<std::auto_ptr<T> > t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual std::auto_ptr<T> from_python_aT(PyObject* p, type<std::auto_ptr<T> > t) {
return from_python(p, t);
}
virtual const std::auto_ptr<T>& from_python_caTr(PyObject* p, boost::python::type<const std::auto_ptr<T>&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual const std::auto_ptr<T>& from_python_caTr(PyObject* p, type<const std::auto_ptr<T>&> t) {
return from_python(p, t);
}
virtual PyObject* to_python(std::auto_ptr<T> x) {
return BOOST_PYTHON_CONVERSION::to_python(x);
return to_python(x);
}
virtual boost::shared_ptr<T>& from_python_sTr(PyObject* p, boost::python::type<boost::shared_ptr<T>&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual boost::shared_ptr<T>& from_python_sTr(PyObject* p, type<boost::shared_ptr<T>&> t) {
return from_python(p, t);
}
virtual const boost::shared_ptr<T>& from_python_sT(PyObject* p, boost::python::type<boost::shared_ptr<T> > t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual const boost::shared_ptr<T>& from_python_sT(PyObject* p, type<boost::shared_ptr<T> > t) {
return from_python(p, t);
}
virtual const boost::shared_ptr<T>& from_python_csTr(PyObject* p, boost::python::type<const boost::shared_ptr<T>&> t) {
return BOOST_PYTHON_CONVERSION::from_python(p, t);
virtual const boost::shared_ptr<T>& from_python_csTr(PyObject* p, type<const boost::shared_ptr<T>&> t) {
return from_python(p, t);
}
virtual PyObject* to_python(boost::shared_ptr<T> x) {
return BOOST_PYTHON_CONVERSION::to_python(x);
return to_python(x);
}
};
@@ -225,7 +225,7 @@ template <class T>
struct export_converter_object : export_converter_object_noncopyable<T>
{
virtual PyObject* to_python(const T& x) {
return BOOST_PYTHON_CONVERSION::py_extension_class_converters(boost::python::type<T>()).to_python(x);
return BOOST_PYTHON_CONVERSION::py_extension_class_converters(type<T>()).to_python(x);
}
};
@@ -247,29 +247,29 @@ class import_extension_class
m_py_class = py_class;
}
static boost::python::export_converter_object_base<T>* get_converters();
static export_converter_object_base<T>* get_converters();
private:
static std::string m_module;
static std::string m_py_class;
static boost::python::export_converter_object_base<T>* imported_converters;
static export_converter_object_base<T>* imported_converters;
};
template <class T> std::string import_extension_class<T>::m_module;
template <class T> std::string import_extension_class<T>::m_py_class;
template <class T>
boost::python::export_converter_object_base<T>*
export_converter_object_base<T>*
import_extension_class<T>::imported_converters = 0;
template <class T>
boost::python::export_converter_object_base<T>*
export_converter_object_base<T>*
import_extension_class<T>::get_converters() {
if (imported_converters == 0) {
void* cobject
= import_converter_object(m_module, m_py_class,
converters_attribute_name);
imported_converters
= static_cast<boost::python::export_converter_object_base<T>*>(cobject);
= static_cast<export_converter_object_base<T>*>(cobject);
check_export_converters_api(
export_converters_api_major,
export_converters_api_minor,

View File

@@ -179,31 +179,32 @@ template <class T, class U = boost::python::detail::held_instance<T> >
class python_extension_class_converters
{
public:
// Get an object which can be used to convert T to/from python. This is used
// as a kind of concept check by the global template
//
// PyObject* to_python(const T& x)
//
// below this class, to prevent the confusing messages that would otherwise
// pop up. Now, if T hasn't been wrapped as an extension class, the user
// will see an error message about the lack of an eligible
// py_extension_class_converters() function.
friend python_extension_class_converters py_extension_class_converters(boost::python::type<T>)
/// Get an object which can be used to convert T to/from python. This is used
/// as a kind of concept check by the free template function
///
/// PyObject* to_python(boost::python::semantics, const T& x)
///
/// below this class, to prevent the confusing messages that would otherwise
/// pop up. Now, if T hasn't been wrapped as an extension class, the user
/// will see an error message about the lack of an eligible
/// py_extension_class_converters() function.
friend python_extension_class_converters py_extension_class_converters(
boost::python::type<T>)
{
return python_extension_class_converters();
}
// This is a member function because in a conforming implementation, friend
// funcitons defined inline in the class body are all instantiated as soon
// as the enclosing class is instantiated. If T is not copyable, that causes
// a compiler error. Instead, we access this function through the global
// template
//
// PyObject* to_python(const T& x)
//
// defined below this class. Since template functions are instantiated only
// on demand, errors will be avoided unless T is noncopyable and the user
// writes code which causes us to try to copy a T.
/// This is a member function because in a conforming implementation, friend
/// funcitons defined inline in the class body are all instantiated as soon
/// as the enclosing class is instantiated. If T is not copyable, that causes
/// a compiler error. Instead, we access this function through the global
/// template
///
/// PyObject* to_python(boost::python::semantics, const T& x)
///
/// defined below this class. Since template functions are instantiated only
/// on demand, errors will be avoided unless T is noncopyable and the user
/// writes code which causes us to try to copy a T.
PyObject* to_python(const T& x) const
{
boost::python::reference<boost::python::detail::extension_instance> result(create_instance());
@@ -213,6 +214,7 @@ class python_extension_class_converters
return result.release();
}
/// Extract a pointer to T from the given PyObject. Will throw argument_error if obj == None.
friend
T* non_null_from_python(PyObject* obj, boost::python::type<T*>)
{
@@ -235,16 +237,19 @@ class python_extension_class_converters
throw boost::python::argument_error();
}
// Convert to T*
/// Convert obj to T*. If obj == None, returns 0.
friend T* from_python(PyObject* obj, boost::python::type<T*>)
{
// forward declaration needed for ordinary lookup.
T* non_null_from_python(PyObject*, boost::python::type<T*>);
if (obj == Py_None)
return 0;
else
return non_null_from_python(obj, boost::python::type<T*>());
}
// Extract from obj a mutable reference to the PtrType object which is holding a T.
/// Extract from obj a mutable reference to the PtrType object which is holding a T.
template <class PtrType>
static PtrType& smart_ptr_reference(PyObject* obj, boost::python::type<PtrType>)
{
@@ -263,10 +268,10 @@ class python_extension_class_converters
throw boost::python::argument_error();
}
// Extract from obj a reference to the PtrType object which is holding a
// T. If it weren't for auto_ptr, it would be a constant reference. Do not
// modify the referent except by copying an auto_ptr! If obj is None, the
// reference denotes a default-constructed PtrType
/// Extract from obj a reference to the PtrType object which is holding a
/// T. If it weren't for auto_ptr, it would be a constant reference. Do not
/// modify the referent except by copying an auto_ptr! If obj is None, the
/// reference denotes a default-constructed PtrType
template <class PtrType>
static PtrType& smart_ptr_value(PyObject* obj, boost::python::type<PtrType>)
{
@@ -277,7 +282,9 @@ class python_extension_class_converters
}
return smart_ptr_reference(obj, boost::python::type<PtrType>());
}
/// Wrap x in a Python object which implements the functionality of a
/// regular wrapped T by dereferencing a copy of x.
template <class PtrType>
static PyObject* smart_ptr_to_python(PtrType x)
{
@@ -293,6 +300,9 @@ class python_extension_class_converters
return result.release();
}
/// Create a Python object which is an instance of the Python type wrapper
/// for T. The result does not actually contain the neccessary instance of
/// T. This function is an implementation detail.
static boost::python::reference<boost::python::detail::extension_instance> create_instance()
{
PyTypeObject* class_object = boost::python::detail::class_registry<T>::class_object();
@@ -303,15 +313,15 @@ class python_extension_class_converters
new boost::python::detail::extension_instance(class_object));
}
// Convert to const T*
/// Convert p to const T*
friend const T* from_python(PyObject* p, boost::python::type<const T*>)
{ return from_python(p, boost::python::type<T*>()); }
// Convert to const T* const&
/// Convert p to const T* const&
friend const T* from_python(PyObject* p, boost::python::type<const T*const&>)
{ return from_python(p, boost::python::type<const T*>()); }
// Convert to T* const&
/// Convert p to T* const&
friend T* from_python(PyObject* p, boost::python::type<T* const&>)
{ return from_python(p, boost::python::type<T*>()); }
@@ -319,11 +329,11 @@ class python_extension_class_converters
friend T& from_python(PyObject* p, boost::python::type<T&>)
{ return *boost::python::detail::check_non_null(non_null_from_python(p, boost::python::type<T*>())); }
// Convert to const T&
// Convert p to const T&
friend const T& from_python(PyObject* p, boost::python::type<const T&>)
{ return from_python(p, boost::python::type<T&>()); }
// Convert to T
/// Convert p to T
friend const T& from_python(PyObject* p, boost::python::type<T>)
{ return from_python(p, boost::python::type<T&>()); }
@@ -336,7 +346,7 @@ class python_extension_class_converters
friend const std::auto_ptr<T>& from_python(PyObject* p, boost::python::type<const std::auto_ptr<T>&>)
{ return smart_ptr_value(p, boost::python::type<std::auto_ptr<T> >()); }
friend PyObject* to_python(std::auto_ptr<T> x)
friend PyObject* to_python(boost::python::semantics, std::auto_ptr<T> x)
{ return smart_ptr_to_python(x); }
friend boost::shared_ptr<T>& from_python(PyObject* p, boost::python::type<boost::shared_ptr<T>&>)
@@ -348,7 +358,7 @@ class python_extension_class_converters
friend const boost::shared_ptr<T>& from_python(PyObject* p, boost::python::type<const boost::shared_ptr<T>&>)
{ return smart_ptr_value(p, boost::python::type<boost::shared_ptr<T> >()); }
friend PyObject* to_python(boost::shared_ptr<T> x)
friend PyObject* to_python(boost::python::semantics, boost::shared_ptr<T> x)
{ return smart_ptr_to_python(x); }
};
@@ -357,7 +367,7 @@ class python_extension_class_converters
// T is a wrapped class. See the first 2 functions declared in
// python_extension_class_converters above for more info.
template <class T>
PyObject* to_python(const T& x)
PyObject* to_python(boost::python::semantics, const T& x)
{
return py_extension_class_converters(boost::python::type<T>()).to_python(x);
}

View File

@@ -94,9 +94,9 @@ struct raw_arguments_function : function
ref(keywords, ref::increment_count) :
ref(PyDict_New()));
return to_python(
(*m_pf)(from_python(args, boost::python::type<Args>()),
from_python(dict.get(), boost::python::type<Keywords>())));
return to_python(search_namespace,
(*m_pf)(from_python(args, type<Args>()),
from_python(dict.get(), type<Keywords>())));
}
const char* description() const
@@ -263,7 +263,7 @@ PyObject* getter_function<ClassType, MemberType>::do_call(
if (!PyArg_ParseTuple(args, const_cast<char*>("O"), &self))
return 0;
return to_python(
return to_python(search_namespace,
from_python(self, type<const ClassType*>())->*m_pm);
}

View File

@@ -297,7 +297,7 @@ struct list::slice_proxy
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
PyObject* to_python(const boost::python::tuple&);
PyObject* to_python(boost::python::search_namespace, const boost::python::tuple&);
boost::python::tuple from_python(PyObject* p, boost::python::type<boost::python::tuple>);
inline boost::python::tuple from_python(PyObject* p, boost::python::type<const boost::python::tuple&>)
@@ -305,7 +305,7 @@ inline boost::python::tuple from_python(PyObject* p, boost::python::type<const b
return from_python(p, boost::python::type<boost::python::tuple>());
}
PyObject* to_python(const boost::python::list&);
PyObject* to_python(boost::python::search_namespace, const boost::python::list&);
boost::python::list from_python(PyObject* p, boost::python::type<boost::python::list>);
inline boost::python::list from_python(PyObject* p, boost::python::type<const boost::python::list&>)
@@ -313,7 +313,7 @@ inline boost::python::list from_python(PyObject* p, boost::python::type<const bo
return from_python(p, boost::python::type<boost::python::list>());
}
PyObject* to_python(const boost::python::string&);
PyObject* to_python(boost::python::search_namespace, const boost::python::string&);
boost::python::string from_python(PyObject* p, boost::python::type<boost::python::string>);
inline boost::python::string from_python(PyObject* p, boost::python::type<const boost::python::string&>)
@@ -321,7 +321,7 @@ inline boost::python::string from_python(PyObject* p, boost::python::type<const
return from_python(p, boost::python::type<boost::python::string>());
}
PyObject* to_python(const boost::python::dictionary&);
PyObject* to_python(boost::python::search_namespace, const boost::python::dictionary&);
boost::python::dictionary from_python(PyObject* p, boost::python::type<boost::python::dictionary>);
inline boost::python::dictionary from_python(PyObject* p, boost::python::type<const boost::python::dictionary&>)

View File

@@ -237,11 +237,11 @@ namespace detail
{ \
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const \
{ \
tuple args(ref(arguments, ref::increment_count)); \
tuple args(ref(arguments, ref::increment_count)); \
\
return BOOST_PYTHON_CONVERSION::to_python( \
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) oper \
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())); \
return to_python(search_namespace \
from_python(args[0].get(), type<Left>()) oper \
from_python(args[1].get(), type<Right>())); \
} \
\
const char* description() const \
@@ -253,11 +253,11 @@ namespace detail
{ \
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const \
{ \
tuple args(ref(arguments, ref::increment_count)); \
tuple args(ref(arguments, ref::increment_count)); \
\
return BOOST_PYTHON_CONVERSION::to_python( \
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) oper \
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())); \
return to_python(search_namespace \
from_python(args[1].get(), type<Left>()) oper \
from_python(args[0].get(), type<Right>())); \
} \
\
const char* description() const \
@@ -278,10 +278,10 @@ namespace detail
{ \
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const \
{ \
tuple args(ref(arguments, ref::increment_count)); \
tuple args(ref(arguments, ref::increment_count)); \
\
return BOOST_PYTHON_CONVERSION::to_python( \
oper(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<operand>()))); \
return to_python(search_namespace, \
oper(from_python(args[0].get(), type<operand>()))); \
} \
\
const char* description() const \
@@ -335,9 +335,9 @@ namespace detail
throw argument_error();
}
return BOOST_PYTHON_CONVERSION::to_python(
pow(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()),
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())));
return to_python(search_namespace,
pow(from_python(args[0].get(), type<Left>()),
from_python(args[1].get(), type<Right>())));
}
const char* description() const
@@ -358,9 +358,9 @@ namespace detail
throw argument_error();
}
return BOOST_PYTHON_CONVERSION::to_python(
pow(BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()),
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())));
return to_python(search_namespace,
pow(from_python(args[1].get(), type<Left>()),
from_python(args[0].get(), type<Right>())));
}
const char* description() const
@@ -386,13 +386,13 @@ namespace detail
PyObject * res = PyTuple_New(2);
PyTuple_SET_ITEM(res, 0,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) /
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())));
to_python(search_namespace,
from_python(args[0].get(), type<Left>()) /
from_python(args[1].get(), type<Right>())));
PyTuple_SET_ITEM(res, 1,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) %
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())));
to_python(search_namespace,
from_python(args[0].get(), type<Left>()) %
from_python(args[1].get(), type<Right>())));
return res;
}
@@ -411,13 +411,13 @@ namespace detail
PyObject * res = PyTuple_New(2);
PyTuple_SET_ITEM(res, 0,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) /
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())));
to_python(search_namespace,
from_python(args[1].get(), type<Left>()) /
from_python(args[0].get(), type<Right>())));
PyTuple_SET_ITEM(res, 1,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) %
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())));
to_python(search_namespace,
from_python(args[1].get(), type<Left>()) %
from_python(args[0].get(), type<Right>())));
return res;
}
@@ -443,12 +443,12 @@ namespace detail
{
tuple args(ref(arguments, ref::increment_count));
return BOOST_PYTHON_CONVERSION::to_python(
(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) <
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())) ?
return to_python(search_namespace,
(from_python(args[0].get(), type<Left>()) <
from_python(args[1].get(), type<Right>())) ?
- 1 :
(BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>()) <
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>())) ?
(from_python(args[1].get(), type<Right>()) <
from_python(args[0].get(), type<Left>())) ?
1 :
0) ;
}
@@ -465,12 +465,12 @@ namespace detail
{
tuple args(ref(arguments, ref::increment_count));
return BOOST_PYTHON_CONVERSION::to_python(
(BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) <
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())) ?
return to_python(search_namespace,
(from_python(args[1].get(), type<Left>()) <
from_python(args[0].get(), type<Right>())) ?
- 1 :
(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>()) <
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>())) ?
(from_python(args[0].get(), type<Right>()) <
from_python(args[1].get(), type<Left>())) ?
1 :
0) ;
}
@@ -510,13 +510,13 @@ namespace detail
// _STL::string, but std::string.
# ifdef BOOST_PYTHON_USE_SSTREAM
std::ostringstream s;
s << BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<operand>());
return BOOST_PYTHON_CONVERSION::to_python(s.str());
s << from_python(args[0].get(), type<operand>());
return to_python(search_namespace, s.str());
# else
std::ostrstream s;
s << BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<operand>()) << char();
s << from_python(args[0].get(), type<operand>()) << char();
auto unfreezer unfreeze(s);
return BOOST_PYTHON_CONVERSION::to_python(const_cast<char const *>(s.str()));
return to_python(search_namespace, const_cast<char const *>(s.str()));
# endif
}

View File

@@ -29,7 +29,7 @@ struct py_ptr_conversions : Base
inline friend T from_python(PyObject* x, boost::python::type<T>)
{ return T(boost::python::downcast<Value>(x).get(), T::increment_count); }
inline friend PyObject* to_python(T x)
inline friend PyObject* to_python(boost::python::semantics, T x)
{ return boost::python::as_object(x.release()); }
};
@@ -165,7 +165,7 @@ typedef reference<PyObject> ref;
template <class T>
ref make_ref(const T& x)
{
return ref(to_python(x));
return ref(to_python(search_namespace, x));
}
}} // namespace boost::python

View File

@@ -22,6 +22,8 @@
namespace boost { namespace python {
enum semantics { search_namespace }; // Used to find to_python functions via koenig lookup.
// IMPORTANT: this function may only be called from within a catch block!
void handle_exception()
{
@@ -142,7 +144,7 @@ PyObject* integer_to_python(T value)
throw boost::python::error_already_set();
}
return to_python(value_as_long);
return to_python(boost::python::search_namespace, value_as_long);
}
int from_python(PyObject* p, boost::python::type<int> type)
@@ -150,7 +152,7 @@ int from_python(PyObject* p, boost::python::type<int> type)
return integer_from_python(p, type);
}
PyObject* to_python(unsigned int i)
PyObject* to_python(boost::python::semantics, unsigned int i)
{
return integer_to_python(i);
}
@@ -170,7 +172,7 @@ float from_python(PyObject* p, boost::python::type<float>)
return static_cast<float>(from_python(p, boost::python::type<double>()));
}
PyObject* to_python(unsigned short i)
PyObject* to_python(boost::python::semantics, unsigned short i)
{
return integer_to_python(i);
}
@@ -180,7 +182,7 @@ unsigned short from_python(PyObject* p, boost::python::type<unsigned short> type
return integer_from_python(p, type);
}
PyObject* to_python(char c)
PyObject* to_python(boost::python::semantics, char c)
{
if (c == '\0') return PyString_FromString("");
return PyString_FromStringAndSize(&c, 1);
@@ -198,7 +200,7 @@ char from_python(PyObject* p, boost::python::type<char>)
return PyString_AsString(p)[0];
}
PyObject* to_python(unsigned char i)
PyObject* to_python(boost::python::semantics, unsigned char i)
{
return integer_to_python(i);
}
@@ -208,7 +210,7 @@ unsigned char from_python(PyObject* p, boost::python::type<unsigned char> type)
return integer_from_python(p, type);
}
PyObject* to_python(signed char i)
PyObject* to_python(boost::python::semantics, signed char i)
{
return integer_to_python(i);
}
@@ -218,7 +220,7 @@ signed char from_python(PyObject* p, boost::python::type<signed char> type)
return integer_from_python(p, type);
}
PyObject* to_python(unsigned long x)
PyObject* to_python(boost::python::semantics, unsigned long x)
{
return integer_to_python(x);
}
@@ -244,7 +246,7 @@ const char* from_python(PyObject* p, boost::python::type<const char*>)
return s;
}
PyObject* to_python(const std::string& s)
PyObject* to_python(boost::python::semantics, const std::string& s)
{
return PyString_FromStringAndSize(s.data(), s.size());
}
@@ -268,12 +270,12 @@ bool from_python(PyObject* p, boost::python::type<bool>)
#ifdef BOOST_MSVC6_OR_EARLIER
// An optimizer bug prevents these from being inlined.
PyObject* to_python(double d)
PyObject* to_python(boost::python::semantics, double d)
{
return PyFloat_FromDouble(d);
}
PyObject* to_python(float f)
PyObject* to_python(boost::python::semantics, float f)
{
return PyFloat_FromDouble(f);
}

View File

@@ -42,7 +42,7 @@ namespace detail {
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
inline PyObject* to_python(boost::python::detail::operator_dispatcher* n) { return n; }
inline PyObject* to_python(boost::python::semantics, boost::python::detail::operator_dispatcher* n) { return n; }
BOOST_PYTHON_END_CONVERSION_NAMESPACE

View File

@@ -38,7 +38,7 @@ struct callback
%{ template <%(class A%n%:, %)>
%} static R call_method(PyObject* self, const char* name%(, const A%n& a%n%))
{%(
ref p%n(to_python(a%n));%)
ref p%n(to_python(search_namespace, a%n));%)
ref result(PyEval_CallMethod(self, const_cast<char*>(name),
const_cast<char*>("(%(O%))")%(,
p%n.get()%)));
@@ -49,7 +49,7 @@ struct callback
%{ template <%(class A%n%:, %)>
%} static R call(PyObject* self%(, const A%n& a%n%))
{%(
ref p%n(to_python(a%n));%)
ref p%n(to_python(search_namespace, a%n));%)
ref result(PyEval_CallFunction(self, const_cast<char*>("(%(O%))")%(,
p%n.get()%)));
detail::callback_adjust_refcount(result.get(), type<R>());
@@ -70,7 +70,7 @@ struct callback<void>
%{ template <%(class A%n%:, %)>
%} static void call_method(PyObject* self, const char* name%(, const A%n& a%n%))
{%(
ref p%n(to_python(a%n));%)
ref p%n(to_python(search_namespace, a%n));%)
ref result(PyEval_CallMethod(self, const_cast<char*>(name),
const_cast<char*>("(%(O%))")%(,
p%n.get()%)));
@@ -79,7 +79,7 @@ struct callback<void>
%{ template <%(class A%n%:, %)>
%} static void call(PyObject* self%(, const A%n& a%n%))
{%(
ref p%n(to_python(a%n));%)
ref p%n(to_python(search_namespace, a%n));%)
ref result(PyEval_CallFunction(self, const_cast<char*>("(%(O%))")%(,
p%n.get()%)));
}

View File

@@ -97,14 +97,14 @@ def gen_caller(member_function_args, free_function_args = None):
return (header % (member_function_args, free_function_args)
+ body_sections[0]
+ gen_functions(member_function, member_function_args,
'R', '', 'return to_python(', ');')
'R', '', 'return to_python(search_namespace, ', ');')
+ body_sections[1]
+ gen_functions(member_function, member_function_args,
'R', ' const', 'return to_python(', ');')
'R', ' const', 'return to_python(search_namespace, ', ');')
+ body_sections[2]
+ gen_functions(free_function, free_function_args,
'R', 'return to_python(', ');')
'R', 'return to_python(search_namespace, ', ');')
+ body_sections[3]
# specialized part for void return values begins here

View File

@@ -3,7 +3,7 @@ import string
def gen_extclass(args):
return (
"""// (C) Copyright David Abrahams 2000. Permission to copy, use, modify, sell and
"""// (C) Copyright David Abrahams 2000-2001. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
@@ -171,35 +171,44 @@ BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
// and U. T is the class the user really intends to wrap. U is a class derived
// from T with some virtual function overriding boilerplate, or if there are no
// virtual functions, U = held_instance<T>.
//
// A look-alike of this class in root/boost/python/cross_module.hpp
// is used for the implementation of the cross-module support
// (export_converters and import_converters). If from_python
// and to_python converters are added or removed from the class
// below, the class python_import_extension_class_converters has
// to be modified accordingly.
//
template <class T, class U = boost::python::detail::held_instance<T> >
class python_extension_class_converters
{
public:
// Get an object which can be used to convert T to/from python. This is used
// as a kind of concept check by the global template
//
// PyObject* to_python(const T& x)
//
// below this class, to prevent the confusing messages that would otherwise
// pop up. Now, if T hasn't been wrapped as an extension class, the user
// will see an error message about the lack of an eligible
// py_extension_class_converters() function.
friend python_extension_class_converters py_extension_class_converters(boost::python::type<T>)
/// Get an object which can be used to convert T to/from python. This is used
/// as a kind of concept check by the free template function
///
/// PyObject* to_python(boost::python::semantics, const T& x)
///
/// below this class, to prevent the confusing messages that would otherwise
/// pop up. Now, if T hasn't been wrapped as an extension class, the user
/// will see an error message about the lack of an eligible
/// py_extension_class_converters() function.
friend python_extension_class_converters py_extension_class_converters(
boost::python::type<T>)
{
return python_extension_class_converters();
}
// This is a member function because in a conforming implementation, friend
// funcitons defined inline in the class body are all instantiated as soon
// as the enclosing class is instantiated. If T is not copyable, that causes
// a compiler error. Instead, we access this function through the global
// template
//
// PyObject* to_python(const T& x)
//
// defined below this class. Since template functions are instantiated only
// on demand, errors will be avoided unless T is noncopyable and the user
// writes code which causes us to try to copy a T.
/// This is a member function because in a conforming implementation, friend
/// funcitons defined inline in the class body are all instantiated as soon
/// as the enclosing class is instantiated. If T is not copyable, that causes
/// a compiler error. Instead, we access this function through the global
/// template
///
/// PyObject* to_python(boost::python::semantics, const T& x)
///
/// defined below this class. Since template functions are instantiated only
/// on demand, errors will be avoided unless T is noncopyable and the user
/// writes code which causes us to try to copy a T.
PyObject* to_python(const T& x) const
{
boost::python::reference<boost::python::detail::extension_instance> result(create_instance());
@@ -209,6 +218,7 @@ class python_extension_class_converters
return result.release();
}
/// Extract a pointer to T from the given PyObject. Will throw argument_error if obj == None.
friend
T* non_null_from_python(PyObject* obj, boost::python::type<T*>)
{
@@ -231,16 +241,19 @@ class python_extension_class_converters
throw boost::python::argument_error();
}
// Convert to T*
/// Convert obj to T*. If obj == None, returns 0.
friend T* from_python(PyObject* obj, boost::python::type<T*>)
{
// forward declaration needed for ordinary lookup.
T* non_null_from_python(PyObject*, boost::python::type<T*>);
if (obj == Py_None)
return 0;
else
return non_null_from_python(obj, boost::python::type<T*>());
}
// Extract from obj a mutable reference to the PtrType object which is holding a T.
/// Extract from obj a mutable reference to the PtrType object which is holding a T.
template <class PtrType>
static PtrType& smart_ptr_reference(PyObject* obj, boost::python::type<PtrType>)
{
@@ -259,10 +272,10 @@ class python_extension_class_converters
throw boost::python::argument_error();
}
// Extract from obj a reference to the PtrType object which is holding a
// T. If it weren't for auto_ptr, it would be a constant reference. Do not
// modify the referent except by copying an auto_ptr! If obj is None, the
// reference denotes a default-constructed PtrType
/// Extract from obj a reference to the PtrType object which is holding a
/// T. If it weren't for auto_ptr, it would be a constant reference. Do not
/// modify the referent except by copying an auto_ptr! If obj is None, the
/// reference denotes a default-constructed PtrType
template <class PtrType>
static PtrType& smart_ptr_value(PyObject* obj, boost::python::type<PtrType>)
{
@@ -273,7 +286,9 @@ class python_extension_class_converters
}
return smart_ptr_reference(obj, boost::python::type<PtrType>());
}
/// Wrap x in a Python object which implements the functionality of a
/// regular wrapped T by dereferencing a copy of x.
template <class PtrType>
static PyObject* smart_ptr_to_python(PtrType x)
{
@@ -289,6 +304,9 @@ class python_extension_class_converters
return result.release();
}
/// Create a Python object which is an instance of the Python type wrapper
/// for T. The result does not actually contain the neccessary instance of
/// T. This function is an implementation detail.
static boost::python::reference<boost::python::detail::extension_instance> create_instance()
{
PyTypeObject* class_object = boost::python::detail::class_registry<T>::class_object();
@@ -299,15 +317,15 @@ class python_extension_class_converters
new boost::python::detail::extension_instance(class_object));
}
// Convert to const T*
/// Convert p to const T*
friend const T* from_python(PyObject* p, boost::python::type<const T*>)
{ return from_python(p, boost::python::type<T*>()); }
// Convert to const T* const&
/// Convert p to const T* const&
friend const T* from_python(PyObject* p, boost::python::type<const T*const&>)
{ return from_python(p, boost::python::type<const T*>()); }
// Convert to T* const&
/// Convert p to T* const&
friend T* from_python(PyObject* p, boost::python::type<T* const&>)
{ return from_python(p, boost::python::type<T*>()); }
@@ -315,11 +333,11 @@ class python_extension_class_converters
friend T& from_python(PyObject* p, boost::python::type<T&>)
{ return *boost::python::detail::check_non_null(non_null_from_python(p, boost::python::type<T*>())); }
// Convert to const T&
// Convert p to const T&
friend const T& from_python(PyObject* p, boost::python::type<const T&>)
{ return from_python(p, boost::python::type<T&>()); }
// Convert to T
/// Convert p to T
friend const T& from_python(PyObject* p, boost::python::type<T>)
{ return from_python(p, boost::python::type<T&>()); }
@@ -332,7 +350,7 @@ class python_extension_class_converters
friend const std::auto_ptr<T>& from_python(PyObject* p, boost::python::type<const std::auto_ptr<T>&>)
{ return smart_ptr_value(p, boost::python::type<std::auto_ptr<T> >()); }
friend PyObject* to_python(std::auto_ptr<T> x)
friend PyObject* to_python(boost::python::semantics, std::auto_ptr<T> x)
{ return smart_ptr_to_python(x); }
friend boost::shared_ptr<T>& from_python(PyObject* p, boost::python::type<boost::shared_ptr<T>&>)
@@ -344,7 +362,7 @@ class python_extension_class_converters
friend const boost::shared_ptr<T>& from_python(PyObject* p, boost::python::type<const boost::shared_ptr<T>&>)
{ return smart_ptr_value(p, boost::python::type<boost::shared_ptr<T> >()); }
friend PyObject* to_python(boost::shared_ptr<T> x)
friend PyObject* to_python(boost::python::semantics, boost::shared_ptr<T> x)
{ return smart_ptr_to_python(x); }
};
@@ -353,7 +371,7 @@ class python_extension_class_converters
// T is a wrapped class. See the first 2 functions declared in
// python_extension_class_converters above for more info.
template <class T>
PyObject* to_python(const T& x)
PyObject* to_python(boost::python::semantics, const T& x)
{
return py_extension_class_converters(boost::python::type<T>()).to_python(x);
}

View File

@@ -49,9 +49,9 @@ PyObject* object::get() const
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
PyObject* to_python(const boost::python::tuple& x)
PyObject* to_python(boost::python::semantics, const boost::python::tuple& x)
{
return object_to_python(x);
return boost::python::object_to_python(x);
}
boost::python::tuple from_python(PyObject* p, boost::python::type<boost::python::tuple> type)
@@ -59,7 +59,7 @@ boost::python::tuple from_python(PyObject* p, boost::python::type<boost::python:
return boost::python::object_from_python(p, type);
}
PyObject* to_python(const boost::python::list& x)
PyObject* to_python(boost::python::semantics, const boost::python::list& x)
{
return object_to_python(x);
}
@@ -69,7 +69,7 @@ boost::python::list from_python(PyObject* p, boost::python::type<boost::python::
return boost::python::object_from_python(p, type);
}
PyObject* to_python(const boost::python::dictionary& x)
PyObject* to_python(boost::python::semantics, const boost::python::dictionary& x)
{
return object_to_python(x);
}
@@ -79,7 +79,7 @@ boost::python::dictionary from_python(PyObject* p, boost::python::type<boost::py
return boost::python::object_from_python(p, type);
}
PyObject* to_python(const boost::python::string& x)
PyObject* to_python(boost::python::semantics, const boost::python::string& x)
{
return object_to_python(x);
}

View File

@@ -165,7 +165,7 @@ void throw_key_error_if_end(const StringMap& m, StringMap::const_iterator p, std
{
if (p == m.end())
{
PyErr_SetObject(PyExc_KeyError, BOOST_PYTHON_CONVERSION::to_python(key));
PyErr_SetObject(PyExc_KeyError, BOOST_PYTHON_CONVERSION::to_python(boost::python::search_namespace, key));
throw boost::python::error_already_set();
}
}
@@ -746,9 +746,9 @@ namespace boost { namespace python {
}} // namespace boost::python
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
inline PyObject* to_python(const bpl_test::Record* p)
inline PyObject* to_python(boost::python::semantics, const bpl_test::Record* p)
{
return to_python(*p);
return to_python(boost::python::search_namespace, *p);
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
@@ -835,7 +835,7 @@ namespace bpl_test {
throw boost::python::error_already_set();
}
const int number = BOOST_PYTHON_CONVERSION::from_python(state[0].get(), boost::python::type<int>());
const int number = from_python(state[0].get(), boost::python::type<int>());
if (number != 42)
w.set_secret_number(number);
}
@@ -1117,13 +1117,13 @@ PyObject* raw(const boost::python::tuple& args, const boost::python::dictionary&
throw boost::python::argument_error();
}
RawTest* first = BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<RawTest*>());
int second = BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<int>());
RawTest* first = from_python(args[0].get(), boost::python::type<RawTest*>());
int second = from_python(args[1].get(), boost::python::type<int>());
int third = BOOST_PYTHON_CONVERSION::from_python(keywords[boost::python::string("third")].get(), boost::python::type<int>());
int fourth = BOOST_PYTHON_CONVERSION::from_python(keywords[boost::python::string("fourth")].get(), boost::python::type<int>());
int third = from_python(keywords[boost::python::string("third")].get(), boost::python::type<int>());
int fourth = from_python(keywords[boost::python::string("fourth")].get(), boost::python::type<int>());
return BOOST_PYTHON_CONVERSION::to_python(first->i_ + second + third + fourth);
return to_python(boost::python::search_namespace, first->i_ + second + third + fourth);
}
void init_module()