Blame test_pytalloc.c

rpm-build 95f51c
/*
rpm-build 95f51c
   Samba Unix SMB/CIFS implementation.
rpm-build 95f51c
rpm-build 95f51c
   C utilities for the pytalloc test suite.
rpm-build 95f51c
   Provides the "_test_pytalloc" Python module.
rpm-build 95f51c
rpm-build 95f51c
   NOTE: Please read talloc_guide.txt for full documentation
rpm-build 95f51c
rpm-build 95f51c
   Copyright (C) Petr Viktorin 2015
rpm-build 95f51c
rpm-build 95f51c
     ** NOTE! The following LGPL license applies to the talloc
rpm-build 95f51c
     ** library. This does NOT imply that all of Samba is released
rpm-build 95f51c
     ** under the LGPL
rpm-build 95f51c
rpm-build 95f51c
   This library is free software; you can redistribute it and/or
rpm-build 95f51c
   modify it under the terms of the GNU Lesser General Public
rpm-build 95f51c
   License as published by the Free Software Foundation; either
rpm-build 95f51c
   version 3 of the License, or (at your option) any later version.
rpm-build 95f51c
rpm-build 95f51c
   This library is distributed in the hope that it will be useful,
rpm-build 95f51c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 95f51c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 95f51c
   Lesser General Public License for more details.
rpm-build 95f51c
rpm-build 95f51c
   You should have received a copy of the GNU Lesser General Public
rpm-build 95f51c
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
rpm-build 95f51c
*/
rpm-build 95f51c
rpm-build 95f51c
#include <Python.h>
rpm-build 95f51c
#include <talloc.h>
rpm-build 95f51c
#include <pytalloc.h>
rpm-build 95f51c
rpm-build 95f51c
static PyObject *testpytalloc_new(PyTypeObject *mod,
rpm-build 95f51c
		PyObject *Py_UNUSED(ignored))
