Changelog for previous commit
[abartlet/talloc-debian.git] / pytalloc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Python Talloc Module
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010-2011
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <Python.h>
21 #include <talloc.h>
22 #include <pytalloc.h>
23
24 static PyTypeObject TallocObject_Type;
25
26 #if PY_MAJOR_VERSION >= 3
27 #define PyStr_FromFormat PyUnicode_FromFormat
28 #else
29 #define PyStr_FromFormat PyString_FromFormat
30 #endif
31
32 /* print a talloc tree report for a talloc python object */
33 static PyObject *pytalloc_report_full(PyObject *self, PyObject *args)
34 {
35         PyObject *py_obj = Py_None;
36
37         if (!PyArg_ParseTuple(args, "|O", &py_obj))
38                 return NULL;
39
40         if (py_obj == Py_None) {
41                 talloc_report_full(NULL, stdout);
42         } else {
43                 talloc_report_full(pytalloc_get_mem_ctx(py_obj), stdout);
44         }
45         return Py_None;
46 }
47
48 /* enable null tracking */
49 static PyObject *pytalloc_enable_null_tracking(PyObject *self)
50 {
51         talloc_enable_null_tracking();
52         return Py_None;
53 }
54
55 /* return the number of talloc blocks */
56 static PyObject *pytalloc_total_blocks(PyObject *self, PyObject *args)
57 {
58         PyObject *py_obj = Py_None;
59
60         if (!PyArg_ParseTuple(args, "|O", &py_obj))
61                 return NULL;
62
63         if (py_obj == Py_None) {
64                 return PyLong_FromLong(talloc_total_blocks(NULL));
65         }
66
67         return PyLong_FromLong(talloc_total_blocks(pytalloc_get_mem_ctx(py_obj)));
68 }
69
70 static PyMethodDef talloc_methods[] = {
71         { "report_full", (PyCFunction)pytalloc_report_full, METH_VARARGS,
72                 "show a talloc tree for an object"},
73         { "enable_null_tracking", (PyCFunction)pytalloc_enable_null_tracking, METH_NOARGS,
74                 "enable tracking of the NULL object"},
75         { "total_blocks", (PyCFunction)pytalloc_total_blocks, METH_VARARGS,
76                 "return talloc block count"},
77         { NULL }
78 };
79
80 /**
81  * Default (but only slightly more useful than the default) implementation of Repr().
82  */
83 static PyObject *pytalloc_default_repr(PyObject *obj)
84 {
85         pytalloc_Object *talloc_obj = (pytalloc_Object *)obj;
86         PyTypeObject *type = (PyTypeObject*)PyObject_Type(obj);
87
88         return PyStr_FromFormat("<%s talloc object at 0x%p>",
89                                 type->tp_name, talloc_obj->ptr);
90 }
91
92 /**
93  * Simple dealloc for talloc-wrapping PyObjects
94  */
95 static void pytalloc_dealloc(PyObject* self)
96 {
97         pytalloc_Object *obj = (pytalloc_Object *)self;
98         assert(talloc_unlink(NULL, obj->talloc_ctx) != -1);
99         obj->talloc_ctx = NULL;
100         self->ob_type->tp_free(self);
101 }
102
103 /**
104  * Default (but only slightly more useful than the default) implementation of cmp.
105  */
106 #if PY_MAJOR_VERSION >= 3
107 static PyObject *pytalloc_default_richcmp(PyObject *obj1, PyObject *obj2, int op)
108 {
109         void *ptr1;
110         void *ptr2;
111         if (Py_TYPE(obj1) == Py_TYPE(obj2)) {
112                 /* When types match, compare pointers */
113                 ptr1 = pytalloc_get_ptr(obj1);
114                 ptr2 = pytalloc_get_ptr(obj2);
115         } else if (PyObject_TypeCheck(obj2, &TallocObject_Type)) {
116                 /* Otherwise, compare types */
117                 ptr1 = Py_TYPE(obj1);
118                 ptr2 = Py_TYPE(obj2);
119         } else {
120                 Py_INCREF(Py_NotImplemented);
121                 return Py_NotImplemented;
122         }
123         switch (op) {
124                 case Py_EQ: return PyBool_FromLong(ptr1 == ptr2);
125                 case Py_NE: return PyBool_FromLong(ptr1 != ptr2);
126                 case Py_LT: return PyBool_FromLong(ptr1 < ptr2);
127                 case Py_GT: return PyBool_FromLong(ptr1 > ptr2);
128                 case Py_LE: return PyBool_FromLong(ptr1 <= ptr2);
129                 case Py_GE: return PyBool_FromLong(ptr1 >= ptr2);
130         }
131         Py_INCREF(Py_NotImplemented);
132         return Py_NotImplemented;
133 }
134 #else
135 static int pytalloc_default_cmp(PyObject *_obj1, PyObject *_obj2)
136 {
137         pytalloc_Object *obj1 = (pytalloc_Object *)_obj1,
138                                          *obj2 = (pytalloc_Object *)_obj2;
139         if (obj1->ob_type != obj2->ob_type)
140                 return ((char *)obj1->ob_type - (char *)obj2->ob_type);
141
142         return ((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2));
143 }
144 #endif
145
146 static PyTypeObject TallocObject_Type = {
147         .tp_name = "talloc.Object",
148         .tp_doc = "Python wrapper for a talloc-maintained object.",
149         .tp_basicsize = sizeof(pytalloc_Object),
150         .tp_dealloc = (destructor)pytalloc_dealloc,
151         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
152         .tp_repr = pytalloc_default_repr,
153 #if PY_MAJOR_VERSION >= 3
154         .tp_richcompare = pytalloc_default_richcmp,
155 #else
156         .tp_compare = pytalloc_default_cmp,
157 #endif
158 };
159
160 #define MODULE_DOC PyDoc_STR("Python wrapping of talloc-maintained objects.")
161
162 #if PY_MAJOR_VERSION >= 3
163 static struct PyModuleDef moduledef = {
164     PyModuleDef_HEAD_INIT,
165     .m_name = "talloc",
166     .m_doc = MODULE_DOC,
167     .m_size = -1,
168     .m_methods = talloc_methods,
169 };
170 #endif
171
172 static PyObject *module_init(void);
173 static PyObject *module_init(void)
174 {
175         PyObject *m;
176
177         if (PyType_Ready(&TallocObject_Type) < 0)
178                 return NULL;
179
180 #if PY_MAJOR_VERSION >= 3
181         m = PyModule_Create(&moduledef);
182 #else
183         m = Py_InitModule3("talloc", talloc_methods, MODULE_DOC);
184 #endif
185         if (m == NULL)
186                 return NULL;
187
188         Py_INCREF(&TallocObject_Type);
189         PyModule_AddObject(m, "Object", (PyObject *)&TallocObject_Type);
190         return m;
191 }
192
193 #if PY_MAJOR_VERSION >= 3
194 PyMODINIT_FUNC PyInit_talloc(void);
195 PyMODINIT_FUNC PyInit_talloc(void)
196 {
197         return module_init();
198 }
199 #else
200 void inittalloc(void);
201 void inittalloc(void)
202 {
203         module_init();
204 }
205 #endif