s4/ntvfs/posix/python: python3 (get|set)xattr value should be bytes
[metze/samba/wip.git] / source4 / ntvfs / posix / python / pyxattr_native.c
index 4f610a01f5ffa41f12558e3f0061c81835aa6ca7..b1fa2a208e5432ff52b3183a2a11893f6fe4c00c 100644 (file)
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
 #include "librpc/ndr/libndr.h"
 #include "system/filesys.h"
-
-void initxattr_native(void);
+#include "lib/util/base64.h"
 
 static PyObject *py_is_xattr_supported(PyObject *self)
 {
@@ -38,10 +38,10 @@ static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
 {
        char *filename, *attribute;
        int ret = 0;
-       int blobsize;
+       Py_ssize_t blobsize;
        DATA_BLOB blob;
 
-       if (!PyArg_ParseTuple(args, "sss#", &filename, &attribute, &blob.data, 
+       if (!PyArg_ParseTuple(args, "ss"PYARG_BYTES_LEN, &filename, &attribute, &blob.data,
         &blobsize))
                return NULL;
 
@@ -90,7 +90,7 @@ static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
                talloc_free(mem_ctx);
                return NULL;
        }
-       ret = PyString_FromStringAndSize(buf, len);
+       ret = Py_BuildValue(PYARG_BYTES_LEN, buf, len);
        talloc_free(mem_ctx);
        return ret;
 }
@@ -98,7 +98,7 @@ static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
 static PyMethodDef py_xattr_methods[] = {
        { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
                "wrap_getxattr(filename,attribute) -> blob\n"
-               "Retreive given attribute on the given file." },
+               "Retrieve given attribute on the given file." },
        { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
                "wrap_setxattr(filename,attribute,value)\n"
                "Set the given attribute to the given value on the given file." },
@@ -107,14 +107,22 @@ static PyMethodDef py_xattr_methods[] = {
        { NULL }
 };
 
-void initxattr_native(void)
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "xattr_native",
+    .m_doc = "Python bindings for xattr manipulation.",
+    .m_size = -1,
+    .m_methods = py_xattr_methods,
+};
+
+MODULE_INIT_FUNC(xattr_native)
 {
        PyObject *m;
 
-       m = Py_InitModule3("xattr_native", py_xattr_methods,
-                          "Python bindings for xattr manipulation.");
+       m = PyModule_Create(&moduledef);
 
        if (m == NULL)
-               return;
-}
+               return NULL;
 
+       return m;
+}