rpm-build 95f51c
{
rpm-build 95f51c
	char *obj = talloc_strdup(NULL, "This is a test string");;
rpm-build 95f51c
	return pytalloc_steal(pytalloc_GetObjectType(), obj);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyObject *testpytalloc_get_object_type(PyObject *mod,
rpm-build 95f51c
		PyObject *Py_UNUSED(ignored))
rpm-build 95f51c
{
rpm-build 95f51c
	PyObject *type = (PyObject *)pytalloc_GetObjectType();
rpm-build 95f51c
	Py_INCREF(type);
rpm-build 95f51c
	return type;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyObject *testpytalloc_base_new(PyTypeObject *mod,
rpm-build 95f51c
		PyObject *Py_UNUSED(ignored))
rpm-build 95f51c
{
rpm-build 95f51c
	char *obj = talloc_strdup(NULL, "This is a test string for a BaseObject");;
rpm-build 95f51c
	return pytalloc_steal(pytalloc_GetBaseObjectType(), obj);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyObject *testpytalloc_base_get_object_type(PyObject *mod,
rpm-build 95f51c
		PyObject *Py_UNUSED(ignored))
rpm-build 95f51c
{
rpm-build 95f51c
	PyObject *type = (PyObject *)pytalloc_GetBaseObjectType();
rpm-build 95f51c
	Py_INCREF(type);
rpm-build 95f51c
	return type;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyObject *testpytalloc_reference(PyObject *mod, PyObject *args) {
rpm-build 95f51c
	PyObject *source = NULL;
rpm-build 95f51c
	void *ptr;
rpm-build 95f51c
rpm-build 95f51c
	if (!PyArg_ParseTuple(args, "O!", pytalloc_GetObjectType(), &source))
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
rpm-build 95f51c
	ptr = pytalloc_get_ptr(source);
rpm-build 95f51c
	return pytalloc_reference_ex(pytalloc_GetObjectType(), ptr, ptr);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyObject *testpytalloc_base_reference(PyObject *mod, PyObject *args) {
rpm-build 95f51c
	PyObject *source = NULL;
rpm-build 95f51c
	void *mem_ctx;
rpm-build 95f51c
rpm-build 95f51c
	if (!PyArg_ParseTuple(args, "O!", pytalloc_GetBaseObjectType(), &source)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	mem_ctx = pytalloc_get_mem_ctx(source);
rpm-build 95f51c
	return pytalloc_reference_ex(pytalloc_GetBaseObjectType(), mem_ctx, mem_ctx);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyMethodDef test_talloc_methods[] = {
rpm-build 95f51c
	{ "new", (PyCFunction)testpytalloc_new, METH_NOARGS,
rpm-build 95f51c
		"create a talloc Object with a testing string"},
rpm-build 95f51c
	{ "get_object_type", (PyCFunction)testpytalloc_get_object_type, METH_NOARGS,
rpm-build 95f51c
		"call pytalloc_GetObjectType"},
rpm-build 95f51c
	{ "base_new", (PyCFunction)testpytalloc_base_new, METH_NOARGS,
rpm-build 95f51c
		"create a talloc BaseObject with a testing string"},
rpm-build 95f51c
	{ "base_get_object_type", (PyCFunction)testpytalloc_base_get_object_type, METH_NOARGS,
rpm-build 95f51c
		"call pytalloc_GetBaseObjectType"},
rpm-build 95f51c
	{ "reference", (PyCFunction)testpytalloc_reference, METH_VARARGS,
rpm-build 95f51c
		"call pytalloc_reference_ex"},
rpm-build 95f51c
	{ "base_reference", (PyCFunction)testpytalloc_base_reference, METH_VARARGS,
rpm-build 95f51c
		"call pytalloc_reference_ex"},
rpm-build 95f51c
	{ NULL }
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
static PyTypeObject DObject_Type;
rpm-build 95f51c
rpm-build 95f51c
static int dobject_destructor(void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	PyObject *destructor_func = *talloc_get_type(ptr, PyObject*);
rpm-build 95f51c
	PyObject *ret;
rpm-build 95f51c
	ret = PyObject_CallObject(destructor_func, NULL);
rpm-build 95f51c
	Py_DECREF(destructor_func);
rpm-build 95f51c
	if (ret == NULL) {
rpm-build 95f51c
		PyErr_Print();
rpm-build 95f51c
	} else {
rpm-build 95f51c
		Py_DECREF(ret);
rpm-build 95f51c
	}
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyObject *dobject_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
rpm-build 95f51c
{
rpm-build 95f51c
	PyObject *destructor_func = NULL;
rpm-build 95f51c
	PyObject **obj;
rpm-build 95f51c
rpm-build 95f51c
	if (!PyArg_ParseTuple(args, "O", &destructor_func))
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	Py_INCREF(destructor_func);
rpm-build 95f51c
rpm-build 95f51c
	obj = talloc(NULL, PyObject*);
rpm-build 95f51c
	*obj = destructor_func;
rpm-build 95f51c
rpm-build 95f51c
	talloc_set_destructor((void*)obj, dobject_destructor);
rpm-build 95f51c
	return pytalloc_steal(&DObject_Type, obj);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyTypeObject DObject_Type = {
rpm-build 95f51c
	.tp_name = "_test_pytalloc.DObject",
rpm-build 95f51c
	.tp_basicsize = sizeof(pytalloc_Object),
rpm-build 95f51c
	.tp_methods = NULL,
rpm-build 95f51c
	.tp_new = dobject_new,
rpm-build 95f51c
	.tp_flags = Py_TPFLAGS_DEFAULT,
rpm-build 95f51c
	.tp_doc = "test talloc object that calls a function when underlying data is freed\n",
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
static PyTypeObject DBaseObject_Type;
rpm-build 95f51c
rpm-build 95f51c
static int d_base_object_destructor(void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	PyObject *destructor_func = *talloc_get_type(ptr, PyObject*);
rpm-build 95f51c
	PyObject *ret;
rpm-build 95f51c
	ret = PyObject_CallObject(destructor_func, NULL);
rpm-build 95f51c
	Py_DECREF(destructor_func);
rpm-build 95f51c
	if (ret == NULL) {
rpm-build 95f51c
		PyErr_Print();
rpm-build 95f51c
	} else {
rpm-build 95f51c
		Py_DECREF(ret);
rpm-build 95f51c
	}
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyObject *d_base_object_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
rpm-build 95f51c
{
rpm-build 95f51c
	PyObject *destructor_func = NULL;
rpm-build 95f51c
	PyObject **obj;
rpm-build 95f51c
rpm-build 95f51c
	if (!PyArg_ParseTuple(args, "O", &destructor_func))
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	Py_INCREF(destructor_func);
rpm-build 95f51c
rpm-build 95f51c
	obj = talloc(NULL, PyObject*);
rpm-build 95f51c
	*obj = destructor_func;
rpm-build 95f51c
rpm-build 95f51c
	talloc_set_destructor((void*)obj, d_base_object_destructor);
rpm-build 95f51c
	return pytalloc_steal(&DBaseObject_Type, obj);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static PyTypeObject DBaseObject_Type = {
rpm-build 95f51c
	.tp_name = "_test_pytalloc.DBaseObject",
rpm-build 95f51c
	.tp_methods = NULL,
rpm-build 95f51c
	.tp_new = d_base_object_new,
rpm-build 95f51c
	.tp_flags = Py_TPFLAGS_DEFAULT,
rpm-build 95f51c
	.tp_doc = "test talloc object that calls a function when underlying data is freed\n",
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
#define MODULE_DOC PyDoc_STR("Test utility module for pytalloc")
rpm-build 95f51c
rpm-build 95f51c
#if PY_MAJOR_VERSION >= 3
rpm-build 95f51c
static struct PyModuleDef moduledef = {
rpm-build 95f51c
    PyModuleDef_HEAD_INIT,
rpm-build 95f51c
    .m_name = "_test_pytalloc",
rpm-build 95f51c
    .m_doc = PyDoc_STR("Test utility module for pytalloc"),
rpm-build 95f51c
    .m_size = -1,
rpm-build 95f51c
    .m_methods = test_talloc_methods,
rpm-build 95f51c
};
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
static PyObject *module_init(void);
rpm-build 95f51c
static PyObject *module_init(void)
rpm-build 95f51c
{
rpm-build 95f51c
	PyObject *m;
rpm-build 95f51c
rpm-build 95f51c
	DObject_Type.tp_base = pytalloc_GetObjectType();
rpm-build 95f51c
	if (PyType_Ready(&DObject_Type) < 0) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	DBaseObject_Type.tp_basicsize = pytalloc_BaseObject_size();
rpm-build 95f51c
	DBaseObject_Type.tp_base = pytalloc_GetBaseObjectType();
rpm-build 95f51c
	if (PyType_Ready(&DBaseObject_Type) < 0) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
#if PY_MAJOR_VERSION >= 3
rpm-build 95f51c
	m = PyModule_Create(&moduledef);
rpm-build 95f51c
#else
rpm-build 95f51c
	m = Py_InitModule3("_test_pytalloc", test_talloc_methods, MODULE_DOC);
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
	if (m == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	Py_INCREF(&DObject_Type);
rpm-build 95f51c
	Py_INCREF(DObject_Type.tp_base);
rpm-build 95f51c
	PyModule_AddObject(m, "DObject", (PyObject *)&DObject_Type);
rpm-build 95f51c
rpm-build 95f51c
	Py_INCREF(&DBaseObject_Type);
rpm-build 95f51c
	Py_INCREF(DBaseObject_Type.tp_base);
rpm-build 95f51c
	PyModule_AddObject(m, "DBaseObject", (PyObject *)&DBaseObject_Type);
rpm-build 95f51c
rpm-build 95f51c
	return m;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#if PY_MAJOR_VERSION >= 3
rpm-build 95f51c
PyMODINIT_FUNC PyInit__test_pytalloc(void);
rpm-build 95f51c
PyMODINIT_FUNC PyInit__test_pytalloc(void)
rpm-build 95f51c
{
rpm-build 95f51c
	return module_init();
rpm-build 95f51c
}
rpm-build 95f51c
#else
rpm-build 95f51c
void init_test_pytalloc(void);
rpm-build 95f51c
void init_test_pytalloc(void)
rpm-build 95f51c
{
rpm-build 95f51c
	module_init();
rpm-build 95f51c
}
rpm-build 95f51c
#endif