2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-27 07:02:15 +00:00

Lots of documentation updates, plus the associated code shuffling needed to expose the right things to users

[SVN r13975]
This commit is contained in:
Dave Abrahams
2002-05-19 04:57:44 +00:00
parent c928eded74
commit ba2f18ce21
40 changed files with 1555 additions and 913 deletions

View File

@@ -10,6 +10,7 @@
#include <boost/python/converter/from_python_data.hpp>
#include <boost/python/converter/registry.hpp>
#include <boost/python/reference.hpp>
#include <boost/python/type_id.hpp>
#include <boost/python/errors.hpp>
#include <boost/cast.hpp>
#include <string>
@@ -38,7 +39,7 @@ namespace
registry::insert(
&slot_rvalue_from_python<T,SlotPolicy>::convertible
, &slot_rvalue_from_python<T,SlotPolicy>::construct
, undecorated_type_id<T>()
, type_id<T>()
);
}
@@ -280,7 +281,7 @@ void initialize_builtin_converters()
slot_rvalue_from_python<std::complex<long double>,complex_rvalue_from_python>();
// Add an lvalue converter for char which gets us char const*
registry::insert(convert_to_cstring,undecorated_type_id<char>());
registry::insert(convert_to_cstring,type_id<char>());
// Register by-value converters to std::string
slot_rvalue_from_python<std::string, string_rvalue_from_python>();

View File

@@ -30,7 +30,7 @@ namespace // <unnamed>
PyTypeObject* m_class_object;
};
typedef std::map<undecorated_type_id_t, entry> registry_t;
typedef std::map<type_info, entry> registry_t;
registry_t& entries()
{
@@ -50,7 +50,7 @@ namespace // <unnamed>
return registry;
}
entry* find(undecorated_type_id_t type)
entry* find(type_info type)
{
return &entries()[type];
}
@@ -67,12 +67,12 @@ namespace // <unnamed>
namespace registry
{
to_python_function_t const& get_to_python_function(
undecorated_type_id_t key)
type_info key)
{
return find(key)->m_to_python_converter;
}
void insert(to_python_function_t f, undecorated_type_id_t source_t)
void insert(to_python_function_t f, type_info source_t)
{
to_python_function_t& slot = find(source_t)->m_to_python_converter;
assert(slot == 0); // we have a problem otherwise
@@ -85,7 +85,7 @@ namespace registry
}
// Insert an lvalue from_python converter
void insert(void* (*convert)(PyObject*), undecorated_type_id_t key)
void insert(void* (*convert)(PyObject*), type_info key)
{
entry* found = find(key);
lvalue_from_python_registration *registration = new lvalue_from_python_registration;
@@ -99,7 +99,7 @@ namespace registry
// Insert an rvalue from_python converter
void insert(void* (*convertible)(PyObject*)
, constructor_function construct
, undecorated_type_id_t key)
, type_info key)
{
entry* found = find(key);
rvalue_from_python_registration *registration = new rvalue_from_python_registration;
@@ -112,7 +112,7 @@ namespace registry
// Insert an rvalue from_python converter
void push_back(void* (*convertible)(PyObject*)
, constructor_function construct
, undecorated_type_id_t key)
, type_info key)
{
rvalue_from_python_registration** found = &find(key)->m_rvalue_from_python;
while (*found != 0)
@@ -125,17 +125,17 @@ namespace registry
*found = registration;
}
PyTypeObject*& class_object(undecorated_type_id_t key)
PyTypeObject*& class_object(type_info key)
{
return find(key)->m_class_object;
}
lvalue_from_python_registration*& lvalue_converters(undecorated_type_id_t key)
lvalue_from_python_registration*& lvalue_converters(type_info key)
{
return find(key)->m_lvalue_from_python;
}
rvalue_from_python_registration*& rvalue_converters(undecorated_type_id_t key)
rvalue_from_python_registration*& rvalue_converters(type_info key)
{
return find(key)->m_rvalue_from_python;
}

View File

