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 "lib/replace/system/python.h"
21 #include "python/py3compat.h"
22 #include "librpc/gen_ndr/misc.h"
24 static PyObject
*py_GUID_richcmp(PyObject
*py_self
, PyObject
*py_other
, int op
)
27 struct GUID
*self
= pytalloc_get_ptr(py_self
), *other
;
28 other
= pytalloc_get_ptr(py_other
);
30 Py_INCREF(Py_NotImplemented
);
31 return Py_NotImplemented
;
34 ret
= GUID_compare(self
, other
);
37 case Py_EQ
: if (ret
== 0) Py_RETURN_TRUE
; else Py_RETURN_FALSE
;
38 case Py_NE
: if (ret
!= 0) Py_RETURN_TRUE
; else Py_RETURN_FALSE
;
39 case Py_LT
: if (ret
< 0) Py_RETURN_TRUE
; else Py_RETURN_FALSE
;
40 case Py_GT
: if (ret
> 0) Py_RETURN_TRUE
; else Py_RETURN_FALSE
;
41 case Py_LE
: if (ret
<= 0) Py_RETURN_TRUE
; else Py_RETURN_FALSE
;
42 case Py_GE
: if (ret
>= 0) Py_RETURN_TRUE
; else Py_RETURN_FALSE
;
44 Py_INCREF(Py_NotImplemented
);
45 return Py_NotImplemented
;
48 static PyObject
*py_GUID_str(PyObject
*py_self
)
50 struct GUID
*self
= pytalloc_get_ptr(py_self
);
51 struct GUID_txt_buf buf
;
52 PyObject
*ret
= PyUnicode_FromString(GUID_buf_string(self
, &buf
));
56 static PyObject
*py_GUID_repr(PyObject
*py_self
)
58 struct GUID
*self
= pytalloc_get_ptr(py_self
);
59 struct GUID_txt_buf buf
;
60 PyObject
*ret
= PyUnicode_FromFormat(
61 "GUID('%s')", GUID_buf_string(self
, &buf
));
65 static int py_GUID_init(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
69 struct GUID
*guid
= pytalloc_get_ptr(self
);
70 const char *kwnames
[] = { "str", NULL
};
72 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O", discard_const_p(char *, kwnames
), &str
))
79 if (PyUnicode_Check(str
)) {
81 discard_const_p(uint8_t,
82 PyUnicode_AsUTF8AndSize(str
, &_size
));
83 } else if (PyBytes_Check(str
)) {
85 discard_const_p(uint8_t,
86 PyBytes_AsString(str
));
87 _size
= PyBytes_Size(str
);
89 PyErr_SetString(PyExc_TypeError
,
90 "Expected a string or bytes argument to GUID()");
93 guid_val
.length
= _size
;
94 status
= GUID_from_data_blob(&guid_val
, guid
);
95 if (!NT_STATUS_IS_OK(status
)) {
96 PyErr_SetNTSTATUS(status
);
104 static void py_GUID_patch(PyTypeObject
*type
)
106 type
->tp_init
= py_GUID_init
;
107 type
->tp_str
= py_GUID_str
;
108 type
->tp_repr
= py_GUID_repr
;
109 type
->tp_richcompare
= py_GUID_richcmp
;
112 #define PY_GUID_PATCH py_GUID_patch
114 static int py_policy_handle_init(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
118 struct policy_handle
*handle
= pytalloc_get_ptr(self
);
119 const char *kwnames
[] = { "uuid", "type", NULL
};
121 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|sI", discard_const_p(char *, kwnames
), &str
, &handle
->handle_type
))
125 status
= GUID_from_string(str
, &handle
->uuid
);
126 if (!NT_STATUS_IS_OK(status
)) {
127 PyErr_SetNTSTATUS(status
);
135 static PyObject
*py_policy_handle_repr(PyObject
*py_self
)
137 struct policy_handle
*self
= pytalloc_get_ptr(py_self
);
138 struct GUID_txt_buf buf
;
139 PyObject
*ret
= PyUnicode_FromFormat(
140 "policy_handle(%d, '%s')",
142 GUID_buf_string(&self
->uuid
, &buf
));
146 static PyObject
*py_policy_handle_str(PyObject
*py_self
)
148 struct policy_handle
*self
= pytalloc_get_ptr(py_self
);
149 struct GUID_txt_buf buf
;
150 PyObject
*ret
= PyUnicode_FromFormat(
153 GUID_buf_string(&self
->uuid
, &buf
));
157 static void py_policy_handle_patch(PyTypeObject
*type
)
159 type
->tp_init
= py_policy_handle_init
;
160 type
->tp_repr
= py_policy_handle_repr
;
161 type
->tp_str
= py_policy_handle_str
;
164 #define PY_POLICY_HANDLE_PATCH py_policy_handle_patch