nfs4acls: Use talloc_realloc()
[Samba.git] / lib / talloc / test_pytalloc.c
blobf66b4e59b524bd04e97ce6ebe511a6d560351e7d
1 /*
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
13 ** under the LGPL
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/>.
29 #include <Python.h>
30 #include <talloc.h>
31 #include <pytalloc.h>
33 static PyObject *testpytalloc_new(PyTypeObject *mod)
35 char *obj = talloc_strdup(NULL, "This is a test string");;
36 return pytalloc_steal(pytalloc_GetObjectType(), obj);
39 static PyObject *testpytalloc_get_object_type(PyObject *mod) {
40 PyObject *type = (PyObject *)pytalloc_GetObjectType();
41 Py_INCREF(type);
42 return type;
45 static PyObject *testpytalloc_reference(PyObject *mod, PyObject *args) {
46 pytalloc_Object *source = NULL;
47 void *ptr;
49 if (!PyArg_ParseTuple(args, "O!", pytalloc_GetObjectType(), &source))
50 return NULL;
52 ptr = source->ptr;
53 return pytalloc_reference_ex(pytalloc_GetObjectType(), ptr, ptr);
56 static PyMethodDef test_talloc_methods[] = {
57 { "new", (PyCFunction)testpytalloc_new, METH_NOARGS,
58 "create a talloc Object with a testing string"},
59 { "get_object_type", (PyCFunction)testpytalloc_get_object_type, METH_NOARGS,
60 "call pytalloc_GetObjectType"},
61 { "reference", (PyCFunction)testpytalloc_reference, METH_VARARGS,
62 "call pytalloc_reference_ex"},
63 { NULL }
66 static PyTypeObject DObject_Type;
68 static int dobject_destructor(void *ptr)
70 PyObject *destructor_func = *talloc_get_type(ptr, PyObject*);
71 PyObject *ret;
72 ret = PyObject_CallObject(destructor_func, NULL);
73 Py_DECREF(destructor_func);
74 if (ret == NULL) {
75 PyErr_Print();
76 } else {
77 Py_DECREF(ret);
79 return 0;
82 static PyObject *dobject_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
84 PyObject *destructor_func = NULL;
85 PyObject **obj;
87 if (!PyArg_ParseTuple(args, "O", &destructor_func))
88 return NULL;
89 Py_INCREF(destructor_func);
91 obj = talloc(NULL, PyObject*);
92 *obj = destructor_func;
94 talloc_set_destructor((void*)obj, dobject_destructor);
95 return pytalloc_steal(&DObject_Type, obj);
98 static PyTypeObject DObject_Type = {
99 .tp_name = "_test_pytalloc.DObject",
100 .tp_basicsize = sizeof(pytalloc_Object),
101 .tp_methods = NULL,
102 .tp_new = dobject_new,
103 .tp_flags = Py_TPFLAGS_DEFAULT,
104 .tp_doc = "test talloc object that calls a function when underlying data is freed\n",
107 #define MODULE_DOC PyDoc_STR("Test utility module for pytalloc")
109 #if PY_MAJOR_VERSION >= 3
110 static struct PyModuleDef moduledef = {
111 PyModuleDef_HEAD_INIT,
112 .m_name = "_test_pytalloc",
113 .m_doc = PyDoc_STR("Test utility module for pytalloc"),
114 .m_size = -1,
115 .m_methods = test_talloc_methods,
117 #endif
119 static PyObject *module_init(void);
120 static PyObject *module_init(void)
122 PyObject *m;
124 DObject_Type.tp_base = pytalloc_GetObjectType();
125 if (PyType_Ready(&DObject_Type) < 0) {
126 return NULL;
129 #if PY_MAJOR_VERSION >= 3
130 m = PyModule_Create(&moduledef);
131 #else
132 m = Py_InitModule3("_test_pytalloc", test_talloc_methods, MODULE_DOC);
133 #endif
135 if (m == NULL) {
136 return NULL;
139 Py_INCREF(&DObject_Type);
140 Py_INCREF(DObject_Type.tp_base);
141 PyModule_AddObject(m, "DObject", (PyObject *)&DObject_Type);
143 return m;
147 #if PY_MAJOR_VERSION >= 3
148 PyMODINIT_FUNC PyInit__test_pytalloc(void);
149 PyMODINIT_FUNC PyInit__test_pytalloc(void)
151 return module_init();
153 #else
154 void init_test_pytalloc(void);
155 void init_test_pytalloc(void)
157 module_init();
159 #endif