2009-04-23 01:20:20 +02:00
/*
Unix SMB / CIFS implementation .
Samba utility functions
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2007 - 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/>.
*/
2023-11-09 11:35:56 +01:00
# include "lib/replace/system/python.h"
2016-12-10 13:55:43 +01:00
# include "py3compat.h"
2009-04-23 01:20:20 +02:00
# include "includes.h"
# include "param/param.h"
2011-03-19 00:44:36 +01:00
# include "param/pyparam.h"
2009-04-23 01:20:20 +02:00
# include "param/loadparm.h"
2011-08-14 15:34:08 +02:00
# include <pytalloc.h>
2009-04-23 01:20:20 +02:00
2011-08-10 15:15:18 +02:00
# define PyLoadparmContext_AsLoadparmContext(obj) pytalloc_get_type(obj, struct loadparm_context)
2009-04-23 01:20:20 +02:00
2010-07-16 14:32:42 +10:00
_PUBLIC_ struct loadparm_context * lpcfg_from_py_object ( TALLOC_CTX * mem_ctx , PyObject * py_obj )
2009-04-23 01:20:20 +02:00
{
2010-11-29 13:25:59 +11:00
struct loadparm_context * lp_ctx ;
2010-09-22 15:35:36 -07:00
PyObject * param_mod ;
PyTypeObject * lp_type ;
bool is_lpobj ;
2009-04-23 01:20:20 +02:00
2019-06-15 23:14:49 +12:00
if ( PyUnicode_Check ( py_obj ) ) {
2010-11-29 13:25:59 +11:00
lp_ctx = loadparm_init_global ( false ) ;
2011-03-21 10:32:24 +01:00
if ( lp_ctx = = NULL ) {
return NULL ;
}
2019-06-07 11:16:25 +02:00
if ( ! lpcfg_load ( lp_ctx , PyUnicode_AsUTF8 ( py_obj ) ) ) {
2009-04-23 01:20:20 +02:00
PyErr_Format ( PyExc_RuntimeError , " Unable to load %s " ,
2019-06-07 11:16:25 +02:00
PyUnicode_AsUTF8 ( py_obj ) ) ;
2010-11-29 13:25:59 +11:00
return NULL ;
}
return lp_ctx ;
}
2009-04-23 01:20:20 +02:00
2010-11-29 13:25:59 +11:00
if ( py_obj = = Py_None ) {
return loadparm_init_global ( true ) ;
}
2009-04-23 01:20:20 +02:00
2010-09-22 15:35:36 -07:00
param_mod = PyImport_ImportModule ( " samba.param " ) ;
if ( param_mod = = NULL ) {
return NULL ;
}
lp_type = ( PyTypeObject * ) PyObject_GetAttrString ( param_mod , " LoadParm " ) ;
Py_DECREF ( param_mod ) ;
if ( lp_type = = NULL ) {
PyErr_SetString ( PyExc_RuntimeError , " Unable to import LoadParm " ) ;
return NULL ;
}
is_lpobj = PyObject_TypeCheck ( py_obj , lp_type ) ;
Py_DECREF ( lp_type ) ;
if ( is_lpobj ) {
return talloc_reference ( mem_ctx , PyLoadparmContext_AsLoadparmContext ( py_obj ) ) ;
}
PyErr_SetNone ( PyExc_TypeError ) ;
return NULL ;
2009-04-23 01:20:20 +02:00
}
2009-06-02 15:00:41 +02:00
struct loadparm_context * py_default_loadparm_context ( TALLOC_CTX * mem_ctx )
{
2010-11-29 13:25:59 +11:00
return loadparm_init_global ( true ) ;
2009-06-02 15:00:41 +02:00
}