@@ -4,30 +4,33 @@
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python/converter/type_id.hpp>
#include <boost/python/type_id.hpp>
#include <boost/python/detail/decorated_type_id.hpp>
#if !defined(__GNUC__) || __GNUC__ >= 3 || __SGI_STL_PORT
# include <ostream>
#else
# include <ostream.h>
#endif
namespace boost { namespace python { namespace converter {
namespace boost { namespace python {
BOOST_PYTHON_DECL std::ostream& operator<<(std::ostream& os, undecorated_type_id_t const& x)
BOOST_PYTHON_DECL std::ostream& operator<<(std::ostream& os, type_info const& x)
{
return os << x.name();
}
BOOST_PYTHON_DECL std::ostream& operator<<(std::ostream& os, type_id_t const& x)
namespace detail
{
os << x.m_base_type;
if (x.m_decoration & type_id_t::const_)
os << " const";
if (x.m_decoration & type_id_t::volatile_)
os << " volatile";
if (x.m_decoration & type_id_t::reference)
os << "&";
return os;
BOOST_PYTHON_DECL std::ostream& operator<<(std::ostream& os, detail::decorated_type_info const& x)
{
os << x.m_base_type;
if (x.m_decoration & decorated_type_info::const_)
os << " const";
if (x.m_decoration & decorated_type_info::volatile_)
os << " volatile";
if (x.m_decoration & decorated_type_info::reference)
os << "&";
return os;
}
}
}}} // namespace boost::python::converter
}} // namespace boost::python::converter

View File

