2008-01-13 16:44:42 +01:00
/*
Unix SMB / CIFS implementation .
Python / Talloc glue
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2008
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program 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 General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2010-01-20 15:07:09 +13:00
# include <Python.h>
2009-01-30 19:38:09 +01:00
# include "replace.h"
2008-10-24 02:52:51 +02:00
# include <talloc.h>
2010-03-29 08:35:30 +11:00
# include "pytalloc.h"
2010-10-24 19:52:01 +02:00
# include <assert.h>
2008-01-13 16:44:42 +01:00
2008-12-23 04:06:21 +01:00
/**
* Simple dealloc for talloc - wrapping PyObjects
*/
2008-01-13 16:44:42 +01:00
void py_talloc_dealloc ( PyObject * self )
{
py_talloc_Object * obj = ( py_talloc_Object * ) self ;
2010-10-24 19:52:01 +02:00
assert ( talloc_unlink ( NULL , obj - > talloc_ctx ) ! = - 1 ) ;
2008-05-23 15:09:51 +02:00
obj - > talloc_ctx = NULL ;
2008-12-21 03:08:14 +01:00
self - > ob_type - > tp_free ( self ) ;
2008-01-13 16:44:42 +01:00
}
2008-12-23 04:06:21 +01:00
/**
* Import an existing talloc pointer into a Python object .
*/
2009-07-30 20:04:42 +02:00
PyObject * py_talloc_steal_ex ( PyTypeObject * py_type , TALLOC_CTX * mem_ctx ,
2008-01-13 20:41:34 +01:00
void * ptr )
2008-01-13 16:44:42 +01:00
{
2008-12-21 03:08:14 +01:00
py_talloc_Object * ret = ( py_talloc_Object * ) py_type - > tp_alloc ( py_type , 0 ) ;
2008-05-23 15:09:51 +02:00
ret - > talloc_ctx = talloc_new ( NULL ) ;
if ( ret - > talloc_ctx = = NULL ) {
return NULL ;
}
2009-07-01 14:05:17 +10:00
if ( talloc_steal ( ret - > talloc_ctx , mem_ctx ) = = NULL ) {
return NULL ;
}
2010-08-25 12:31:32 +10:00
talloc_set_name_const ( ret - > talloc_ctx , py_type - > tp_name ) ;
2009-07-01 14:05:17 +10:00
ret - > ptr = ptr ;
return ( PyObject * ) ret ;
}
2010-08-25 14:29:59 +10:00
/**
* Import an existing talloc pointer into a Python object .
*/
PyObject * py_talloc_steal ( PyTypeObject * py_type , void * ptr )
{
return py_talloc_steal_ex ( py_type , ptr , ptr ) ;
}
2009-07-01 14:05:17 +10:00
/**
* Import an existing talloc pointer into a Python object , leaving the
* original parent , and creating a reference to the object in the python
* object
*/
2009-07-30 20:04:42 +02:00
PyObject * py_talloc_reference_ex ( PyTypeObject * py_type , TALLOC_CTX * mem_ctx , void * ptr )
2009-07-01 14:05:17 +10:00
{
2010-08-28 21:53:27 +10:00
py_talloc_Object * ret ;
if ( ptr = = NULL ) {
Py_RETURN_NONE ;
}
ret = ( py_talloc_Object * ) py_type - > tp_alloc ( py_type , 0 ) ;
2009-07-01 14:05:17 +10:00
ret - > talloc_ctx = talloc_new ( NULL ) ;
if ( ret - > talloc_ctx = = NULL ) {
return NULL ;
}
2009-07-30 20:04:42 +02:00
if ( talloc_reference ( ret - > talloc_ctx , mem_ctx ) = = NULL ) {
2008-05-23 15:09:51 +02:00
return NULL ;
}
2010-08-25 12:31:32 +10:00
talloc_set_name_const ( ret - > talloc_ctx , py_type - > tp_name ) ;
2008-01-13 20:41:34 +01:00
ret - > ptr = ptr ;
2008-01-14 02:48:50 +01:00
return ( PyObject * ) ret ;
2008-01-13 16:44:42 +01:00
}
2008-01-13 18:38:12 +01:00
2008-12-23 04:06:21 +01:00
/**
2010-09-21 00:40:17 -07:00
* Default ( but only slightly more useful than the default ) implementation of Repr ( ) .
2008-12-23 04:06:21 +01:00
*/
PyObject * py_talloc_default_repr ( PyObject * obj )
2008-01-13 18:38:12 +01:00
{
2008-12-23 04:06:21 +01:00
py_talloc_Object * talloc_obj = ( py_talloc_Object * ) obj ;
PyTypeObject * type = ( PyTypeObject * ) PyObject_Type ( obj ) ;
2008-01-13 18:38:12 +01:00
2009-07-01 14:05:17 +10:00
return PyString_FromFormat ( " <%s talloc object at 0x%p> " ,
type - > tp_name , talloc_obj - > ptr ) ;
2008-01-13 18:38:12 +01:00
}
2009-09-28 15:03:17 +02:00
2010-09-21 00:40:17 -07:00
/**
* Default ( but only slightly more useful than the default ) implementation of cmp .
*/
int py_talloc_default_cmp ( PyObject * _obj1 , PyObject * _obj2 )
{
py_talloc_Object * obj1 = ( py_talloc_Object * ) _obj1 ,
* obj2 = ( py_talloc_Object * ) _obj2 ;
if ( obj1 - > ob_type ! = obj2 - > ob_type )
return ( obj1 - > ob_type - obj2 - > ob_type ) ;
2010-10-08 04:19:30 +03:00
return ( ( char * ) py_talloc_get_ptr ( obj1 ) - ( char * ) py_talloc_get_ptr ( obj2 ) ) ;
2010-09-21 00:40:17 -07:00
}
2009-09-28 15:03:17 +02:00
static void py_cobject_talloc_free ( void * ptr )
{
talloc_free ( ptr ) ;
}
PyObject * PyCObject_FromTallocPtr ( void * ptr )
{
2010-08-28 22:00:21 +10:00
if ( ptr = = NULL ) {
Py_RETURN_NONE ;
}
2009-09-28 15:03:17 +02:00
return PyCObject_FromVoidPtr ( ptr , py_cobject_talloc_free ) ;
}
2010-08-28 22:00:21 +10:00
PyObject * PyString_FromString_check_null ( const char * ptr )
{
if ( ptr = = NULL ) {
Py_RETURN_NONE ;
}
return PyString_FromString ( ptr ) ;
}