python/rpc: Add custom GUID.__str__, GUID.__repr__, GUID.__init__ and GUID.__cmp__.
authorAndrew Bartlett <abartlet@samba.org>
Tue, 21 Apr 2009 09:14:11 +0000 (11:14 +0200)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 21 Apr 2009 09:14:11 +0000 (11:14 +0200)
librpc/idl/misc.idl
source4/librpc/ndr/py_misc.c [new file with mode: 0644]
source4/scripting/python/samba/tests/dcerpc/misc.py [new file with mode: 0644]
source4/selftest/tests.sh

index c4d8c62ca391c7f79868be392891493c3d3fb64a..5bc3c9f976c886146caa6136ebb0d38077cbc2e2 100644 (file)
@@ -2,7 +2,9 @@
   miscellaneous IDL structures
 */
 
+
 [
+       pyhelper("librpc/ndr/py_misc.c"),
        pointer_default(unique)
 ]
 interface misc
diff --git a/source4/librpc/ndr/py_misc.c b/source4/librpc/ndr/py_misc.c
new file mode 100644 (file)
index 0000000..4e25566
--- /dev/null
@@ -0,0 +1,92 @@
+/* 
+   Unix SMB/CIFS implementation.
+   Samba utility functions
+   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include <Python.h>
+#include "librpc/gen_ndr/misc.h"
+
+#ifndef Py_RETURN_NONE
+#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
+#endif
+
+static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
+{
+       int ret;
+       struct GUID *self = py_talloc_get_ptr(py_self), *other;
+       other = py_talloc_get_ptr(py_other);
+       if (other == NULL)
+               return -1;
+
+       ret = GUID_compare(self, other);
+       if (ret < 0) {
+               return -1;
+       } else if (ret > 0) {
+               return 1;
+       } else {
+               return 0;
+       }
+}
+
+static PyObject *py_GUID_str(PyObject *py_self)
+{
+       struct GUID *self = py_talloc_get_ptr(py_self);
+       char *str = GUID_string(NULL, self);
+       PyObject *ret = PyString_FromString(str);
+       talloc_free(str);
+       return ret;
+}
+
+static PyObject *py_GUID_repr(PyObject *py_self)
+{
+       struct GUID *self = py_talloc_get_ptr(py_self);
+       char *str = GUID_string(NULL, self);
+       PyObject *ret = PyString_FromFormat("GUID('%s')", str);
+       talloc_free(str);
+       return ret;
+}
+
+static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+       char *str = NULL;
+       NTSTATUS status;
+       struct GUID *guid = py_talloc_get_ptr(self);
+       const char *kwnames[] = { "str", NULL };
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
+               return -1;
+
+       if (str != NULL) {
+               status = GUID_from_string(str, guid);
+               if (!NT_STATUS_IS_OK(status)) {
+                       PyErr_SetNTSTATUS(status);
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+static void py_GUID_patch(PyTypeObject *type)
+{
+       type->tp_init = py_GUID_init;
+       type->tp_str = py_GUID_str;
+       type->tp_repr = py_GUID_repr;
+       type->tp_compare = py_GUID_cmp;
+}
+
+#define PY_GUID_PATCH py_GUID_patch
+
diff --git a/source4/scripting/python/samba/tests/dcerpc/misc.py b/source4/scripting/python/samba/tests/dcerpc/misc.py
new file mode 100644 (file)
index 0000000..d9a5573
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/python
+
+# Unix SMB/CIFS implementation.
+# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
+#   
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#   
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#   
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import unittest
+from samba.dcerpc import misc
+
+text1 = "76f53846-a7c2-476a-ae2c-20e2b80d7b34"
+text2 = "344edffa-330a-4b39-b96e-2c34da52e8b1"
+
+class GUIDTests(unittest.TestCase):
+    def test_str(self):
+        guid = misc.GUID(text1)
+        self.assertEquals(text1, str(guid))
+
+    def test_repr(self):
+        guid = misc.GUID(text1)
+        self.assertEquals("GUID('%s')" % text1, repr(guid))
+
+    def test_compare_different(self):
+        guid1 = misc.GUID(text1)
+        guid2 = misc.GUID(text2)
+        self.assertTrue(cmp(guid1, guid2) > 0)
+
+    def test_compare_same(self):
+        guid1 = misc.GUID(text1)
+        guid2 = misc.GUID(text1)
+        self.assertEquals(0, cmp(guid1, guid2))
+        self.assertEquals(guid1, guid2)
+
+
+         
+        
index d18fffae5c7938f417c41e9def49a23ee1de6017..97aae8470d70992e5d799ceb6b730a78f1bb957b 100755 (executable)
@@ -406,6 +406,7 @@ plantest "registry.python" none PYTHONPATH="$PYTHONPATH:$samba4srcdir/lib/regist
 plantest "tdb.python" none PYTHONPATH="$PYTHONPATH:../lib/tdb/python/tests" $SUBUNITRUN simple
 plantest "auth.python" none PYTHONPATH="$PYTHONPATH:$samba4srcdir/auth/tests/" $SUBUNITRUN bindings
 plantest "security.python" none PYTHONPATH="$PYTHONPATH:$samba4srcdir/libcli/security/tests" $SUBUNITRUN bindings
+plantest "misc.python" none $SUBUNITRUN samba.tests.dcerpc.misc
 plantest "param.python" none PYTHONPATH="$PYTHONPATH:$samba4srcdir/param/tests" $SUBUNITRUN bindings
 plantest "upgrade.python" none $SUBUNITRUN samba.tests.upgrade
 plantest "samba.python" none $SUBUNITRUN samba.tests