From 864218b6beebaeb337f14398f0544340ad30dd58 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 14 Dec 2007 01:26:25 +0100 Subject: r26448: Add basic tests for param python module. (This used to be commit ccfab20dcc2d7059c402c03be244b759d59c4b81) --- source4/param/param.i | 23 +++++++++++--- source4/param/param.py | 13 +++++++- source4/param/param_wrap.c | 67 ----------------------------------------- source4/param/tests/bindings.py | 41 +++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 73 deletions(-) create mode 100644 source4/param/tests/bindings.py (limited to 'source4/param') diff --git a/source4/param/param.i b/source4/param/param.i index aadca9b1fb1..ca1bb691842 100644 --- a/source4/param/param.i +++ b/source4/param/param.i @@ -40,12 +40,11 @@ typedef struct param_section param_section; $1 = loadparm_init(NULL); } -%typemap(freearg) struct loadparm_context * { - talloc_free($1); -} - %rename(LoadParm) loadparm_context; +%nodefaultctor loadparm_context; +%nodefaultdtor loadparm_context; + typedef struct loadparm_context { %extend { loadparm_context(TALLOC_CTX *mem_ctx) { return loadparm_init(mem_ctx); } @@ -85,13 +84,27 @@ typedef struct param_context { int use(struct param_context *); int write(const char *fn); } + %pythoncode { + def __getitem__(self, name): + ret = self.get_section(name) + if ret is None: + raise KeyError("No such section %s" % name) + return ret + } } param; typedef struct param_section { %extend { struct param_opt *get(const char *name); } + %pythoncode { + def __getitem__(self, name): + ret = self.get_section(name) + if ret is None: + raise KeyError("No such section %s" % name) + return ret + } } param_section; %rename(default_config) global_loadparm; -struct loadparm_context *global_loadparm; +//struct loadparm_context *global_loadparm ; diff --git a/source4/param/param.py b/source4/param/param.py index 6b90581c688..8ac009af967 100644 --- a/source4/param/param.py +++ b/source4/param/param.py @@ -88,6 +88,12 @@ class ParamFile(object): def __init__(self, *args, **kwargs): _param.ParamFile_swiginit(self,_param.new_ParamFile(*args, **kwargs)) __swig_destroy__ = _param.delete_ParamFile + def __getitem__(self, name): + ret = self.get_section(name) + if ret is None: + raise KeyError("No such section %s" % name) + return ret + ParamFile.get_section = new_instancemethod(_param.ParamFile_get_section,None,ParamFile) ParamFile.get = new_instancemethod(_param.ParamFile_get,None,ParamFile) ParamFile.set_string = new_instancemethod(_param.ParamFile_set_string,None,ParamFile) @@ -100,6 +106,12 @@ ParamFile_swigregister(ParamFile) class param_section(object): thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag') __repr__ = _swig_repr + def __getitem__(self, name): + ret = self.get_section(name) + if ret is None: + raise KeyError("No such section %s" % name) + return ret + def __init__(self, *args, **kwargs): _param.param_section_swiginit(self,_param.new_param_section(*args, **kwargs)) __swig_destroy__ = _param.delete_param_section @@ -108,5 +120,4 @@ param_section_swigregister = _param.param_section_swigregister param_section_swigregister(param_section) -cvar = _param.cvar diff --git a/source4/param/param_wrap.c b/source4/param/param_wrap.c index 9f65ba6f886..704759ad191 100644 --- a/source4/param/param_wrap.c +++ b/source4/param/param_wrap.c @@ -2689,14 +2689,8 @@ SWIGINTERN PyObject *_wrap_delete_LoadParm(PyObject *SWIGUNUSEDPARM(self), PyObj delete_loadparm_context(arg1); resultobj = SWIG_Py_Void(); - { - talloc_free(arg1); - } return resultobj; fail: - { - talloc_free(arg1); - } return NULL; } @@ -2737,15 +2731,9 @@ SWIGINTERN PyObject *_wrap_LoadParm_load(PyObject *SWIGUNUSEDPARM(self), PyObjec } result = (bool)loadparm_context_load(arg1,(char const *)arg2); resultobj = SWIG_From_bool((bool)(result)); - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } @@ -2775,14 +2763,8 @@ SWIGINTERN PyObject *_wrap_LoadParm___len__(PyObject *SWIGUNUSEDPARM(self), PyOb } result = (int)loadparm_context___len__(arg1); resultobj = SWIG_From_int((int)(result)); - { - talloc_free(arg1); - } return resultobj; fail: - { - talloc_free(arg1); - } return NULL; } @@ -2823,15 +2805,9 @@ SWIGINTERN PyObject *_wrap_LoadParm___getitem__(PyObject *SWIGUNUSEDPARM(self), } result = (struct loadparm_service *)loadparm_context___getitem__(arg1,(char const *)arg2); resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_loadparm_service, 0 | 0 ); - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } @@ -2861,14 +2837,8 @@ SWIGINTERN PyObject *_wrap_LoadParm_configfile(PyObject *SWIGUNUSEDPARM(self), P } result = (char *)loadparm_context_configfile(arg1); resultobj = SWIG_FromCharPtr((const char *)result); - { - talloc_free(arg1); - } return resultobj; fail: - { - talloc_free(arg1); - } return NULL; } @@ -2909,15 +2879,9 @@ SWIGINTERN PyObject *_wrap_LoadParm_is_mydomain(PyObject *SWIGUNUSEDPARM(self), } result = (bool)loadparm_context_is_mydomain(arg1,(char const *)arg2); resultobj = SWIG_From_bool((bool)(result)); - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } @@ -2959,15 +2923,9 @@ SWIGINTERN PyObject *_wrap_LoadParm_is_myname(PyObject *SWIGUNUSEDPARM(self), Py } result = (bool)loadparm_context_is_myname(arg1,(char const *)arg2); resultobj = SWIG_From_bool((bool)(result)); - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: - { - talloc_free(arg1); - } if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } @@ -3450,29 +3408,6 @@ SWIGINTERN PyObject *param_section_swiginit(PyObject *SWIGUNUSEDPARM(self), PyOb return SWIG_Python_InitShadowInstance(args); } -SWIGINTERN int Swig_var_default_config_set(PyObject *_val) { - { - void *argp = 0; - int res = SWIG_ConvertPtr(_val, &argp, SWIGTYPE_p_loadparm_context, 0 ); - if (!SWIG_IsOK(res)) { - SWIG_exception_fail(SWIG_ArgError(res), "in variable '""global_loadparm""' of type '""struct loadparm_context *""'"); - } - global_loadparm = (struct loadparm_context *)(argp); - } - return 0; -fail: - return 1; -} - - -SWIGINTERN PyObject *Swig_var_default_config_get(void) { - PyObject *pyobj = 0; - - pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(global_loadparm), SWIGTYPE_p_loadparm_context, 0 ); - return pyobj; -} - - static PyMethodDef SwigMethods[] = { { (char *)"new_LoadParm", (PyCFunction)_wrap_new_LoadParm, METH_NOARGS, NULL}, { (char *)"delete_LoadParm", (PyCFunction) _wrap_delete_LoadParm, METH_VARARGS | METH_KEYWORDS, NULL}, @@ -4095,7 +4030,5 @@ SWIGEXPORT void SWIG_init(void) { SWIG_InstallConstants(d,swig_const_table); - PyDict_SetItemString(d,(char*)"cvar", SWIG_globals()); - SWIG_addvarlink(SWIG_globals(),(char*)"default_config",Swig_var_default_config_get, Swig_var_default_config_set); } diff --git a/source4/param/tests/bindings.py b/source4/param/tests/bindings.py new file mode 100644 index 00000000000..ea542e21878 --- /dev/null +++ b/source4/param/tests/bindings.py @@ -0,0 +1,41 @@ +#!/usr/bin/python + +# Unix SMB/CIFS implementation. +# Copyright (C) Jelmer Vernooij 2007 +# +# 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 . +# + +import param +import unittest + +class LoadParmTestCase(unittest.TestCase): + def test_init(self): + file = param.LoadParm() + self.assertTrue(file is not None) + + def test_lenght(self): + file = param.LoadParm() + self.assertEquals(0, len(file)) + + +class ParamTestCase(unittest.TestCase): + def test_init(self): + file = param.ParamFile() + self.assertTrue(file is not None) + + def test_get_section(self): + file = param.ParamFile() + self.assertEquals(None, file.get_section("unknown")) + self.assertRaises(KeyError, lambda: file["unknown"]) -- cgit