along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdint.h>
-#include <stdbool.h>
-
+#include <Python.h>
#include "includes.h"
#include "param/param.h"
#include "param/loadparm.h"
-#include <Python.h>
-#include "pytalloc.h"
+#include "lib/talloc/pytalloc.h"
/* There's no Py_ssize_t in 2.4, apparently */
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
typedef inquiry lenfunc;
#endif
-#ifndef Py_RETURN_NONE
-#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
-#endif
-
#define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
PyAPI_DATA(PyTypeObject) PyLoadparmContext;
PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
{
- return py_talloc_import(&PyLoadparmService, service);
+ return py_talloc_reference(&PyLoadparmService, service);
}
static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
{
int j;
const char **strlist = *(const char ***)parm_ptr;
- PyObject *pylist = PyList_New(str_list_length(strlist));
+ PyObject *pylist;
+
+ if(strlist == NULL) {
+ return PyList_New(0);
+ }
+
+ pylist = PyList_New(str_list_length(strlist));
for (j = 0; strlist[j]; j++)
PyList_SetItem(pylist, j,
PyString_FromString(strlist[j]));
return ret;
}
+static PyObject *py_lp_ctx_services(py_talloc_Object *self)
+{
+ struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
+ PyObject *ret;
+ int i;
+ ret = PyList_New(lp_numservices(lp_ctx));
+ for (i = 0; i < lp_numservices(lp_ctx); i++) {
+ struct loadparm_service *service = lp_servicebynum(lp_ctx, i);
+ if (service != NULL) {
+ PyList_SetItem(ret, i, PyString_FromString(lp_servicename(service)));
+ }
+ }
+ return ret;
+}
+
static PyMethodDef py_lp_ctx_methods[] = {
{ "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS,
"S.load(filename) -> None\n"
"Change a parameter." },
{ "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
"S.private_path(name) -> path\n" },
+ { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
+ "S.services() -> list" },
{ NULL }
};
static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
- return py_talloc_import(type, loadparm_init(NULL));
+ py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
+ if (ret == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ ret->talloc_ctx = talloc_new(NULL);
+ if (ret->talloc_ctx == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ ret->ptr = loadparm_init(ret->talloc_ctx);
+ return (PyObject *)ret;
}
static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
.tp_flags = Py_TPFLAGS_DEFAULT,
};
-_PUBLIC_ struct loadparm_context *lp_from_py_object(PyObject *py_obj)
-{
- struct loadparm_context *lp_ctx;
- if (PyString_Check(py_obj)) {
- lp_ctx = loadparm_init(NULL);
- if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
- talloc_free(lp_ctx);
- PyErr_Format(PyExc_RuntimeError,
- "Unable to load %s", PyString_AsString(py_obj));
- return NULL;
- }
- return lp_ctx;
- }
-
- if (py_obj == Py_None) {
- lp_ctx = loadparm_init(NULL);
- /* We're not checking that loading the file succeeded *on purpose */
- lp_load_default(lp_ctx);
- return lp_ctx;
- }
-
- return PyLoadparmContext_AsLoadparmContext(py_obj);
-}
-
-struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx)
-{
- struct loadparm_context *ret;
- ret = loadparm_init(mem_ctx);
- if (!lp_load_default(ret))
- return NULL;
- return ret;
-}
-
static PyObject *py_default_path(PyObject *self)
{
return PyString_FromString(lp_default_path());
if (PyType_Ready(&PyLoadparmContext) < 0)
return;
+ if (PyType_Ready(&PyLoadparmService) < 0)
+ return;
+
m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
if (m == NULL)
return;