@@ -12,7 +12,7 @@
#include <functional>
#include <vector>
namespace boost { namespace python { namespace objects {
namespace boost { namespace python {
instance_holder::instance_holder()
: m_next(0)
@@ -35,269 +35,272 @@ type_is_gc(PyTypeObject *python_type)
return python_type->tp_flags & Py_TPFLAGS_HEAPTYPE;
}
PyTypeObject class_metatype_object = {
static PyTypeObject class_metatype_object = {
PyObject_HEAD_INIT(0)//&PyType_Type)
0,
"Boost.Python.class",
PyType_Type.tp_basicsize,
0,
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
0,
"Boost.Python.class",
PyType_Type.tp_basicsize,
0,
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT // | Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, //&PyType_Type, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, // filled in with type_new /* tp_new */
0, // filled in with __PyObject_GC_Del /* tp_free */
(inquiry)type_is_gc, /* tp_is_gc */
| Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, //&PyType_Type, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, // filled in with type_new /* tp_new */
0, // filled in with __PyObject_GC_Del /* tp_free */
(inquiry)type_is_gc, /* tp_is_gc */
};
// Get the metatype object for all extension classes.
BOOST_PYTHON_DECL ref class_metatype()
{
if (class_metatype_object.tp_dict == 0)
{
class_metatype_object.ob_type = &PyType_Type;
class_metatype_object.tp_base = &PyType_Type;
if (PyType_Ready(&class_metatype_object))
return ref();
}
return ref((PyObject*)&class_metatype_object, ref::increment_count);
}
extern "C"
{
static void instance_dealloc(PyObject* inst)
{
instance* kill_me = (instance*)inst;
for (instance_holder* p = kill_me->objects, *next; p != 0; p = next)
{
next = p->next();
delete p;
}
inst->ob_type->tp_free(inst);
}
}
// Do we really need this? I'm beginning to think we don't!
PyTypeObject class_type_object = {
PyObject_HEAD_INIT(0) //&class_metatype_object)
0,
"Boost.Python.instance",
sizeof(instance),
0,
instance_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT // | Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, //&PyBaseObject_Type, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew
};
BOOST_PYTHON_DECL ref class_type()
{
if (class_type_object.tp_dict == 0)
{
class_type_object.ob_type = (PyTypeObject*)class_metatype().release();
class_type_object.tp_base = &PyBaseObject_Type;
if (PyType_Ready(&class_type_object))
return ref();
}
return ref((PyObject*)&class_type_object, ref::increment_count);
}
// Install the instance data for a C++ object into a Python instance
// object.
void instance_holder::install(PyObject* self) throw()
{
assert(self->ob_type->ob_type == &class_metatype_object);
m_next = ((instance*)self)->objects;
((instance*)self)->objects = this;
m_next = ((objects::instance*)self)->objects;
((objects::instance*)self)->objects = this;
}
BOOST_PYTHON_DECL void*
find_instance_impl(PyObject* inst, converter::undecorated_type_id_t type)
{
if (inst->ob_type->ob_type != &class_metatype_object)
return 0;
instance* self = reinterpret_cast<instance*>(inst);
for (instance_holder* match = self->objects; match != 0; match = match->next())
{
void* const found = match->holds(type);
if (found)
return found;
}
return 0;
}
namespace
namespace objects
{
struct class_registry
// Get the metatype object for all extension classes.
BOOST_PYTHON_DECL ref class_metatype()
{
public:
ref get(class_id id) const;
ref query(class_id id) const;
void set(class_id, ref class_object);
private:
typedef detail::map_entry<class_id,ref> entry;
std::vector<entry> m_impl;
if (class_metatype_object.tp_dict == 0)
{
class_metatype_object.ob_type = &PyType_Type;
class_metatype_object.tp_base = &PyType_Type;
if (PyType_Ready(&class_metatype_object))
return ref();
}
return ref((PyObject*)&class_metatype_object, ref::increment_count);
}
extern "C"
{
static void instance_dealloc(PyObject* inst)
{
instance* kill_me = (instance*)inst;
for (instance_holder* p = kill_me->objects, *next; p != 0; p = next)
{
next = p->next();
delete p;
}
inst->ob_type->tp_free(inst);
}
}
static PyTypeObject class_type_object = {
PyObject_HEAD_INIT(0) //&class_metatype_object)
0,
"Boost.Python.instance",
sizeof(instance),
0,
instance_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT // | Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, //&PyBaseObject_Type, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew
};
class_registry& registry()
BOOST_PYTHON_DECL ref class_type()
{
static class_registry x;
return x;
}
inline ref class_registry::query(class_id id) const
{
std::vector<entry>::const_iterator start = m_impl.begin();
std::vector<entry>::const_iterator finish = m_impl.end();
std::vector<entry>::const_iterator p
= boost::detail::lower_bound(start, finish, id);
return (p == finish || p->key != id) ? ref() : p->value;
}
inline ref class_registry::get(class_id id) const
{
ref result(this->query(id));
if (result.get() == 0)
if (class_type_object.tp_dict == 0)
{
string report("extension class wrapper for base class ");
(report += id.name()) += " has not been created yet";
PyErr_SetObject(PyExc_RuntimeError, report.get());
throw_error_already_set();
class_type_object.ob_type = (PyTypeObject*)class_metatype().release();
class_type_object.tp_base = &PyBaseObject_Type;
if (PyType_Ready(&class_type_object))
return ref();
}
return result;
return ref((PyObject*)&class_type_object, ref::increment_count);
}
inline void class_registry::set(class_id id, ref object)
BOOST_PYTHON_DECL void*
find_instance_impl(PyObject* inst, type_info type)
{
std::vector<entry>::iterator start = m_impl.begin();
std::vector<entry>::iterator finish = m_impl.end();
m_impl.insert(
boost::detail::lower_bound(start, finish, id)
, entry(id, object));
converter::registry::class_object(id) = (PyTypeObject*)object.get();
if (inst->ob_type->ob_type != &class_metatype_object)
return 0;
instance* self = reinterpret_cast<instance*>(inst);
for (instance_holder* match = self->objects; match != 0; match = match->next())
{
void* const found = match->holds(type);
if (found)
return found;
}
return 0;
}
}
class_base::class_base(
char const* name, std::size_t num_types, class_id const* const types)
{
class_registry& r = registry();
assert(num_types >= 1);
tuple bases(std::max(num_types - 1, static_cast<std::size_t>(1)));
if (num_types > 1)
namespace
{
struct class_registry
{
for (std::size_t i = 1; i < num_types; ++i)
bases.set_item(i - 1, r.get(types[i]));
}
else
public:
ref get(class_id id) const;
ref query(class_id id) const;
void set(class_id, ref class_object);
private:
typedef detail::map_entry<class_id,ref> entry;
std::vector<entry> m_impl;
};
class_registry& registry()
{
bases.set_item(0, class_type());
static class_registry x;
return x;
}
inline ref class_registry::query(class_id id) const
{
std::vector<entry>::const_iterator start = m_impl.begin();
std::vector<entry>::const_iterator finish = m_impl.end();
std::vector<entry>::const_iterator p
= boost::detail::lower_bound(start, finish, id);
return (p == finish || p->key != id) ? ref() : p->value;
}
inline ref class_registry::get(class_id id) const
{
ref result(this->query(id));
if (result.get() == 0)
{
string report("extension class wrapper for base class ");
(report += id.name()) += " has not been created yet";
PyErr_SetObject(PyExc_RuntimeError, report.get());
throw_error_already_set();
}
return result;
}
inline void class_registry::set(class_id id, ref object)
{
std::vector<entry>::iterator start = m_impl.begin();
std::vector<entry>::iterator finish = m_impl.end();
m_impl.insert(
boost::detail::lower_bound(start, finish, id)
, entry(id, object));
converter::registry::class_object(id) = (PyTypeObject*)object.get();
}
}
class_base::class_base(
char const* name, std::size_t num_types, class_id const* const types)
{
class_registry& r = registry();
assert(num_types >= 1);
tuple bases(std::max(num_types - 1, static_cast<std::size_t>(1)));
if (num_types > 1)
{
for (std::size_t i = 1; i < num_types; ++i)
bases.set_item(i - 1, r.get(types[i]));
}
else
{
bases.set_item(0, class_type());
}
tuple args(3);
args.set_item(0, string(name).reference());
args.set_item(1, bases.reference());
args.set_item(2, dictionary().reference());
tuple args(3);
args.set_item(0, string(name).reference());
args.set_item(1, bases.reference());
args.set_item(2, dictionary().reference());
m_object = ref(PyObject_CallObject(class_metatype().get(), args.get()));
r.set(types[0], m_object);
}
m_object = ref(PyObject_CallObject(class_metatype().get(), args.get()));
r.set(types[0], m_object);
}
extern "C"
{
// This declaration needed due to broken Python 2.2 headers
extern DL_IMPORT(PyTypeObject) PyProperty_Type;
}
extern "C"
{
// This declaration needed due to broken Python 2.2 headers
extern DL_IMPORT(PyTypeObject) PyProperty_Type;
}
void class_base::add_property(char const* name, ref const& fget)
{
ref property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "O", fget.get()));
setattr(name, property);
}
void class_base::add_property(char const* name, ref const& fget)
{
ref property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "O", fget.get()));
setattr(name, property);
}
void class_base::add_property(char const* name, ref const& fget, ref const& fset)
{
ref property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "OO", fget.get(), fset.get()));
setattr(name, property);
}
void class_base::add_property(char const* name, ref const& fget, ref const& fset)
{
ref property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "OO", fget.get(), fset.get()));
setattr(name, property);
}
void class_base::setattr(char const* name, ref const& x)
{
if (PyObject_SetAttrString(object().get(), const_cast<char*>(name), x.get()) < 0)
throw_error_already_set();
}
void class_base::setattr(char const* name, ref const& x)
{
if (PyObject_SetAttrString(object().get(), const_cast<char*>(name), x.get()) < 0)
throw_error_already_set();
}
BOOST_PYTHON_DECL ref registered_class_object(class_id id)
{
return registry().query(id);
}
BOOST_PYTHON_DECL ref registered_class_object(class_id id)
{
return registry().query(id);
}
} // namespace objects
}}} // namespace boost::python::objects
}} // namespace boost::python

View File

@@ -4,7 +4,7 @@
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python/object/inheritance.hpp>
#include <boost/python/converter/type_id.hpp>
#include <boost/python/type_id.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/reverse_graph.hpp>
@@ -45,7 +45,7 @@ namespace
// Here we put together the low-level data structures of the
// casting graph representation.
//
typedef python::converter::undecorated_type_id_t class_id;
typedef python::type_info class_id;
// represents a graph of available casts