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

Fix refcounting bugs in class object; add regression

Removed flotsam


[SVN r14287]
This commit is contained in:
Dave Abrahams
2002-07-02 23:34:21 +00:00
parent 279ad90a3c
commit 2ae7c60780
4 changed files with 70 additions and 597 deletions

View File

@@ -1,277 +0,0 @@
// Copyright David Abrahams 2002. 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.
#ifndef CALLBACK_DWA2002228_HPP
# define CALLBACK_DWA2002228_HPP
# include <boost/python/converter/to_python_function.hpp>
# include <boost/python/converter/pointee_to_python_function.hpp>
# include <boost/python/converter/from_python.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/converter/callback_to_python_base.hpp>
# include <boost/python/converter/callback_from_python_base.hpp>
# include <boost/python/converter/builtin_converters.hpp>
# include <boost/python/to_python_indirect.hpp>
# include <boost/python/detail/none.hpp>
# include <boost/python/ptr.hpp>
# include <boost/python/errors.hpp>
namespace boost { namespace python { namespace converter {
namespace detail
{
template <class T>
struct pointer_callback_from_python
{
pointer_callback_from_python();
T operator()(PyObject*) const;
};
template <class T>
struct reference_callback_from_python
{
reference_callback_from_python();
T operator()(PyObject*) const;
};
template <class T>
struct rvalue_callback_from_python
{
rvalue_callback_from_python();
T const& operator()(PyObject*);
private:
rvalue_data<T> m_data;
};
template <class T>
struct select_callback_from_python
{
BOOST_STATIC_CONSTANT(
bool, ptr = is_pointer<T>::value);
BOOST_STATIC_CONSTANT(
bool, ref = is_reference<T>::value);
typedef typename mpl::select_type<
ptr
, pointer_callback_from_python<T>
, typename mpl::select_type<
ref
, reference_callback_from_python<T>
, rvalue_callback_from_python<T>
>::type
>::type type;
};
template <class T>
struct reference_callback_to_python : callback_to_python_holder
{
reference_callback_to_python(T& x);
private:
static PyObject* get_object(T& x);
};
template <class T>
struct value_callback_to_python : callback_to_python_base
{
// Throw an exception if the conversion can't succeed
value_callback_to_python(T const&);
};
template <class Ptr>
struct pointer_deep_callback_to_python : callback_to_python_base
{
// Throw an exception if the conversion can't succeed
pointer_deep_callback_to_python(Ptr);
};
template <class Ptr>
struct pointer_shallow_callback_to_python : callback_to_python_holder
{
// Throw an exception if the conversion can't succeed
pointer_shallow_callback_to_python(Ptr);
private:
static PyObject* get_object(Ptr p);
};
template <class T>
struct select_callback_to_python
{
BOOST_STATIC_CONSTANT(
bool, ptr = is_pointer<T>::value);
BOOST_STATIC_CONSTANT(
bool, ref_wrapper = is_reference_wrapper<T>::value);
BOOST_STATIC_CONSTANT(
bool, ptr_wrapper = is_pointer_wrapper<T>::value);
typedef typename unwrap_reference<T>::type unwrapped_referent;
typedef typename unwrap_pointer<T>::type unwrapped_ptr;
typedef typename mpl::select_type<
ptr
, pointer_deep_callback_to_python<T>
, typename mpl::select_type<
ptr_wrapper
, pointer_shallow_callback_to_python<unwrapped_ptr>
, typename mpl::select_type<
ref_wrapper
, reference_callback_to_python<unwrapped_referent>
, value_callback_to_python<T>
>::type
>::type
>::type type;
};
}
template <class T>
struct callback_from_python
: detail::select_callback_from_python<T>::type
{
typedef T result_type;
};
struct void_result
{
private:
void_result() {}
void operator=(void_result const&);
// I would prefer to make this completely untouchable, but few
// compilers support template friends
# if 0
void_result(void_result const&);
# endif
friend struct callback_from_python<void>;
};
// Specialization as a convenience for call and call_method
template <>
struct callback_from_python<void>
{
typedef void_result result_type;
result_type operator()(PyObject* x) const
{
Py_DECREF(expect_non_null(x));
return result_type();
}
};
template <class T>
struct callback_to_python
: detail::select_callback_to_python<T>::type
{
typedef typename detail::select_callback_to_python<T>::type base;
public: // member functions
// Throw an exception if the conversion can't succeed
callback_to_python(T const& x);
};
// Convenience macros for call<> and call_method<> code generation
# define BOOST_PYTHON_CALLBACK_TO_PYTHON_GET(index,ignored) \
converter::callback_to_python<BOOST_PP_CAT(A,index)>( \
BOOST_PP_CAT(a,index)).get()
# define BOOST_PYTHON_ARG_STRING(nargs) \
"(" BOOST_PP_REPEAT(nargs,BOOST_PYTHON_PROJECT_2ND,"O") ")"
//
// Implementations
//
namespace detail
{
template <class T>
inline rvalue_callback_from_python<T>::rvalue_callback_from_python()
: m_data(rvalue_from_python_chain<T>::value)
{
throw_if_not_registered(m_data.stage1);
}
template <class T>
inline T const& rvalue_callback_from_python<T>::operator()(PyObject* obj)
{
return *(T*)convert_rvalue(obj, m_data.stage1, m_data.storage.bytes);
}
BOOST_PYTHON_DECL void throw_no_class_registered();
template <class T>
inline reference_callback_from_python<T>::reference_callback_from_python()
{
detail::throw_if_not_registered(lvalue_from_python_chain<T,true>::value);
}
template <class T>
inline T reference_callback_from_python<T>::operator()(PyObject* obj) const
{
return python::detail::void_ptr_to_reference(
callback_convert_reference(obj, lvalue_from_python_chain<T,true>::value)
, (T(*)())0);
}
template <class T>
inline pointer_callback_from_python<T>::pointer_callback_from_python()
{
detail::throw_if_not_registered(lvalue_from_python_chain<T,true>::value);
}
template <class T>
inline T pointer_callback_from_python<T>::operator()(PyObject* obj) const
{
return T(callback_convert_pointer(obj, lvalue_from_python_chain<T,true>::value));
}
template <class T>
inline value_callback_to_python<T>::value_callback_to_python(T const& x)
: callback_to_python_base(&x, to_python_function<T>::value)
{
}
template <class Ptr>
inline pointer_deep_callback_to_python<Ptr>::pointer_deep_callback_to_python(Ptr x)
: callback_to_python_base(x, pointee_to_python_function<Ptr>::value)
{
}
template <class T>
inline PyObject* reference_callback_to_python<T>::get_object(T& x)
{
to_python_indirect<T&,python::detail::make_reference_holder> convert;
if (!convert.convertible())
throw_no_class_registered();
return convert(x);
}
template <class T>
inline reference_callback_to_python<T>::reference_callback_to_python(T& x)
: callback_to_python_holder(get_object(x))
{
}
template <class Ptr>
inline pointer_shallow_callback_to_python<Ptr>::pointer_shallow_callback_to_python(Ptr x)
: callback_to_python_holder(get_object(x))
{}
template <class Ptr>
inline PyObject* pointer_shallow_callback_to_python<Ptr>::get_object(Ptr x)
{
to_python_indirect<Ptr,python::detail::make_reference_holder> convert;
if (!convert.convertible())
throw_no_class_registered();
return x ? convert(x) : python::detail::none();
}
}
template <class T>
inline callback_to_python<T>::callback_to_python(T const& x)
: base(x)
{}
}}} // namespace boost::python::converter
#endif // CALLBACK_DWA2002228_HPP

