a59dcbf970ccecb50d0adef4894897f14829b843
[samba.git] / source4 / librpc / ndr / py_lsa.c
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 static PyObject *py_lsa_String_str(PyObject *py_self)
24 {
25         struct lsa_String *self = pytalloc_get_ptr(py_self);
26         PyObject *ret = NULL;
27         if (self->string == NULL) {
28                 const char *empty = "";
29                 ret = PyUnicode_FromString(empty);
30         } else {
31                 ret = PyUnicode_FromString(self->string);
32         }
33         return ret;
34 }
35
36 static PyObject *py_lsa_String_repr(PyObject *py_self)
37 {
38         struct lsa_String *self = pytalloc_get_ptr(py_self);
39         PyObject *ret = NULL;
40         if (self->string == NULL) {
41                 const char *empty = "lsaString(None)";
42                 ret = PyUnicode_FromString(empty);
43         } else {
44                 ret = PyUnicode_FromFormat("lsaString('%s')", self->string);
45         }
46         return ret;
47 }
48
49 static int py_lsa_String_init(PyObject *self, PyObject *args, PyObject *kwargs)
50 {
51         struct lsa_String *string = pytalloc_get_ptr(self);
52         const char *str = NULL;
53         const char *kwnames[] = { "str", NULL };
54
55         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
56                 return -1;
57
58         string->string = talloc_strdup(string, str);
59
60         if (str != NULL && string->string == NULL) {
61                 PyErr_NoMemory();
62                 return -1;
63         }
64
65         return 0;
66 }
67
68
69 static void py_lsa_String_patch(PyTypeObject *type)
70 {
71         type->tp_init = py_lsa_String_init;
72         type->tp_str = py_lsa_String_str;
73         type->tp_repr = py_lsa_String_repr;
74 }
75
76 #define PY_STRING_PATCH py_lsa_String_patch
77