talloc: Add python talloc module, move convenience functions to it.
[samba.git] / lib / talloc / pytalloc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Python Talloc Module
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
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 /* print a talloc tree report for a talloc python object */
25 static PyObject *py_talloc_report_full(PyObject *self, PyObject *args)
26 {
27         PyObject *py_obj;
28         PyTypeObject *type;
29
30         if (!PyArg_ParseTuple(args, "O", &py_obj))
31                 return NULL;
32
33         if (py_obj == Py_None) {
34                 talloc_report_full(NULL, stdout);
35         } else {
36                 type = (PyTypeObject*)PyObject_Type(py_obj);
37                 talloc_report_full(py_talloc_get_mem_ctx(py_obj), stdout);
38         }
39         return Py_None;
40 }
41
42 /* enable null tracking */
43 static PyObject *py_talloc_enable_null_tracking(PyObject *self, PyObject *args)
44 {
45         talloc_enable_null_tracking();
46         return Py_None;
47 }
48
49 /* return the number of talloc blocks */
50 static PyObject *py_talloc_total_blocks(PyObject *self, PyObject *args)
51 {
52         PyObject *py_obj;
53         PyTypeObject *type;
54
55         if (!PyArg_ParseTuple(args, "O", &py_obj))
56                 return NULL;
57
58         if (py_obj == Py_None) {
59                 return PyLong_FromLong(talloc_total_blocks(NULL));
60         }
61
62         type = (PyTypeObject*)PyObject_Type(py_obj);
63
64         return PyLong_FromLong(talloc_total_blocks(py_talloc_get_mem_ctx(py_obj)));
65 }
66
67 static PyMethodDef talloc_methods[] = {
68         { "report_full", (PyCFunction)py_talloc_report_full, METH_VARARGS,
69                 "show a talloc tree for an object"},
70         { "enable_null_tracking", (PyCFunction)py_talloc_enable_null_tracking, METH_VARARGS,
71                 "enable tracking of the NULL object"},
72         { "total_blocks", (PyCFunction)py_talloc_total_blocks, METH_VARARGS,
73                 "return talloc block count"},
74         { NULL }
75 };
76
77 void inittalloc(void)
78 {
79         PyObject *m;
80
81         m = Py_InitModule3("talloc", talloc_methods, "Debug utilities for talloc-wrapped objects.");
82         if (m == NULL)
83                 return;
84 }