View File

@@ -1,288 +0,0 @@
#error obsolete
// Copyright David Abrahams 2002. 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.
#ifndef FROM_PYTHON_DWA2002127_HPP
# define FROM_PYTHON_DWA2002127_HPP
# include <boost/python/converter/find_from_python.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/type_traits/transform_traits.hpp>
# include <boost/type_traits/cv_traits.hpp>
# include <boost/python/converter/from_python_data.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/converter/registry.hpp>
# include <boost/python/converter/lvalue_from_python_chain.hpp>
# include <boost/python/converter/rvalue_from_python_chain.hpp>
# include <boost/python/detail/void_ptr.hpp>
# include <boost/python/back_reference.hpp>
namespace boost { namespace python
{
template <class T> struct from_python;
}}
namespace boost { namespace python { namespace converter {
struct from_python_base
{
public: // member functions
from_python_base(void* result);
from_python_base(PyObject*, lvalue_from_python_registration const* chain);
bool convertible() const;
protected: // member functions
void*const& result() const;
private: // data members
void* m_result;
};
// Used when T == U*const&
template <class T>
struct pointer_const_reference_from_python
{
typedef T result_type;
pointer_const_reference_from_python(PyObject*);
T operator()(PyObject*) const;
bool convertible() const;
private:
typename detail::referent_storage<T>::type m_result;
};
// Used when T == U*
template <class T>
struct pointer_from_python : from_python_base
{
typedef T result_type;
pointer_from_python(PyObject*);
T operator()(PyObject*) const;
};
// Used when T == U& and (T != V const& or T == W volatile&)
template <class T>
struct reference_from_python : from_python_base
{
typedef T result_type;
reference_from_python(PyObject*);
T operator()(PyObject*) const;
};
// ------- rvalue converters ---------
// Used for the case where T is a non-pointer, non-reference type OR
// is a const non-volatile reference to a non-pointer type.
template <class T>
struct rvalue_from_python
{
typedef typename boost::add_reference<
typename boost::add_const<T>::type
>::type result_type;
rvalue_from_python(PyObject*);
bool convertible() const;
result_type operator()(PyObject*);
private:
rvalue_data<result_type> m_data;
};
// ------- back-reference converters --------
// Converts to a (PyObject*,T) bundle, for when you need a reference
// back to the Python object
template <class T>
struct back_reference_from_python
: boost::python::from_python<typename T::type>
{
typedef T result_type;
back_reference_from_python(PyObject*);
T operator()(PyObject*);
private:
typedef boost::python::from_python<typename T::type> base;
};
template <class T>
struct select_from_python
{
BOOST_STATIC_CONSTANT(
bool, ptr = is_pointer<T>::value);
BOOST_STATIC_CONSTANT(
bool, ptr_cref
= boost::python::detail::is_reference_to_pointer<T>::value
&& boost::python::detail::is_reference_to_const<T>::value
&& !boost::python::detail::is_reference_to_volatile<T>::value);
BOOST_STATIC_CONSTANT(
bool, ref =
boost::python::detail::is_reference_to_non_const<T>::value
|| boost::python::detail::is_reference_to_volatile<T>::value);
BOOST_STATIC_CONSTANT(
bool, back_ref =
boost::python::is_back_reference<T>::value);
typedef typename mpl::select_type<
ptr
, pointer_from_python<T>
, typename mpl::select_type<
ptr_cref
, pointer_const_reference_from_python<T>
, typename mpl::select_type<
ref
, reference_from_python<T>
, typename mpl::select_type<
back_ref
, back_reference_from_python<T>
, rvalue_from_python<T>
>::type
>::type
>::type
>::type type;
};
//
// implementations
//
inline from_python_base::from_python_base(void* result)
: m_result(result)
{
}
inline from_python_base::from_python_base(
PyObject* source
, lvalue_from_python_registration const* chain)
: m_result(find(source, chain))
{
}
inline bool from_python_base::convertible() const
{
return m_result != 0;
}
inline void*const& from_python_base::result() const
{
return m_result;
}
// --------
namespace detail
{
template <class T>
struct null_ptr_owner
{
static T value;
};
template <class T> T null_ptr_owner<T>::value = 0;
template <class U>
inline U& null_ptr_reference(U&(*)())
{
return null_ptr_owner<U>::value;
}
}
template <class T>
inline pointer_const_reference_from_python<T>::pointer_const_reference_from_python(PyObject* p)
{
python::detail::write_void_ptr_reference(
m_result.bytes
, p == Py_None ? p : find(p, lvalue_from_python_chain<T>::value)
, (T(*)())0);
}
template <class T>
inline bool pointer_const_reference_from_python<T>::convertible() const
{
return python::detail::void_ptr_to_reference(m_result.bytes, (T(*)())0) != 0;
}
template <class T>
inline T pointer_const_reference_from_python<T>::operator()(PyObject* p) const
{
return (p == Py_None)
? detail::null_ptr_reference((T(*)())0)
: python::detail::void_ptr_to_reference(m_result.bytes, (T(*)())0);
}
// --------
template <class T>
inline pointer_from_python<T>::pointer_from_python(PyObject* p)
: from_python_base(p == Py_None ? p : find(p, lvalue_from_python_chain<T>::value))
{
}
template <class T>
inline T pointer_from_python<T>::operator()(PyObject* p) const
{
return (p == Py_None) ? 0 : T(result());
}
// --------
template <class T>
inline reference_from_python<T>::reference_from_python(PyObject* p)
: from_python_base(find(p,lvalue_from_python_chain<T>::value))
{
}
template <class T>
inline T reference_from_python<T>::operator()(PyObject*) const
{
return python::detail::void_ptr_to_reference(result(), (T(*)())0);
}
// -------
template <class T>
inline rvalue_from_python<T>::rvalue_from_python(PyObject* obj)
: m_data(find(obj, rvalue_from_python_chain<T>::value))
{
}
template <class T>
inline bool rvalue_from_python<T>::convertible() const
{
return m_data.stage1.convertible != 0;
}
template <class T>
inline typename rvalue_from_python<T>::result_type
rvalue_from_python<T>::operator()(PyObject* p)
{
if (m_data.stage1.construct != 0)
m_data.stage1.construct(p, &m_data.stage1);
return python::detail::void_ptr_to_reference(m_data.stage1.convertible, (result_type(*)())0);
}
template <class T>
back_reference_from_python<T>::back_reference_from_python(PyObject* x)
: base(x)
{
}
template <class T>
inline T
back_reference_from_python<T>::operator()(PyObject* x)
{
return T(x, base::operator()(x));
}
}}} // namespace boost::python::converter
#endif // FROM_PYTHON_DWA2002127_HPP

