From 1856b999c88e0e460fbf713b459d5d367db999f2 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 21 Apr 2009 11:14:11 +0200 Subject: python/rpc: Add custom GUID.__str__, GUID.__repr__, GUID.__init__ and GUID.__cmp__. --- source4/librpc/ndr/py_misc.c | 92 ++++++++++++++++++++++ .../scripting/python/samba/tests/dcerpc/misc.py | 48 +++++++++++ source4/selftest/tests.sh | 1 + 3 files changed, 141 insertions(+) create mode 100644 source4/librpc/ndr/py_misc.c create mode 100644 source4/scripting/python/samba/tests/dcerpc/misc.py (limited to 'source4') diff --git a/source4/librpc/ndr/py_misc.c b/source4/librpc/ndr/py_misc.c new file mode 100644 index 0000000000..4e25566148 --- /dev/null +++ b/source4/librpc/ndr/py_misc.c @@ -0,0 +1,92 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + Copyright (C) Jelmer Vernooij 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 . +*/ +#include +#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 index 0000000000..d9a5573980 --- /dev/null +++ b/source4/scripting/python/samba/tests/dcerpc/misc.py @@ -0,0 +1,48 @@ +#!/usr/bin/python + +# Unix SMB/CIFS implementation. +# Copyright (C) Jelmer Vernooij 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 . +# + +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) + + + + diff --git a/source4/selftest/tests.sh b/source4/selftest/tests.sh index d18fffae5c..97aae8470d 100755 --- a/source4/selftest/tests.sh +++ b/source4/selftest/tests.sh @@ -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 -- cgit