2 Samba Unix SMB/CIFS implementation.
4 C utilities for the pytalloc test suite.
5 Provides the "_test_pytalloc" Python module.
7 NOTE: Please read talloc_guide.txt for full documentation
9 Copyright (C) Petr Viktorin 2015
11 ** NOTE! The following LGPL license applies to the talloc
12 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
33 static PyObject
*testpytalloc_new(PyTypeObject
*mod
,
34 PyObject
*Py_UNUSED(ignored
))
36 char *obj
= talloc_strdup(NULL
, "This is a test string");;
37 return pytalloc_steal(pytalloc_GetObjectType(), obj
);
40 static PyObject
*testpytalloc_get_object_type(PyObject
*mod
,
41 PyObject
*Py_UNUSED(ignored
))
43 PyObject
*type
= (PyObject
*)pytalloc_GetObjectType();
48 static PyObject
*testpytalloc_base_new(PyTypeObject
*mod
,
49 PyObject
*Py_UNUSED(ignored
))
51 char *obj
= talloc_strdup(NULL
, "This is a test string for a BaseObject");;
52 return pytalloc_steal(pytalloc_GetBaseObjectType(), obj
);
55 static PyObject
*testpytalloc_base_get_object_type(PyObject
*mod
,
56 PyObject
*Py_UNUSED(ignored
))
58 PyObject
*type
= (PyObject
*)pytalloc_GetBaseObjectType();
63 static PyObject
*testpytalloc_reference(PyObject
*mod
, PyObject
*args
) {
64 PyObject
*source
= NULL
;
67 if (!PyArg_ParseTuple(args
, "O!", pytalloc_GetObjectType(), &source
))
70 ptr
= pytalloc_get_ptr(source
);
71 return pytalloc_reference_ex(pytalloc_GetObjectType(), ptr
, ptr
);
74 static PyObject
*testpytalloc_base_reference(PyObject
*mod
, PyObject
*args
) {
75 PyObject
*source
= NULL
;
78 if (!PyArg_ParseTuple(args
, "O!", pytalloc_GetBaseObjectType(), &source
)) {
81 mem_ctx
= pytalloc_get_mem_ctx(source
);
82 return pytalloc_reference_ex(pytalloc_GetBaseObjectType(), mem_ctx
, mem_ctx
);
85 static PyMethodDef test_talloc_methods
[] = {
86 { "new", (PyCFunction
)testpytalloc_new
, METH_NOARGS
,
87 "create a talloc Object with a testing string"},
88 { "get_object_type", (PyCFunction
)testpytalloc_get_object_type
, METH_NOARGS
,
89 "call pytalloc_GetObjectType"},
90 { "base_new", (PyCFunction
)testpytalloc_base_new
, METH_NOARGS
,
91 "create a talloc BaseObject with a testing string"},
92 { "base_get_object_type", (PyCFunction
)testpytalloc_base_get_object_type
, METH_NOARGS
,
93 "call pytalloc_GetBaseObjectType"},
94 { "reference", (PyCFunction
)testpytalloc_reference
, METH_VARARGS
,
95 "call pytalloc_reference_ex"},
96 { "base_reference", (PyCFunction
)testpytalloc_base_reference
, METH_VARARGS
,
97 "call pytalloc_reference_ex"},
101 static PyTypeObject DObject_Type
;
103 static int dobject_destructor(void *ptr
)
105 PyObject
*destructor_func
= *talloc_get_type(ptr
, PyObject
*);
107 ret
= PyObject_CallObject(destructor_func
, NULL
);
108 Py_DECREF(destructor_func
);
117 static PyObject
*dobject_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
119 PyObject
*destructor_func
= NULL
;
122 if (!PyArg_ParseTuple(args
, "O", &destructor_func
))
124 Py_INCREF(destructor_func
);
126 obj
= talloc(NULL
, PyObject
*);
127 *obj
= destructor_func
;
129 talloc_set_destructor((void*)obj
, dobject_destructor
);
130 return pytalloc_steal(&DObject_Type
, obj
);
133 static PyTypeObject DObject_Type
= {
134 .tp_name
= "_test_pytalloc.DObject",
135 .tp_basicsize
= sizeof(pytalloc_Object
),
137 .tp_new
= dobject_new
,
138 .tp_flags
= Py_TPFLAGS_DEFAULT
,
139 .tp_doc
= "test talloc object that calls a function when underlying data is freed\n",
142 static PyTypeObject DBaseObject_Type
;
144 static int d_base_object_destructor(void *ptr
)
146 PyObject
*destructor_func
= *talloc_get_type(ptr
, PyObject
*);
148 ret
= PyObject_CallObject(destructor_func
, NULL
);
149 Py_DECREF(destructor_func
);
158 static PyObject
*d_base_object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
160 PyObject
*destructor_func
= NULL
;
163 if (!PyArg_ParseTuple(args
, "O", &destructor_func
))
165 Py_INCREF(destructor_func
);
167 obj
= talloc(NULL
, PyObject
*);
168 *obj
= destructor_func
;
170 talloc_set_destructor((void*)obj
, d_base_object_destructor
);
171 return pytalloc_steal(&DBaseObject_Type
, obj
);
174 static PyTypeObject DBaseObject_Type
= {
175 .tp_name
= "_test_pytalloc.DBaseObject",
177 .tp_new
= d_base_object_new
,
178 .tp_flags
= Py_TPFLAGS_DEFAULT
,
179 .tp_doc
= "test talloc object that calls a function when underlying data is freed\n",
182 #define MODULE_DOC PyDoc_STR("Test utility module for pytalloc")
184 #if PY_MAJOR_VERSION >= 3
185 static struct PyModuleDef moduledef
= {
186 PyModuleDef_HEAD_INIT
,
187 .m_name
= "_test_pytalloc",
188 .m_doc
= PyDoc_STR("Test utility module for pytalloc"),
190 .m_methods
= test_talloc_methods
,
194 static PyObject
*module_init(void);
195 static PyObject
*module_init(void)
199 DObject_Type
.tp_base
= pytalloc_GetObjectType();
200 if (PyType_Ready(&DObject_Type
) < 0) {
204 DBaseObject_Type
.tp_basicsize
= pytalloc_BaseObject_size();
205 DBaseObject_Type
.tp_base
= pytalloc_GetBaseObjectType();
206 if (PyType_Ready(&DBaseObject_Type
) < 0) {
210 #if PY_MAJOR_VERSION >= 3
211 m
= PyModule_Create(&moduledef
);
213 m
= Py_InitModule3("_test_pytalloc", test_talloc_methods
, MODULE_DOC
);
220 Py_INCREF(&DObject_Type
);
221 Py_INCREF(DObject_Type
.tp_base
);
222 PyModule_AddObject(m
, "DObject", (PyObject
*)&DObject_Type
);
224 Py_INCREF(&DBaseObject_Type
);
225 Py_INCREF(DBaseObject_Type
.tp_base
);
226 PyModule_AddObject(m
, "DBaseObject", (PyObject
*)&DBaseObject_Type
);
232 #if PY_MAJOR_VERSION >= 3
233 PyMODINIT_FUNC
PyInit__test_pytalloc(void);
234 PyMODINIT_FUNC
PyInit__test_pytalloc(void)
236 return module_init();
239 void init_test_pytalloc(void);
240 void init_test_pytalloc(void)