Some more questions about adding my SMB Object stuff

Richard Sharpe realrichardsharpe at gmail.com
Sat Apr 7 12:38:30 MDT 2012


Hi folks,

In looking at implementing an SMBFile object, as suggested by a couple
of people, I have some further questions.

Here is what I currently have:

diff --git a/source4/libcli/pysmb.c b/source4/libcli/pysmb.c
index 3f2efe9..74b15bc 100644
--- a/source4/libcli/pysmb.c
+++ b/source4/libcli/pysmb.c
@@ -53,6 +53,11 @@ struct smb_private_data {
        struct smbcli_tree *tree;
 };

+struct smb_file_private_data {
+       struct smb_private_data *spdata;
+       int fnum;
+};
+
 static void dos_format(char *s)
 {
        string_replace(s, '/', '\\');
@@ -459,6 +464,10 @@ static PyObject *py_open_file(pytalloc_Object *self, PyObje
        spdata = self->ptr;

        mem_ctx = talloc_new(NULL);
+       if (!mem_ctx) {
+               PyErr_NoMemory();
+               return NULL;
+       }

        io.generic.level = RAW_OPEN_NTCREATEX;
        io.ntcreatex.in.root_fid.fnum = 0;
@@ -537,7 +546,7 @@ static PyMethodDef py_smb_methods[] = {
                Set security descriptor for file." },
        { "open_file", (PyCFunction)py_open_file, METH_VARARGS,
                "open_file(path, access_mask[, share_access[, open_disposition[,
-               Open a file. Throws NTSTATUS exceptions on errors." },
+               Open a file. Throws RuntimeError exceptions for things like NT_S
        { "close_file", (PyCFunction)py_close_file, METH_VARARGS,
                "close_file(fnum) -> None\n\n \
                Close the file based on fnum."},
@@ -613,6 +622,24 @@ static PyTypeObject PySMB = {

 };

+/*
+ * SMBFile object
+ */
+static PyMethodDef py_smbfile_methods[] = {
+       { "close", (PyCFunction)py_smbfile_close, METH_NOARGS,
+               "close() -> file contents as a string\n\n \
+               close and destroy an smbfile object." },
+       { NULL },
+};
+
+PyTypeObject PySMBFile = {
+       .tp_name = "SMBFile",
+       .tp_basicsize = sizeof(pytalloc_Object),
+       .tp_flags = Py_TPFLAGS_DEFAULT,
+       .tp_methods = py_smbfile_methods,
+       .tp_doc = "SMBFile(unc[, creds[, lp]]) -> SMBFile object\n",
+};
+
 void initsmb(void)
 {
        PyObject *m;
@@ -653,4 +680,18 @@ void initsmb(void)
        ADD_FLAGS(FILE_ATTRIBUTE_NONINDEXED);
        ADD_FLAGS(FILE_ATTRIBUTE_ENCRYPTED);
        ADD_FLAGS(FILE_ATTRIBUTE_ALL_MASK);
+
+       /* Init the other type we are creating */
+       PySMBFile.tp_base = talloc_type;
+
+       if (PyType_Ready(&PySMBFile) < 0) {
+               return;
+       }
+
+       m = Py_InitModule3("smbfile", NULL, "SMB File Object support");
+       if (m == NULL) {
+               return;
+       }
+
+       Py_INCREF(&PySMBFile);
 }

So, my questions (ignore the fact that this does not compile currently
and there are some further fixes suggested by Jelmer):

1. I currently do not have a tp_new defined, because initially I
expect such an object to be created via an SMB object's open_file
(which is incorrectly named :-(). Will this be a problem?

2. I will need a close method which should Py_DECREF the SMBFile
object to allow it to be deallocated. Is that correct?

3. I probably should have a tp_new defined that takes either an SMB
object as a parameter, or a UNC string and constructs an anonymous SMB
object. Does that sound reasonable.

I would like to move more towards a correct Python object-oriented
approach, but I also want to make a start on some damn test cases
(since I have already started some in another Object Oriented
scripting language for other purposes.)
-- 
Regards,
Richard Sharpe
(何以解憂?唯有杜康。--曹操)


More information about the samba-technical mailing list