lib/adouble: README.Coding fix: multi-line if expression
[samba.git] / python / pyglue.c
index 04efa14d0513eaf1ed4be8de4e5ebd713c179c23..83b968bad8c80363cebe58bf59fc8c67b270cec2 100644 (file)
@@ -23,6 +23,7 @@
 #include "version.h"
 #include "param/pyparam.h"
 #include "lib/socket/netif.h"
+#include "lib/util/debug.h"
 
 void init_glue(void);
 static PyObject *PyExc_NTSTATUSError;
@@ -39,7 +40,7 @@ static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
                return NULL;
 
        retstr = generate_random_str(NULL, len);
-       ret = PyStr_FromString(retstr);
+       ret = PyUnicode_FromString(retstr);
        talloc_free(retstr);
        return ret;
 }
@@ -56,7 +57,7 @@ static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
        if (retstr == NULL) {
                return NULL;
        }
-       ret = PyStr_FromString(retstr);
+       ret = PyUnicode_FromString(retstr);
        talloc_free(retstr);
        return ret;
 }
@@ -149,7 +150,7 @@ static PyObject *py_nttime2string(PyObject *self, PyObject *args)
        }
 
        string = nt_time_string(tmp_ctx, nt);
-       ret =  PyStr_FromString(string);
+       ret =  PyUnicode_FromString(string);
 
        talloc_free(tmp_ctx);
 
@@ -161,16 +162,18 @@ static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
        unsigned level;
        if (!PyArg_ParseTuple(args, "I", &level))
                return NULL;
-       (DEBUGLEVEL) = level;
+       debuglevel_set(level);
        Py_RETURN_NONE;
 }
 
-static PyObject *py_get_debug_level(PyObject *self)
+static PyObject *py_get_debug_level(PyObject *self,
+               PyObject *Py_UNUSED(ignored))
 {
-       return PyInt_FromLong(DEBUGLEVEL);
+       return PyInt_FromLong(debuglevel_get());
 }
 
-static PyObject *py_fault_setup(PyObject *self)
+static PyObject *py_fault_setup(PyObject *self,
+               PyObject *Py_UNUSED(ignored))
 {
        static bool done;
        if (!done) {
@@ -180,7 +183,8 @@ static PyObject *py_fault_setup(PyObject *self)
        Py_RETURN_NONE;
 }
 
-static PyObject *py_is_ntvfs_fileserver_built(PyObject *self)
+static PyObject *py_is_ntvfs_fileserver_built(PyObject *self,
+               PyObject *Py_UNUSED(ignored))
 {
 #ifdef WITH_NTVFS_FILESERVER
        Py_RETURN_TRUE;
@@ -189,7 +193,8 @@ static PyObject *py_is_ntvfs_fileserver_built(PyObject *self)
 #endif
 }
 
-static PyObject *py_is_heimdal_built(PyObject *self)
+static PyObject *py_is_heimdal_built(PyObject *self,
+               PyObject *Py_UNUSED(ignored))
 {
 #ifdef SAMBA4_USES_HEIMDAL
        Py_RETURN_TRUE;
@@ -267,7 +272,7 @@ static PyObject *py_interface_ips(PyObject *self, PyObject *args)
                const char *ip = iface_list_n_ip(ifaces, i);
 
                if (all_interfaces) {
-                       PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
+                       PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
                        ifcount++;
                        continue;
                }
@@ -288,7 +293,7 @@ static PyObject *py_interface_ips(PyObject *self, PyObject *args)
                        continue;
                }
 
-               PyList_SetItem(pylist, ifcount, PyStr_FromString(ip));
+               PyList_SetItem(pylist, ifcount, PyUnicode_FromString(ip));
                ifcount++;
        }
        talloc_free(tmp_ctx);
@@ -299,28 +304,40 @@ static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
 {
        const char *s1 = NULL;
        const char *s2 = NULL;
-
-       if (!PyArg_ParseTuple(args, "eses", "utf8", &s1, "utf8", &s2)) {
+       long cmp_result = 0;
+       if (!PyArg_ParseTuple(args, PYARG_STR_UNI
+                             PYARG_STR_UNI,
+                             "utf8", &s1, "utf8", &s2)) {
                return NULL;
        }
 
-       return PyInt_FromLong(strcasecmp_m(s1, s2));
+       cmp_result = strcasecmp_m(s1, s2);
+       PyMem_Free(discard_const_p(char, s1));
+       PyMem_Free(discard_const_p(char, s2));
+       return PyInt_FromLong(cmp_result);
 }
 
 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
 {
        const char *s1 = NULL;
        const char *s2 = NULL;
-       char *ret = NULL;
-
-       if (!PyArg_ParseTuple(args, "eses", "utf8", &s1, "utf8", &s2))
+       char *strstr_ret = NULL;
+       PyObject *result = NULL;
+       if (!PyArg_ParseTuple(args, PYARG_STR_UNI
+                             PYARG_STR_UNI,
+                             "utf8", &s1, "utf8", &s2))
                return NULL;
 
-       ret = strstr_m(s1, s2);
-       if (!ret) {
+       strstr_ret = strstr_m(s1, s2);
+       if (!strstr_ret) {
+               PyMem_Free(discard_const_p(char, s1));
+               PyMem_Free(discard_const_p(char, s2));
                Py_RETURN_NONE;
        }
-       return PyUnicode_FromString(ret);
+       result = PyUnicode_FromString(strstr_ret);
+       PyMem_Free(discard_const_p(char, s1));
+       PyMem_Free(discard_const_p(char, s2));
+       return result;
 }
 
 static PyMethodDef py_misc_methods[] = {
@@ -394,7 +411,7 @@ MODULE_INIT_FUNC(_glue)
                return NULL;
 
        PyModule_AddObject(m, "version",
-                                          PyStr_FromString(SAMBA_VERSION_STRING));
+                                          PyUnicode_FromString(SAMBA_VERSION_STRING));
        PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
        if (PyExc_NTSTATUSError != NULL) {
                Py_INCREF(PyExc_NTSTATUSError);