- Got rid of newmodule.c
[python/dscho.git] / Doc / ext / cycle-gc.c
blobc3a0caa5b7a6584b76d184a589b0665f7c972a97
1 #include "Python.h"
3 typedef struct {
4 PyObject_HEAD
5 PyObject *container;
6 } MyObject;
8 static int
9 my_traverse(MyObject *self, visitproc visit, void *arg)
11 if (self->container != NULL)
12 return visit(self->container, arg);
13 else
14 return 0;
17 static int
18 my_clear(MyObject *self)
20 Py_XDECREF(self->container);
21 self->container = NULL;
23 return 0;
26 static void
27 my_dealloc(MyObject *self)
29 PyObject_GC_UnTrack((PyObject *) self);
30 Py_XDECREF(self->container);
31 PyObject_GC_Del(self);
34 static PyTypeObject
35 MyObject_Type = {
36 PyObject_HEAD_INIT(NULL)
38 "MyObject",
39 sizeof(MyObject),
41 (destructor)my_dealloc, /* tp_dealloc */
42 0, /* tp_print */
43 0, /* tp_getattr */
44 0, /* tp_setattr */
45 0, /* tp_compare */
46 0, /* tp_repr */
47 0, /* tp_as_number */
48 0, /* tp_as_sequence */
49 0, /* tp_as_mapping */
50 0, /* tp_hash */
51 0, /* tp_call */
52 0, /* tp_str */
53 0, /* tp_getattro */
54 0, /* tp_setattro */
55 0, /* tp_as_buffer */
56 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
57 0, /* tp_doc */
58 (traverseproc)my_traverse, /* tp_traverse */
59 (inquiry)my_clear, /* tp_clear */
60 0, /* tp_richcompare */
61 0, /* tp_weaklistoffset */
64 /* This constructor should be made accessible from Python. */
65 static PyObject *
66 new_object(PyObject *unused, PyObject *args)
68 PyObject *container = NULL;
69 MyObject *result = NULL;
71 if (PyArg_ParseTuple(args, "|O:new_object", &container)) {
72 result = PyObject_GC_New(MyObject, &MyObject_Type);
73 if (result != NULL) {
74 result->container = container;
75 PyObject_GC_Track(result);
78 return (PyObject *) result;