s4/registry/py: use unsigned ParseTuple format for unsigned value
[sfrench/samba-autobuild/.git] / source4 / lib / registry / pyregistry.c
index 8b75bbb60da18179b078d4596f8dfe87cc1f763a..78b47b8286d8e03fe69ed0489ce49a78b1ebd891 100644 (file)
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
 #include "libcli/util/pyerrors.h"
 #include "lib/registry/registry.h"
-#include "lib/talloc/pytalloc.h"
+#include <pytalloc.h>
 #include "lib/events/events.h"
 #include "auth/credentials/pycredentials.h"
 #include "param/pyparam.h"
@@ -31,9 +32,9 @@ extern PyTypeObject PyRegistryKey;
 extern PyTypeObject PyRegistry;
 extern PyTypeObject PyHiveKey;
 
-/*#define PyRegistryKey_AsRegistryKey(obj) py_talloc_get_type(obj, struct registry_key)*/
-#define PyRegistry_AsRegistryContext(obj) ((struct registry_context *)py_talloc_get_ptr(obj))
-#define PyHiveKey_AsHiveKey(obj) ((struct hive_key*)py_talloc_get_ptr(obj))
+/*#define PyRegistryKey_AsRegistryKey(obj) pytalloc_get_type(obj, struct registry_key)*/
+#define PyRegistry_AsRegistryContext(obj) ((struct registry_context *)pytalloc_get_ptr(obj))
+#define PyHiveKey_AsHiveKey(obj) ((struct hive_key*)pytalloc_get_ptr(obj))
 
 
 static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
@@ -47,9 +48,9 @@ static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
                return NULL;
 
        result = reg_get_predefined_key_by_name(ctx, name, &key);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
-       return py_talloc_steal(&PyRegistryKey, key);
+       return pytalloc_steal(&PyRegistryKey, key);
 }
 
 static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
@@ -62,7 +63,7 @@ static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
                return NULL;
 
        result = reg_key_del_abs(ctx, path);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
        Py_RETURN_NONE;
 }
@@ -78,9 +79,9 @@ static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
                return NULL;
 
        result = reg_get_predefined_key(ctx, hkey, &key);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
-       return py_talloc_steal(&PyRegistryKey, key);
+       return pytalloc_steal(&PyRegistryKey, key);
 }
 
 static PyObject *py_diff_apply(PyObject *self, PyObject *args)
@@ -92,7 +93,7 @@ static PyObject *py_diff_apply(PyObject *self, PyObject *args)
                return NULL;
 
        result = reg_diff_apply(ctx, filename);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
        Py_RETURN_NONE; 
 }
@@ -119,13 +120,13 @@ static PyObject *py_mount_hive(PyObject *self, PyObject *args)
                int i;
                elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
                for (i = 0; i < PyList_Size(py_elements); i++)
-                       elements[i] = PyString_AsString(PyList_GetItem(py_elements, i));
+                       elements[i] = PyStr_AsString(PyList_GetItem(py_elements, i));
        }
 
        SMB_ASSERT(ctx != NULL);
 
        result = reg_mount_hive(ctx, PyHiveKey_AsHiveKey(py_hivekey), hkey, elements);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
        Py_RETURN_NONE;
 }
@@ -135,8 +136,8 @@ static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwar
        WERROR result;
        struct registry_context *ctx;
        result = reg_open_local(NULL, &ctx);
-       PyErr_WERROR_IS_ERR_RAISE(result);
-       return py_talloc_steal(&PyRegistry, ctx);
+       PyErr_WERROR_NOT_OK_RAISE(result);
+       return pytalloc_steal(&PyRegistry, ctx);
 }
 
 static PyMethodDef registry_methods[] = {
@@ -158,8 +159,6 @@ PyTypeObject PyRegistry = {
        .tp_name = "Registry",
        .tp_methods = registry_methods,
        .tp_new = registry_new,
-       .tp_basicsize = sizeof(py_talloc_Object),
-       .tp_dealloc = py_talloc_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
@@ -174,7 +173,7 @@ static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
 
        result = hive_key_del(NULL, key, name);
 
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
        Py_RETURN_NONE; 
 }
@@ -185,7 +184,7 @@ static PyObject *py_hive_key_flush(PyObject *self)
        struct hive_key *key = PyHiveKey_AsHiveKey(self);
 
        result = hive_key_flush(key);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
        Py_RETURN_NONE;
 }
@@ -201,7 +200,7 @@ static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
 
        result = hive_key_del_value(NULL, key, name);
 
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
        Py_RETURN_NONE; 
 }
@@ -211,18 +210,21 @@ static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
        char *name;
        uint32_t type;
        DATA_BLOB value;
+       Py_ssize_t value_length = 0;
        WERROR result;
        struct hive_key *key = PyHiveKey_AsHiveKey(self);
 
-       if (!PyArg_ParseTuple(args, "siz#", &name, &type, &value.data, &value.length))
+       if (!PyArg_ParseTuple(args, "sIz#", &name, &type, &value.data, &value_length)) {
                return NULL;
+       }
+       value.length = value_length;
 
        if (value.data != NULL)
                result = hive_key_set_value(key, name, type, value);
        else
                result = hive_key_del_value(NULL, key, name);
 
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
        Py_RETURN_NONE; 
 }
@@ -248,7 +250,9 @@ static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwar
        const char *kwnames[] = { "location", "lp_ctx", "session_info", "credentials", NULL };
        WERROR result;
        struct loadparm_context *lp_ctx;
-       PyObject *py_lp_ctx, *py_session_info, *py_credentials;
+       PyObject *py_lp_ctx = Py_None;
+       PyObject *py_session_info = Py_None;
+       PyObject *py_credentials = Py_None;
        struct auth_session_info *session_info;
        struct cli_credentials *credentials;
        char *location;
