Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Samba utility functions
4 :
5 : Copyright (C) Catalyst IT 2017
6 :
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.
11 :
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.
16 :
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/>.
19 : */
20 : #include <Python.h>
21 : #include "librpc/gen_ndr/lsa.h"
22 :
23 1244 : static PyObject *py_lsa_String_str(PyObject *py_self)
24 : {
25 1244 : struct lsa_String *self = pytalloc_get_ptr(py_self);
26 1244 : PyObject *ret = NULL;
27 1244 : if (self->string == NULL) {
28 0 : const char *empty = "";
29 0 : ret = PyUnicode_FromString(empty);
30 : } else {
31 1244 : ret = PyUnicode_FromString(self->string);
32 : }
33 1244 : return ret;
34 : }
35 :
36 0 : static PyObject *py_lsa_String_repr(PyObject *py_self)
37 : {
38 0 : struct lsa_String *self = pytalloc_get_ptr(py_self);
39 0 : PyObject *ret = NULL;
40 0 : if (self->string == NULL) {
41 0 : const char *empty = "lsaString(None)";
42 0 : ret = PyUnicode_FromString(empty);
43 : } else {
44 0 : ret = PyUnicode_FromFormat("lsaString('%s')", self->string);
45 : }
46 0 : return ret;
47 : }
48 :
49 329 : static int py_lsa_String_init(PyObject *self, PyObject *args, PyObject *kwargs)
50 : {
51 329 : struct lsa_String *string = pytalloc_get_ptr(self);
52 329 : const char *str = NULL;
53 329 : const char *kwnames[] = { "str", NULL };
54 :
55 329 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
56 0 : return -1;
57 :
58 329 : string->string = talloc_strdup(string, str);
59 :
60 329 : if (str != NULL && string->string == NULL) {
61 0 : PyErr_NoMemory();
62 0 : return -1;
63 : }
64 :
65 329 : return 0;
66 : }
67 :
68 :
69 3514 : static void py_lsa_String_patch(PyTypeObject *type)
70 : {
71 3514 : type->tp_init = py_lsa_String_init;
72 3514 : type->tp_str = py_lsa_String_str;
73 3514 : type->tp_repr = py_lsa_String_repr;
74 3514 : }
75 :
76 : #define PY_STRING_PATCH py_lsa_String_patch
77 :
|