lib: talloc: Mark talloc_autofree_context() as deprecated.
[samba.git] / lib / talloc / test_pytalloc.c
1 /*
2    Samba Unix SMB/CIFS implementation.
3
4    C utilities for the pytalloc test suite.
5    Provides the "_test_pytalloc" Python module.
6
7    NOTE: Please read talloc_guide.txt for full documentation
8
9    Copyright (C) Petr Viktorin 2015
10
11      ** NOTE! The following LGPL license applies to the talloc
12      ** library. This does NOT imply that all of Samba is released
13      ** under the LGPL
14
15    This library is free software; you can redistribute it and/or
16    modify it under the terms of the GNU Lesser General Public
17    License as published by the Free Software Foundation; either
18    version 3 of the License, or (at your option) any later version.
19
20    This library is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    Lesser General Public License for more details.
24
25    You should have received a copy of the GNU Lesser General Public
26    License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #include <Python.h>
30 #include <talloc.h>
31 #include <pytalloc.h>
32
33 static PyObject *testpytalloc_new(PyTypeObject *mod)
34 {
35         char *obj = talloc_strdup(NULL, "This is a test string");;
36         return pytalloc_steal(pytalloc_GetObjectType(), obj);
37 }
38
39 static PyObject *testpytalloc_get_object_type(PyObject *mod) {
40         PyObject *type = (PyObject *)pytalloc_GetObjectType();
41         Py_INCREF(type);
42         return type;
43 }
44
45 static PyObject *testpytalloc_base_new(PyTypeObject *mod)
46 {
47         char *obj = talloc_strdup(NULL, "This is a test string for a BaseObject");;
48         return pytalloc_steal(pytalloc_GetBaseObjectType(), obj);
49 }
50
51 static PyObject *testpytalloc_base_get_object_type(PyObject *mod) {
52         PyObject *type = (PyObject *)pytalloc_GetBaseObjectType();
53         Py_INCREF(type);
54         return type;
55 }
56
57 static PyObject *testpytalloc_reference(PyObject *mod, PyObject *args) {
58         PyObject *source = NULL;
59         void *ptr;
60
61         if (!PyArg_ParseTuple(args, "O!", pytalloc_GetObjectType(), &source))
62                 return NULL;
63
64         ptr = pytalloc_get_ptr(source);
65         return pytalloc_reference_ex(pytalloc_GetObjectType(), ptr, ptr);
66 }
67
68 static PyObject *testpytalloc_base_reference(PyObject *mod, PyObject *args) {
69         PyObject *source = NULL;
70         void *mem_ctx;
71
72         if (!PyArg_ParseTuple(args, "O!", pytalloc_GetBaseObjectType(), &source)) {
73                 return NULL;
74         }
75         mem_ctx = pytalloc_get_mem_ctx(source);
76         return pytalloc_reference_ex(pytalloc_GetBaseObjectType(), mem_ctx, mem_ctx);
77 }
78
79 static PyMethodDef test_talloc_methods[] = {
80         { "new", (PyCFunction)testpytalloc_new, METH_NOARGS,
81                 "create a talloc Object with a testing string"},
82         { "get_object_type", (PyCFunction)testpytalloc_get_object_type, METH_NOARGS,
83                 "call pytalloc_GetObjectType"},
84         { "base_new", (PyCFunction)testpytalloc_base_new, METH_NOARGS,
85                 "create a talloc BaseObject with a testing string"},
86         { "base_get_object_type", (PyCFunction)testpytalloc_base_get_object_type, METH_NOARGS,
87                 "call pytalloc_GetBaseObjectType"},
88         { "reference", (PyCFunction)testpytalloc_reference, METH_VARARGS,
89                 "call pytalloc_reference_ex"},
90         { "base_reference", (PyCFunction)testpytalloc_base_reference, METH_VARARGS,
91                 "call pytalloc_reference_ex"},
92         { NULL }
93 };
94
95 static PyTypeObject DObject_Type;
96
97 static int dobject_destructor(void *ptr)
98 {
99         PyObject *destructor_func = *talloc_get_type(ptr, PyObject*);
100         PyObject *ret;
101         ret = PyObject_CallObject(destructor_func, NULL);
102         Py_DECREF(destructor_func);
103         if (ret == NULL) {
104                 PyErr_Print();
105         } else {
106                 Py_DECREF(ret);
107         }
108         return 0;
109 }
110
111 static PyObject *dobject_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
112 {
113         PyObject *destructor_func = NULL;
114         PyObject **obj;
115
116         if (!PyArg_ParseTuple(args, "O", &destructor_func))
117                 return NULL;
118         Py_INCREF(destructor_func);
119
120         obj = talloc(NULL, PyObject*);
121         *obj = destructor_func;
122
123         talloc_set_destructor((void*)obj, dobject_destructor);
124         return pytalloc_steal(&DObject_Type, obj);
125 }
126
127 static PyTypeObject DObject_Type = {
128         .tp_name = "_test_pytalloc.DObject",
129         .tp_basicsize = sizeof(pytalloc_Object),
130         .tp_methods = NULL,
131         .tp_new = dobject_new,
132         .tp_flags = Py_TPFLAGS_DEFAULT,
133         .tp_doc = "test talloc object that calls a function when underlying data is freed\n",
134 };
135
136 static PyTypeObject DBaseObject_Type;
137
138 static int d_base_object_destructor(void *ptr)
139 {
140         PyObject *destructor_func = *talloc_get_type(ptr, PyObject*);
141         PyObject *ret;
142         ret = PyObject_CallObject(destructor_func, NULL);
143         Py_DECREF(destructor_func);
144         if (ret == NULL) {
145                 PyErr_Print();
146         } else {
147                 Py_DECREF(ret);
148         }
149         return 0;
150 }
151
152 static PyObject *d_base_object_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
153 {
154         PyObject *destructor_func = NULL;
155         PyObject **obj;
156
157         if (!PyArg_ParseTuple(args, "O", &destructor_func))
158                 return NULL;
159         Py_INCREF(destructor_func);
160
161         obj = talloc(NULL, PyObject*);
162         *obj = destructor_func;
163
164         talloc_set_destructor((void*)obj, d_base_object_destructor);
165         return pytalloc_steal(&DBaseObject_Type, obj);
166 }
167
168 static PyTypeObject DBaseObject_Type = {
169         .tp_name = "_test_pytalloc.DBaseObject",
170         .tp_methods = NULL,
171         .tp_new = d_base_object_new,
172         .tp_flags = Py_TPFLAGS_DEFAULT,
173         .tp_doc = "test talloc object that calls a function when underlying data is freed\n",
174 };
175
176 #define MODULE_DOC PyDoc_STR("Test utility module for pytalloc")
177
178 #if PY_MAJOR_VERSION >= 3
179 static struct PyModuleDef moduledef = {
180     PyModuleDef_HEAD_INIT,
181     .m_name = "_test_pytalloc",
182     .m_doc = PyDoc_STR("Test utility module for pytalloc"),
183     .m_size = -1,
184     .m_methods = test_talloc_methods,
185 };
186 #endif
187
188 static PyObject *module_init(void);
189 static PyObject *module_init(void)
190 {
191         PyObject *m;
192
193         DObject_Type.tp_base = pytalloc_GetObjectType();
194         if (PyType_Ready(&DObject_Type) < 0) {
195                 return NULL;
196         }
197
198         DBaseObject_Type.tp_basicsize = pytalloc_BaseObject_size();
199         DBaseObject_Type.tp_base = pytalloc_GetBaseObjectType();
200         if (PyType_Ready(&DBaseObject_Type) < 0) {
201                 return NULL;
202         }
203
204 #if PY_MAJOR_VERSION >= 3
205         m = PyModule_Create(&moduledef);
206 #else
207         m = Py_InitModule3("_test_pytalloc", test_talloc_methods, MODULE_DOC);
208 #endif
209
210         if (m == NULL) {
211                 return NULL;
212         }
213
214         Py_INCREF(&DObject_Type);
215         Py_INCREF(DObject_Type.tp_base);
216         PyModule_AddObject(m, "DObject", (PyObject *)&DObject_Type);
217
218         Py_INCREF(&DBaseObject_Type);
219         Py_INCREF(DBaseObject_Type.tp_base);
220         PyModule_AddObject(m, "DBaseObject", (PyObject *)&DBaseObject_Type);
221
222         return m;
223 }
224
225
226 #if PY_MAJOR_VERSION >= 3
227 PyMODINIT_FUNC PyInit__test_pytalloc(void);
228 PyMODINIT_FUNC PyInit__test_pytalloc(void)
229 {
230         return module_init();
231 }
232 #else
233 void init_test_pytalloc(void);
234 void init_test_pytalloc(void)
235 {
236         module_init();
237 }
238 #endif