@@ -284,27 +288,23 @@ static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwar
        session_info = NULL;
 
        result = reg_open_hive(NULL, location, session_info, credentials,
-                              tevent_context_init(NULL),
+                              samba_tevent_context_init(NULL),
                               lp_ctx, &hive_key);
        talloc_free(mem_ctx);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
-       return py_talloc_steal(&PyHiveKey, hive_key);
+       return pytalloc_steal(&PyHiveKey, hive_key);
 }
 
 PyTypeObject PyHiveKey = {
        .tp_name = "HiveKey",
        .tp_methods = hive_key_methods,
        .tp_new = hive_new,
-       .tp_basicsize = sizeof(py_talloc_Object),
-       .tp_dealloc = py_talloc_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
 PyTypeObject PyRegistryKey = {
        .tp_name = "RegistryKey",
-       .tp_basicsize = sizeof(py_talloc_Object),
-       .tp_dealloc = py_talloc_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
@@ -314,7 +314,9 @@ static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
        struct registry_context *reg_ctx;
        WERROR result;
        struct loadparm_context *lp_ctx;
-       PyObject *py_lp_ctx, *py_session_info, *py_credentials;
+       PyObject *py_lp_ctx = Py_None;
+       PyObject *py_session_info = Py_None;
+       PyObject *py_credentials = Py_None;
        struct auth_session_info *session_info;
        struct cli_credentials *credentials;
        TALLOC_CTX *mem_ctx;
@@ -354,38 +356,8 @@ static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
                PyErr_SetWERROR(result);
                return NULL;
        }
-       
-       return py_talloc_steal(&PyRegistry, reg_ctx);
-}
-
-static PyObject *py_open_directory(PyObject *self, PyObject *args)
-{
-       char *location;
-       WERROR result;
-       struct hive_key *key;
-
-       if (!PyArg_ParseTuple(args, "s", &location))
-               return NULL;
-
-       result = reg_open_directory(NULL, location, &key);
-       PyErr_WERROR_IS_ERR_RAISE(result);
-
-       return py_talloc_steal(&PyHiveKey, key);
-}
-
-static PyObject *py_create_directory(PyObject *self, PyObject *args)
-{
-       char *location;
-       WERROR result;
-       struct hive_key *key;
-
-       if (!PyArg_ParseTuple(args, "s", &location))
-               return NULL;
-
-       result = reg_create_directory(NULL, location, &key);
-       PyErr_WERROR_IS_ERR_RAISE(result);
 
-       return py_talloc_steal(&PyHiveKey, key);
+       return pytalloc_steal(&PyRegistry, reg_ctx);
 }
 
 static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
@@ -431,9 +403,9 @@ static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwar
        result = reg_open_ldb_file(NULL, location, session_info, credentials,
                                   s4_event_context_init(NULL), lp_ctx, &key);
        talloc_free(mem_ctx);
-       PyErr_WERROR_IS_ERR_RAISE(result);
+       PyErr_WERROR_NOT_OK_RAISE(result);
 
-       return py_talloc_steal(&PyHiveKey, key);
+       return pytalloc_steal(&PyHiveKey, key);
 }
 
 static PyObject *py_str_regtype(PyObject *self, PyObject *args)
@@ -443,7 +415,7 @@ static PyObject *py_str_regtype(PyObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "i", &regtype))
                return NULL;
        
-       return PyString_FromString(str_regtype(regtype));
+       return PyStr_FromString(str_regtype(regtype));
 }
 
 static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
@@ -457,13 +429,11 @@ static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
        str = reg_get_predef_name(hkey);
        if (str == NULL)
                Py_RETURN_NONE;
-       return PyString_FromString(str);
+       return PyStr_FromString(str);
 }
 
 static PyMethodDef py_registry_methods[] = {
        { "open_samba", (PyCFunction)py_open_samba, METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
-       { "open_directory", py_open_directory, METH_VARARGS, "open_dir(location) -> key" },
-       { "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
        { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
        { "open_hive", (PyCFunction)py_open_hive, METH_VARARGS|METH_KEYWORDS, "open_hive(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
        { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
@@ -471,22 +441,30 @@ static PyMethodDef py_registry_methods[] = {
        { NULL }
 };
 
-void initregistry(void)
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "registry",
+    .m_doc = "Registry",
+    .m_size = -1,
+    .m_methods = py_registry_methods,
+};
+
+MODULE_INIT_FUNC(registry)
 {
        PyObject *m;
 
-       if (PyType_Ready(&PyHiveKey) < 0)
-               return;
+       if (pytalloc_BaseObject_PyType_Ready(&PyHiveKey) < 0)
+               return NULL;
 
-       if (PyType_Ready(&PyRegistry) < 0)
-               return;
+       if (pytalloc_BaseObject_PyType_Ready(&PyRegistry) < 0)
+               return NULL;
 
-       if (PyType_Ready(&PyRegistryKey) < 0)
-               return;
+       if (pytalloc_BaseObject_PyType_Ready(&PyRegistryKey) < 0)
+               return NULL;
 
-       m = Py_InitModule3("registry", py_registry_methods, "Registry");
+       m = PyModule_Create(&moduledef);
        if (m == NULL)
-               return;
+               return NULL;
 
        PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyInt_FromLong(HKEY_CLASSES_ROOT));
        PyModule_AddObject(m, "HKEY_CURRENT_USER", PyInt_FromLong(HKEY_CURRENT_USER));
@@ -506,4 +484,6 @@ void initregistry(void)
 
        Py_INCREF(&PyRegistryKey);
        PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
+
+       return m;
 }