summaryrefslogtreecommitdiff
path: root/source4/libcli/pysmb.c
diff options
context:
space:
mode:
authorNoel Power <noel.power@suse.com>2017-12-14 11:32:23 +0000
committerDouglas Bagnall <dbagnall@samba.org>2018-04-13 07:27:11 +0200
commit5c7e7cad4bf82ad08c5f11616d15f80842a930e1 (patch)
tree0aa7c256bd7d027174d430dfaf1e6106b1615a7f /source4/libcli/pysmb.c
parent23d841036d3ba8f2c80bee4edc89049533524e68 (diff)
downloadsamba-5c7e7cad4bf82ad08c5f11616d15f80842a930e1.tar.gz
s4/libcli: python3 port for smb module
Signed-off-by: Noel Power <noel.power@suse.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Diffstat (limited to 'source4/libcli/pysmb.c')
-rw-r--r--source4/libcli/pysmb.c45
1 files changed, 30 insertions, 15 deletions
diff --git a/source4/libcli/pysmb.c b/source4/libcli/pysmb.c
index 10ab54a9338..a370ab2a98e 100644
--- a/source4/libcli/pysmb.c
+++ b/source4/libcli/pysmb.c
@@ -18,6 +18,7 @@
*/
#include <Python.h>
+#include "python/py3compat.h"
#include <tevent.h>
#include <pytalloc.h>
#include "includes.h"
@@ -115,7 +116,7 @@ static PyObject * py_smb_loadfile(PyObject *self, PyObject *args)
status = smb_composite_loadfile(spdata->tree, pytalloc_get_mem_ctx(self), &io);
PyErr_NTSTATUS_IS_ERR_RAISE(status);
- return Py_BuildValue("s#", io.out.data, io.out.size);
+ return Py_BuildValue(PYARG_BYTES_LEN, io.out.data, io.out.size);
}
/*
@@ -125,17 +126,18 @@ static PyObject * py_smb_savefile(PyObject *self, PyObject *args)
{
struct smb_composite_savefile io;
const char *filename;
- char *data;
+ char *data = NULL;
+ Py_ssize_t size = 0;
NTSTATUS status;
struct smb_private_data *spdata;
- if (!PyArg_ParseTuple(args, "ss:savefile", &filename, &data)) {
+ if (!PyArg_ParseTuple(args, "s"PYARG_BYTES_LEN":savefile", &filename, &data, &size )) {
return NULL;
}
io.in.fname = filename;
io.in.data = (unsigned char *)data;
- io.in.size = strlen(data);
+ io.in.size = size;
spdata = pytalloc_get_ptr(self);
status = smb_composite_savefile(spdata->tree, &io);
@@ -157,11 +159,11 @@ static void py_smb_list_callback(struct clilist_file_info *f, const char *mask,
dict = PyDict_New();
if(dict) {
- PyDict_SetItemString(dict, "name", PyString_FromString(f->name));
+ PyDict_SetItemString(dict, "name", PyStr_FromString(f->name));
/* Windows does not always return short_name */
if (f->short_name) {
- PyDict_SetItemString(dict, "short_name", PyString_FromString(f->short_name));
+ PyDict_SetItemString(dict, "short_name", PyStr_FromString(f->short_name));
} else {
PyDict_SetItemString(dict, "short_name", Py_None);
}
@@ -524,11 +526,13 @@ static PyObject *py_close_file(PyObject *self, PyObject *args, PyObject *kwargs)
static PyMethodDef py_smb_methods[] = {
{ "loadfile", py_smb_loadfile, METH_VARARGS,
- "loadfile(path) -> file contents as a string\n\n \
- Read contents of a file." },
+ "loadfile(path) -> file contents as a "
+ PY_DESC_PY3_BYTES
+ "\n\n Read contents of a file." },
{ "savefile", py_smb_savefile, METH_VARARGS,
- "savefile(path, str) -> None\n\n \
- Write string str to file." },
+ "savefile(path, str) -> None\n\n Write "
+ PY_DESC_PY3_BYTES
+ " str to file." },
{ "list", (PyCFunction)py_smb_list, METH_VARARGS|METH_KEYWORDS,
"list(path) -> directory contents as a dictionary\n\n \
List contents of a directory. The keys are, \n \
@@ -651,17 +655,27 @@ static PyTypeObject PySMB = {
};
-void initsmb(void)
+static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "smb",
+ .m_doc = "SMB File I/O support",
+ .m_size = -1,
+ .m_methods = NULL,
+};
+
+void initsmb(void);
+
+MODULE_INIT_FUNC(smb)
{
- PyObject *m;
+ PyObject *m = NULL;
if (pytalloc_BaseObject_PyType_Ready(&PySMB) < 0) {
- return;
+ return m;
}
- m = Py_InitModule3("smb", NULL, "SMB File I/O support");
+ m = PyModule_Create(&moduledef);
if (m == NULL) {
- return;
+ return m;
}
Py_INCREF(&PySMB);
@@ -685,4 +699,5 @@ void initsmb(void)
ADD_FLAGS(FILE_ATTRIBUTE_NONINDEXED);
ADD_FLAGS(FILE_ATTRIBUTE_ENCRYPTED);
ADD_FLAGS(FILE_ATTRIBUTE_ALL_MASK);
+ return m;
}