s3: VFS: Change SMB_VFS_CHFLAGS to use const struct smb_filename * instead of const...
[Samba.git] / source4 / librpc / ndr / py_misc.c
blob6316b9b6704d72a5bf728ca1ad4bf0736dcfe95a
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <Python.h>
21 #include "python/py3compat.h"
22 #include "librpc/gen_ndr/misc.h"
24 #if PY_MAJOR_VERSION >= 3
25 static PyObject *py_GUID_richcmp(PyObject *py_self, PyObject *py_other, int op)
27 int ret;
28 struct GUID *self = pytalloc_get_ptr(py_self), *other;
29 other = pytalloc_get_ptr(py_other);
30 if (other == NULL) {
31 Py_INCREF(Py_NotImplemented);
32 return Py_NotImplemented;
35 ret = GUID_compare(self, other);
37 switch (op) {
38 case Py_EQ: if (ret == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
39 case Py_NE: if (ret != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
40 case Py_LT: if (ret < 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
41 case Py_GT: if (ret > 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
42 case Py_LE: if (ret <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
43 case Py_GE: if (ret >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
45 Py_INCREF(Py_NotImplemented);
46 return Py_NotImplemented;
48 #else
49 static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
51 int ret;
52 struct GUID *self = pytalloc_get_ptr(py_self), *other;
53 other = pytalloc_get_ptr(py_other);
54 if (other == NULL)
55 return -1;
57 ret = GUID_compare(self, other);
58 if (ret < 0) {
59 return -1;
60 } else if (ret > 0) {
61 return 1;
62 } else {
63 return 0;
66 #endif
68 static PyObject *py_GUID_str(PyObject *py_self)
70 struct GUID *self = pytalloc_get_ptr(py_self);
71 char *str = GUID_string(NULL, self);
72 PyObject *ret = PyStr_FromString(str);
73 talloc_free(str);
74 return ret;
77 static PyObject *py_GUID_repr(PyObject *py_self)
79 struct GUID *self = pytalloc_get_ptr(py_self);
80 char *str = GUID_string(NULL, self);
81 PyObject *ret = PyStr_FromFormat("GUID('%s')", str);
82 talloc_free(str);
83 return ret;
86 static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
88 PyObject *str = NULL;
89 NTSTATUS status;
90 struct GUID *guid = pytalloc_get_ptr(self);
91 const char *kwnames[] = { "str", NULL };
93 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &str))
94 return -1;
96 if (str != NULL) {
97 DATA_BLOB guid_val;
98 Py_ssize_t _size;
100 if (!PyStr_Check(str)) {
101 PyErr_SetString(PyExc_TypeError, "Expected a string argument to GUID()");
102 return -1;
104 guid_val.data = (uint8_t *)PyStr_AsUTF8AndSize(str, &_size);
105 guid_val.length = _size;
106 status = GUID_from_data_blob(&guid_val, guid);
107 if (!NT_STATUS_IS_OK(status)) {
108 PyErr_SetNTSTATUS(status);
109 return -1;
113 return 0;
116 static void py_GUID_patch(PyTypeObject *type)
118 type->tp_init = py_GUID_init;
119 type->tp_str = py_GUID_str;
120 type->tp_repr = py_GUID_repr;
121 #if PY_MAJOR_VERSION >= 3
122 type->tp_richcompare = py_GUID_richcmp;
123 #else
124 type->tp_compare = py_GUID_cmp;
125 #endif
128 #define PY_GUID_PATCH py_GUID_patch
130 static int py_policy_handle_init(PyObject *self, PyObject *args, PyObject *kwargs)
132 char *str = NULL;
133 NTSTATUS status;
134 struct policy_handle *handle = pytalloc_get_ptr(self);
135 const char *kwnames[] = { "uuid", "type", NULL };
137 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", discard_const_p(char *, kwnames), &str, &handle->handle_type))
138 return -1;
140 if (str != NULL) {
141 status = GUID_from_string(str, &handle->uuid);
142 if (!NT_STATUS_IS_OK(status)) {
143 PyErr_SetNTSTATUS(status);
144 return -1;
148 return 0;
151 static PyObject *py_policy_handle_repr(PyObject *py_self)
153 struct policy_handle *self = pytalloc_get_ptr(py_self);
154 char *uuid_str = GUID_string(NULL, &self->uuid);
155 PyObject *ret = PyStr_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str);
156 talloc_free(uuid_str);
157 return ret;
160 static PyObject *py_policy_handle_str(PyObject *py_self)
162 struct policy_handle *self = pytalloc_get_ptr(py_self);
163 char *uuid_str = GUID_string(NULL, &self->uuid);
164 PyObject *ret = PyStr_FromFormat("%d, %s", self->handle_type, uuid_str);
165 talloc_free(uuid_str);
166 return ret;
169 static void py_policy_handle_patch(PyTypeObject *type)
171 type->tp_init = py_policy_handle_init;
172 type->tp_repr = py_policy_handle_repr;
173 type->tp_str = py_policy_handle_str;
176 #define PY_POLICY_HANDLE_PATCH py_policy_handle_patch