View File

@@ -164,35 +164,32 @@ namespace api
const_object_slice slice(object_cref, slice_nil) const;
object_slice slice(object_cref, slice_nil);
# if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
template <class T, class V>
const_object_slice
slice(T const& start, V const& end) const;
template <class T, class V>
object_slice
slice(T const& start, V const& end);
# else
template <class T, class V>
const_object_slice
slice(T const& start, V const& end) const
# if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
;
# else
{
return this->slice(
slice_bound<T>::type(start)
, slice_bound<V>::type(end));
}
# endif
template <class T, class V>
object_slice
slice(T const& start, V const& end)
# if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
;
# else
{
return this->slice(
slice_bound<T>::type(start)
, slice_bound<V>::type(end));
}
# endif
# endif
private:
// there is a confirmed CWPro8 codegen bug here. We prevent the
// early destruction of a temporary by binding a named object
@@ -204,35 +201,42 @@ namespace api
# endif
};
class object : public object_operators<object>
// VC6 and VC7 require this base class in order to generate the
// correct copy constructor for object. We can't define it there
// explicitly or it will complain of ambiguity.
struct object_base : object_operators<object>
{
// copy constructor without NULL checking, for efficiency.
object_base(object_base const&);
object_base(PyObject* ptr);
object_base& operator=(object_base const& rhs);
~object_base();
// Underlying object access -- returns a borrowed reference
PyObject* ptr() const;
private:
PyObject* m_ptr;
};
class object : public object_base
{
public:
# ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
// copy constructor without NULL checking, for efficiency. This
// confuses VC6/7 so object_initializer also handles this case.
object(object const&);
# endif
// explicit conversion from any C++ object to Python
template <class T>
explicit object(T const& x)
: m_ptr(object_initializer<is_proxy<T>::value>::get(
: object_base(object_initializer<is_proxy<T>::value>::get(
x, detail::convertible<object const*>::check(&x)))
{
}
// Throw error_already_set() if the handle is null.
explicit object(handle<> const&);
// Underlying object access -- returns a borrowed reference
PyObject* ptr() const;
public: // implementation detail -- for internal use only
explicit object(detail::borrowed_reference);
explicit object(detail::new_reference);
private:
PyObject* m_ptr;
};
//
@@ -327,26 +331,42 @@ namespace converter
//
inline object::object(handle<> const& x)
: m_ptr(incref(expect_non_null(x.get())))
: object_base(incref(expect_non_null(x.get())))
{}
# ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
// copy constructor without NULL checking, for efficiency
inline object::object(object const& rhs)
inline api::object_base::object_base(object_base const& rhs)
: m_ptr(incref(rhs.m_ptr))
{}
# endif
inline api::object_base::object_base(PyObject* p)
: m_ptr(p)
{}
inline api::object_base& api::object_base::operator=(api::object_base const& rhs)
{
Py_INCREF(rhs.m_ptr);
Py_DECREF(this->m_ptr);
this->m_ptr = rhs.m_ptr;
return *this;
}
inline api::object_base::~object_base()
{
Py_DECREF(m_ptr);
}
inline object::object(detail::borrowed_reference p)
: m_ptr(incref((PyObject*)p))
: object_base(incref((PyObject*)p))
{}
inline object::object(detail::new_reference p)
: m_ptr(expect_non_null((PyObject*)p))
: object_base(expect_non_null((PyObject*)p))
{}
inline PyObject* object::ptr() const
inline PyObject* api::object_base::ptr() const
{
return m_ptr;
}

View File

@@ -62,6 +62,8 @@
>>> obj_const_getitem(d, 'foo')
1
>>> obj_setitem42(d, 'foo')
>>> obj_getitem(d, 'foo')
42
>>> d['foo']
42
>>> obj_moveitem(d, 'foo', 'bar')
@@ -92,6 +94,22 @@
>>> class X: pass
...
>>> assert check_inplace(range(3), X())
Now make sure that object is actually managing reference counts
>>> import weakref
>>> class Z: pass
...
>>> z = Z()
>>> def death(r): print 'death'
...
>>> r = weakref.ref(z, death)
>>> z.foo = 1
>>> obj_getattr(z, 'foo')
1
>>> del z
death
'''
def run(args = None):