Skip to content

Commit 41634f9

Browse files
committed
Use ref everywhere for reliability
[SVN r12394]
1 parent 93501af commit 41634f9

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

include/boost/python/class.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class class_ : objects::class_base
100100
{
101101
// Use function::add_to_namespace to achieve overloading if
102102
// appropriate.
103-
objects::function::add_to_namespace(this->object(), name, detail::wrap_function(f));
103+
objects::function::add_to_namespace(this->object(), name, ref(detail::wrap_function(f)));
104104
return *this;
105105
}
106106

include/boost/python/module.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class BOOST_PYTHON_DECL module_base
2525
// Add elements to the module
2626
void add(PyObject* x, const char* name);
2727
void add(PyTypeObject* x, const char* name = 0);
28-
void add(ref x, const char*name);
28+
void add(ref const& x, const char*name);
2929

3030
// Return true iff a module is currently being built.
3131
static bool initializing();
@@ -35,10 +35,10 @@ class BOOST_PYTHON_DECL module_base
3535
static string name();
3636

3737
// Return a pointer to the Python module object being built
38-
PyObject* module() const;
38+
ref module() const;
3939

4040
private:
41-
PyObject* m_module;
41+
ref m_module;
4242
static PyMethodDef initial_methods[1];
4343
};
4444

@@ -65,7 +65,7 @@ class module : public module_base
6565
//
6666
// inline implementations
6767
//
68-
inline PyObject* module_base::module() const
68+
inline ref module_base::module() const
6969
{
7070
return m_module;
7171
}

include/boost/python/object/function.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct BOOST_PYTHON_DECL function : PyObject
2727
// a function object (this class), and an existing function is
2828
// already there, add it as an overload.
2929
static void add_to_namespace(
30-
PyObject* name_space, char const* name, PyObject* attribute);
30+
ref const& name_space, char const* name, ref const& attribute);
3131

3232
private: // helper functions
3333
void argument_error(PyObject* args, PyObject* keywords) const;

src/module.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ string module_base::name()
2727
}
2828

2929
module_base::module_base(const char* name)
30-
: m_module(Py_InitModule(const_cast<char*>(name), initial_methods))
30+
: m_module(
31+
Py_InitModule(const_cast<char*>(name), initial_methods)
32+
, ref::increment_count)
3133
{
3234
// If this fails, you've created more than 1 module object in your module
3335
assert(name_holder.get() == 0);
34-
name_holder = ref(PyObject_GetAttrString(m_module, const_cast<char*>("__name__")));
36+
name_holder = ref(PyObject_GetAttrString(
37+
m_module.get() , const_cast<char*>("__name__")));
3538
}
3639

3740
module_base::~module_base()
@@ -44,11 +47,11 @@ void module_base::add(PyObject* x, const char* name)
4447
add(ref(x), name);
4548
}
4649

47-
void module_base::add(ref x, const char* name)
50+
void module_base::add(ref const& x, const char* name)
4851
{
4952
// Use function::add_to_namespace to achieve overloading if
5053
// appropriate.
51-
objects::function::add_to_namespace(m_module, name, x.get());
54+
objects::function::add_to_namespace(m_module, name, x);
5255
}
5356

5457
void module_base::add(PyTypeObject* x, const char* name /*= 0*/)

src/object/function.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,21 @@ void function::add_overload(function* overload)
7777
}
7878

7979
void function::add_to_namespace(
80-
PyObject* name_space, char const* name_, PyObject* attribute_)
80+
ref const& name_space, char const* name_, ref const& attribute)
8181
{
82-
ref attribute(attribute_, ref::increment_count);
83-
string name(name_);
82+
string const name(name_);
83+
PyObject* const ns = name_space.get();
8484

85-
if (attribute_->ob_type == &function_type)
85+
if (attribute->ob_type == &function_type)
8686
{
8787
PyObject* dict = 0;
8888

89-
if (PyClass_Check(name_space))
90-
dict = ((PyClassObject*)name_space)->cl_dict;
91-
else if (PyType_Check(name_space))
92-
dict = ((PyTypeObject*)name_space)->tp_dict;
89+
if (PyClass_Check(ns))
90+
dict = ((PyClassObject*)ns)->cl_dict;
91+
else if (PyType_Check(ns))
92+
dict = ((PyTypeObject*)ns)->tp_dict;
9393
else
94-
dict = PyObject_GetAttrString(name_space, "__dict__");
94+
dict = PyObject_GetAttrString(ns, "__dict__");
9595

9696
if (dict == 0)
9797
throw error_already_set();
@@ -101,14 +101,14 @@ void function::add_to_namespace(
101101
if (existing.get() && existing->ob_type == &function_type)
102102
{
103103
static_cast<function*>(existing.get())->add_overload(
104-
static_cast<function*>(attribute_));
104+
static_cast<function*>(attribute.get()));
105105
return;
106106
}
107107
}
108108

109109
// The PyObject_GetAttrString() call above left an active error
110110
PyErr_Clear();
111-
if (PyObject_SetAttr(name_space, name.get(), attribute_) < 0)
111+
if (PyObject_SetAttr(ns, name.get(), attribute.get()) < 0)
112112
throw error_already_set();
113113
}
114114

0 commit comments

Comments
 (0)