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