1 // Copyright David Abrahams 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #include <boost/python/object/life_support.hpp>
6 #include <boost/python/detail/none.hpp>
7 #include <boost/python/refcount.hpp>
9 namespace boost
{ namespace python
{ namespace objects
{
20 life_support_dealloc(PyObject
* self
)
22 Py_XDECREF(((life_support
*)self
)->patient
);
23 self
->ob_type
->tp_free(self
);
27 life_support_call(PyObject
*self
, PyObject
*arg
, PyObject
* /*kw*/)
29 // Let the patient die now
30 Py_XDECREF(((life_support
*)self
)->patient
);
31 ((life_support
*)self
)->patient
= 0;
32 // Let the weak reference die. This probably kills us.
33 Py_XDECREF(PyTuple_GET_ITEM(arg
, 0));
34 return ::boost::python::detail::none();
38 PyTypeObject life_support_type
= {
39 PyVarObject_HEAD_INIT(NULL
, 0)//(&PyType_Type)
40 const_cast<char*>("Boost.Python.life_support"),
43 life_support_dealloc
, /* tp_dealloc */
48 0, //(reprfunc)func_repr, /* tp_repr */
50 0, /* tp_as_sequence */
51 0, /* tp_as_mapping */
53 life_support_call
, /* tp_call */
55 0, // PyObject_GenericGetAttr, /* tp_getattro */
56 0, // PyObject_GenericSetAttr, /* tp_setattro */
58 Py_TPFLAGS_DEFAULT
/* | Py_TPFLAGS_HAVE_GC */,/* tp_flags */
60 0, // (traverseproc)func_traverse, /* tp_traverse */
62 0, /* tp_richcompare */
63 0, //offsetof(PyLife_SupportObject, func_weakreflist), /* tp_weaklistoffset */
67 0, // func_memberlist, /* tp_members */
68 0, //func_getsetlist, /* tp_getset */
73 0, //offsetof(PyLife_SupportObject, func_dict), /* tp_dictoffset */
82 0, /* tp_subclasses */
84 #if PYTHON_API_VERSION >= 1012
89 PyObject
* make_nurse_and_patient(PyObject
* nurse
, PyObject
* patient
)
91 if (nurse
== Py_None
|| nurse
== patient
)
94 if (Py_TYPE(&life_support_type
) == 0)
96 Py_TYPE(&life_support_type
) = &PyType_Type
;
97 PyType_Ready(&life_support_type
);
100 life_support
* system
= PyObject_New(life_support
, &life_support_type
);
106 // We're going to leak this reference, but don't worry; the
107 // life_support system decrements it when the nurse dies.
108 PyObject
* weakref
= PyWeakref_NewRef(nurse
, (PyObject
*)system
);
110 // weakref has either taken ownership, or we have to release it
116 system
->patient
= patient
;
117 Py_XINCREF(patient
); // hang on to the patient until death
121 }}} // namespace boost::python::objects