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 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 source4/librpc/ndr/py_misc.c (limited to 'source4/librpc') 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 + -- cgit