s3:lib: fix const warnings in popt_common.c
[Samba.git] / lib / talloc / pytalloc.c
blob80196c6c77b58c395f5dde78e3d19f6d9de4c4a3
1 /*
2 Unix SMB/CIFS implementation.
3 Python Talloc Module
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010-2011
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.
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.
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/>.
20 #include <Python.h>
21 #include <talloc.h>
22 #include <pytalloc.h>
24 void inittalloc(void);
26 /* print a talloc tree report for a talloc python object */
27 static PyObject *pytalloc_report_full(PyObject *self, PyObject *args)
29 PyObject *py_obj = Py_None;
31 if (!PyArg_ParseTuple(args, "|O", &py_obj))
32 return NULL;
34 if (py_obj == Py_None) {
35 talloc_report_full(NULL, stdout);
36 } else {
37 talloc_report_full(pytalloc_get_mem_ctx(py_obj), stdout);
39 return Py_None;
42 /* enable null tracking */
43 static PyObject *pytalloc_enable_null_tracking(PyObject *self)
45 talloc_enable_null_tracking();
46 return Py_None;
49 /* return the number of talloc blocks */
50 static PyObject *pytalloc_total_blocks(PyObject *self, PyObject *args)
52 PyObject *py_obj = Py_None;
54 if (!PyArg_ParseTuple(args, "|O", &py_obj))
55 return NULL;
57 if (py_obj == Py_None) {
58 return PyLong_FromLong(talloc_total_blocks(NULL));
61 return PyLong_FromLong(talloc_total_blocks(pytalloc_get_mem_ctx(py_obj)));
64 static PyMethodDef talloc_methods[] = {
65 { "report_full", (PyCFunction)pytalloc_report_full, METH_VARARGS,
66 "show a talloc tree for an object"},
67 { "enable_null_tracking", (PyCFunction)pytalloc_enable_null_tracking, METH_NOARGS,
68 "enable tracking of the NULL object"},
69 { "total_blocks", (PyCFunction)pytalloc_total_blocks, METH_VARARGS,
70 "return talloc block count"},
71 { NULL }
74 /**
75 * Default (but only slightly more useful than the default) implementation of Repr().
77 static PyObject *pytalloc_default_repr(PyObject *obj)
79 pytalloc_Object *talloc_obj = (pytalloc_Object *)obj;
80 PyTypeObject *type = (PyTypeObject*)PyObject_Type(obj);
82 return PyString_FromFormat("<%s talloc object at 0x%p>",
83 type->tp_name, talloc_obj->ptr);
86 /**
87 * Simple dealloc for talloc-wrapping PyObjects
89 static void pytalloc_dealloc(PyObject* self)
91 pytalloc_Object *obj = (pytalloc_Object *)self;
92 assert(talloc_unlink(NULL, obj->talloc_ctx) != -1);
93 obj->talloc_ctx = NULL;
94 self->ob_type->tp_free(self);
97 /**
98 * Default (but only slightly more useful than the default) implementation of cmp.
100 static int pytalloc_default_cmp(PyObject *_obj1, PyObject *_obj2)
102 pytalloc_Object *obj1 = (pytalloc_Object *)_obj1,
103 *obj2 = (pytalloc_Object *)_obj2;
104 if (obj1->ob_type != obj2->ob_type)
105 return (obj1->ob_type - obj2->ob_type);
107 return ((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2));
110 static PyTypeObject TallocObject_Type = {
111 .tp_name = "talloc.Object",
112 .tp_doc = "Python wrapper for a talloc-maintained object.",
113 .tp_basicsize = sizeof(pytalloc_Object),
114 .tp_dealloc = (destructor)pytalloc_dealloc,
115 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
116 .tp_repr = pytalloc_default_repr,
117 .tp_compare = pytalloc_default_cmp,
120 void inittalloc(void)
122 PyObject *m;
124 if (PyType_Ready(&TallocObject_Type) < 0)
125 return;
127 m = Py_InitModule3("talloc", talloc_methods,
128 "Python wrapping of talloc-maintained objects.");
129 if (m == NULL)
130 return;
132 Py_INCREF(&TallocObject_Type);
133 PyModule_AddObject(m, "Object", (PyObject *)&TallocObject_Type);