diff --git a/include/boost/python/class.hpp b/include/boost/python/class.hpp index d61a8fc0..76a74df2 100644 --- a/include/boost/python/class.hpp +++ b/include/boost/python/class.hpp @@ -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; } diff --git a/include/boost/python/module.hpp b/include/boost/python/module.hpp index 317cc5e4..318f4a64 100644 --- a/include/boost/python/module.hpp +++ b/include/boost/python/module.hpp @@ -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; } diff --git a/include/boost/python/object/function.hpp b/include/boost/python/object/function.hpp index cc2695d8..c239b6e6 100644 --- a/include/boost/python/object/function.hpp +++ b/include/boost/python/object/function.hpp @@ -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; diff --git a/src/module.cpp b/src/module.cpp index 0851805f..917f8d3a 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -27,11 +27,14 @@ string module_base::name() } module_base::module_base(const char* name) - : m_module(Py_InitModule(const_cast(name), initial_methods)) + : m_module( + Py_InitModule(const_cast(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("__name__"))); + name_holder = ref(PyObject_GetAttrString( + m_module.get() , const_cast("__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*/) diff --git a/src/object/function.cpp b/src/object/function.cpp index 3755dfb1..7878fa47 100644 --- a/src/object/function.cpp +++ b/src/object/function.cpp @@ -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(existing.get())->add_overload( - static_cast(attribute_)); + static_cast(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(); }