2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-26 18:52:26 +00:00

Use ref everywhere for reliability

[SVN r12394]
This commit is contained in:
Dave Abrahams
2002-01-21 06:56:27 +00:00
parent 93501af046
commit 41634f9998
5 changed files with 24 additions and 21 deletions

View File

@@ -100,7 +100,7 @@ class class_ : objects::class_base
{
// Use function::add_to_namespace to achieve overloading if
// appropriate.
objects::function::add_to_namespace(this->object(), name, detail::wrap_function(f));
objects::function::add_to_namespace(this->object(), name, ref(detail::wrap_function(f)));
return *this;
}

View File

@@ -25,7 +25,7 @@ class BOOST_PYTHON_DECL module_base
// Add elements to the module
void add(PyObject* x, const char* name);
void add(PyTypeObject* x, const char* name = 0);
void add(ref x, const char*name);
void add(ref const& x, const char*name);
// Return true iff a module is currently being built.
static bool initializing();
@@ -35,10 +35,10 @@ class BOOST_PYTHON_DECL module_base
static string name();
// Return a pointer to the Python module object being built
PyObject* module() const;
ref module() const;
private:
PyObject* m_module;
ref m_module;
static PyMethodDef initial_methods[1];
};
@@ -65,7 +65,7 @@ class module : public module_base
//
// inline implementations
//
inline PyObject* module_base::module() const
inline ref module_base::module() const
{
return m_module;
}

View File

@@ -27,7 +27,7 @@ struct BOOST_PYTHON_DECL function : PyObject
// a function object (this class), and an existing function is
// already there, add it as an overload.
static void add_to_namespace(
PyObject* name_space, char const* name, PyObject* attribute);
ref const& name_space, char const* name, ref const& attribute);
private: // helper functions
void argument_error(PyObject* args, PyObject* keywords) const;

View File

@@ -27,11 +27,14 @@ string module_base::name()
}
module_base::module_base(const char* name)
: m_module(Py_InitModule(const_cast<char*>(name), initial_methods))
: m_module(
Py_InitModule(const_cast<char*>(name), initial_methods)
, ref::increment_count)
{
// If this fails, you've created more than 1 module object in your module
assert(name_holder.get() == 0);
name_holder = ref(PyObject_GetAttrString(m_module, const_cast<char*>("__name__")));
name_holder = ref(PyObject_GetAttrString(
m_module.get() , const_cast<char*>("__name__")));
}
module_base::~module_base()
@@ -44,11 +47,11 @@ void module_base::add(PyObject* x, const char* name)
add(ref(x), name);
}
void module_base::add(ref x, const char* name)
void module_base::add(ref const& x, const char* name)
{
// Use function::add_to_namespace to achieve overloading if
// appropriate.
objects::function::add_to_namespace(m_module, name, x.get());
objects::function::add_to_namespace(m_module, name, x);
}
void module_base::add(PyTypeObject* x, const char* name /*= 0*/)

View File

@@ -77,21 +77,21 @@ void function::add_overload(function* overload)
}
void function::add_to_namespace(
PyObject* name_space, char const* name_, PyObject* attribute_)
ref const& name_space, char const* name_, ref const& attribute)
{
ref attribute(attribute_, ref::increment_count);
string name(name_);
string const name(name_);
PyObject* const ns = name_space.get();
if (attribute_->ob_type == &function_type)
if (attribute->ob_type == &function_type)
{
PyObject* dict = 0;
if (PyClass_Check(name_space))
dict = ((PyClassObject*)name_space)->cl_dict;
else if (PyType_Check(name_space))
dict = ((PyTypeObject*)name_space)->tp_dict;
if (PyClass_Check(ns))
dict = ((PyClassObject*)ns)->cl_dict;
else if (PyType_Check(ns))
dict = ((PyTypeObject*)ns)->tp_dict;
else
dict = PyObject_GetAttrString(name_space, "__dict__");
dict = PyObject_GetAttrString(ns, "__dict__");
if (dict == 0)
throw error_already_set();
@@ -101,14 +101,14 @@ void function::add_to_namespace(
if (existing.get() && existing->ob_type == &function_type)
{
static_cast<function*>(existing.get())->add_overload(
static_cast<function*>(attribute_));
static_cast<function*>(attribute.get()));
return;
}
}
// The PyObject_GetAttrString() call above left an active error
PyErr_Clear();
if (PyObject_SetAttr(name_space, name.get(), attribute_) < 0)
if (PyObject_SetAttr(ns, name.get(), attribute.get()) < 0)
throw error_already_set();
}