summaryrefslogtreecommitdiffstats
path: root/pytalloc_util.c
blob: 766938a82c83350e7eed13b4415f28a132b3ca6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* 
   Unix SMB/CIFS implementation.
   Python/Talloc glue
   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
   
     ** NOTE! The following LGPL license applies to the talloc
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "lib/replace/system/python.h"
#include "replace.h"
#include <talloc.h>
#include "pytalloc.h"
#include <assert.h>
#include "pytalloc_private.h"

static PyObject *pytalloc_steal_or_reference(PyTypeObject *py_type,
					 TALLOC_CTX *mem_ctx, void *ptr, bool steal);

_PUBLIC_ PyTypeObject *pytalloc_GetObjectType(void)
{
	static PyTypeObject *type = NULL;
	PyObject *mod;

	mod = PyImport_ImportModule("talloc");
	if (mod == NULL) {
		return NULL;
	}

	type = (PyTypeObject *)PyObject_GetAttrString(mod, "Object");
	Py_DECREF(mod);

	return type;
}

_PUBLIC_ PyTypeObject *pytalloc_GetBaseObjectType(void)
{
	static PyTypeObject *type = NULL;
	PyObject *mod;

	mod = PyImport_ImportModule("talloc");
	if (mod == NULL) {
		return NULL;
	}

	type = (PyTypeObject *)PyObject_GetAttrString(mod, "BaseObject");
	Py_DECREF(mod);

	return type;
}

static PyTypeObject *pytalloc_GetGenericObjectType(void)
{
	static PyTypeObject *type = NULL;
	PyObject *mod;

	mod = PyImport_ImportModule("talloc");
	if (mod == NULL) {
		return NULL;
	}

	type = (PyTypeObject *)PyObject_GetAttrString(mod, "GenericObject");
	Py_DECREF(mod);

	return type;
}

/**
 * Import an existing talloc pointer into a Python object.
 */
_PUBLIC_ PyObject *pytalloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx,
				     void *ptr)
{
	return pytalloc_steal_or_reference(py_type, mem_ctx, ptr, true);
}

/**
 * Import an existing talloc pointer into a Python object.
 */
_PUBLIC_ PyObject *pytalloc_steal(PyTypeObject *py_type, void *ptr)
{
	return pytalloc_steal_or_reference(py_type, ptr, ptr, true);
}


/**
 * Import an existing talloc pointer into a Python object, leaving the
 * original parent, and creating a reference to the object in the python
 * object.
 *
 * We remember the object we hold the reference to (a
 * possibly-non-talloc pointer), the existing parent (typically the
 * start of the array) and the new referenced parent.  That way we can
 * cope with the fact that we will have multiple parents, one per time
 * python sees the object.
 */
_PUBLIC_ PyObject *pytalloc_reference_ex(PyTypeObject *py_type,
					 TALLOC_CTX *mem_ctx, void *ptr)
{
	return pytalloc_steal_or_reference(py_type, mem_ctx, ptr, false);
}


/**
 * Internal function that either steals or references the talloc
 * pointer into a new talloc context.
 */
static PyObject *pytalloc_steal_or_reference(PyTypeObject *py_type,
					 TALLOC_CTX *mem_ctx, void *ptr, bool steal)
{
	bool ok = false;
	TALLOC_CTX *talloc_ctx = NULL;
	bool is_baseobject = false;
	PyObject *obj = NULL;
	PyTypeObject *BaseObjectType = NULL, *ObjectType = NULL;

	BaseObjectType = pytalloc_GetBaseObjectType();
	if (BaseObjectType == NULL) {
		goto err;
	}
	ObjectType = pytalloc_GetObjectType();
	if (ObjectType == NULL) {
		goto err;
	}

	/* this should have been tested by caller */
	if (mem_ctx == NULL) {
		return PyErr_NoMemory();
	}

	is_baseobject = PyType_IsSubtype(py_type, BaseObjectType);
	if (!is_baseobject) {
		if (!PyType_IsSubtype(py_type, ObjectType)) {
			PyErr_SetString(PyExc_TypeError,
				"Expected type based on talloc");
			return NULL;
		}
	}

	obj = py_type->tp_alloc(py_type, 0);
	if (obj == NULL) {
		goto err;
	}

	talloc_ctx = talloc_new(NULL);
	if (talloc_ctx == NULL) {
		PyErr_NoMemory();
		goto err;
	}

	if (steal) {
		ok = (talloc_steal(talloc_ctx, mem_ctx) != NULL);
	} else {
		ok = (talloc_reference(talloc_ctx, mem_ctx) != NULL);
	}
	if (!ok) {
		goto err;
	}
	talloc_set_name_const(talloc_ctx, py_type->tp_name);

	if (is_baseobject) {
		pytalloc_BaseObject *ret = (pytalloc_BaseObject*)obj;
		ret->talloc_ctx = talloc_ctx;
		ret->talloc_ptr_ctx = mem_ctx;
		ret->ptr = ptr;
	} else {
		pytalloc_Object *ret = (pytalloc_Object*)obj;
		ret->talloc_ctx = talloc_ctx;
		ret->ptr = ptr;
	}
	return obj;

err:
	TALLOC_FREE(talloc_ctx);
	Py_XDECREF(obj);
	return NULL;
}

/*
 * Wrap a generic talloc pointer into a talloc.GenericObject,
 * this is a subclass of talloc.BaseObject.
 */
_PUBLIC_ PyObject *pytalloc_GenericObject_steal_ex(TALLOC_CTX *mem_ctx, void *ptr)
{
	PyTypeObject *tp = pytalloc_GetGenericObjectType();
	return pytalloc_steal_ex(tp, mem_ctx, ptr);
}

/*
 * Wrap a generic talloc pointer into a talloc.GenericObject,
 * this is a subclass of talloc.BaseObject.
 */
_PUBLIC_ PyObject *pytalloc_GenericObject_reference_ex(TALLOC_CTX *mem_ctx, void *ptr)
{
	PyTypeObject *tp = pytalloc_GetGenericObjectType();
	return pytalloc_reference_ex(tp, mem_ctx, ptr);
}

_PUBLIC_ int pytalloc_Check(PyObject *obj)
{
	PyTypeObject *tp = pytalloc_GetObjectType();

	return PyObject_TypeCheck(obj, tp);
}

_PUBLIC_ int pytalloc_BaseObject_check(PyObject *obj)
{
	PyTypeObject *tp = pytalloc_GetBaseObjectType();

	return PyObject_TypeCheck(obj, tp);
}

_PUBLIC_ size_t pytalloc_BaseObject_size(void)
{
	return sizeof(pytalloc_BaseObject);
}

static void *_pytalloc_get_checked_type(PyObject *py_obj, const char *type_name,
					bool check_only, const char *function)
{
	TALLOC_CTX *mem_ctx;
	void *ptr = NULL;
	void *type_obj;

	mem_ctx = _pytalloc_get_mem_ctx(py_obj);
	ptr = _pytalloc_get_ptr(py_obj);

	if (mem_ctx != ptr || ptr == NULL) {
		if (check_only) {
			return NULL;
		}

		PyErr_Format(PyExc_TypeError, "%s: expected %s, "
			     "but the pointer is no talloc pointer, "
			     "pytalloc_get_ptr() would get the raw pointer.",
			     function, type_name);
		return NULL;
	}

	type_obj = talloc_check_name(ptr, type_name);
	if (type_obj == NULL) {
		const char *name = NULL;

		if (check_only) {
			return NULL;
		}

		name = talloc_get_name(ptr);
		PyErr_Format(PyExc_TypeError, "%s: expected %s, got %s",
			     function, type_name, name);
		return NULL;
	}

	return ptr;
}

_PUBLIC_ int _pytalloc_check_type(PyObject *py_obj, const char *type_name)
{
	void *ptr = NULL;

	ptr = _pytalloc_get_checked_type(py_obj, type_name,
					 true, /* check_only */
					 "pytalloc_check_type");
	if (ptr == NULL) {
		return 0;
	}

	return 1;
}

_PUBLIC_ void *_pytalloc_get_type(PyObject *py_obj, const char *type_name)
{
	return _pytalloc_get_checked_type(py_obj, type_name,
					  false, /* not check_only */
					  "pytalloc_get_type");
}

_PUBLIC_ void *_pytalloc_get_ptr(PyObject *py_obj)
{
	if (pytalloc_BaseObject_check(py_obj)) {
		return ((pytalloc_BaseObject *)py_obj)->ptr;
	}
	if (pytalloc_Check(py_obj)) {
		return ((pytalloc_Object *)py_obj)->ptr;
	}
	return NULL;
}

_PUBLIC_ TALLOC_CTX *_pytalloc_get_mem_ctx(PyObject *py_obj)
{
	if (pytalloc_BaseObject_check(py_obj)) {
		return ((pytalloc_BaseObject *)py_obj)->talloc_ptr_ctx;
	}
	if (pytalloc_Check(py_obj)) {
		return ((pytalloc_Object *)py_obj)->talloc_ctx;
	}
	return NULL;
}

_PUBLIC_ int pytalloc_BaseObject_PyType_Ready(PyTypeObject *type)
{
	PyTypeObject *talloc_type = pytalloc_GetBaseObjectType();
	if (talloc_type == NULL) {
		return -1;
	}

	type->tp_base = talloc_type;
	type->tp_basicsize = pytalloc_BaseObject_size();

	return PyType_Ready(type);
}

_PUBLIC_ const char *_pytalloc_get_name(PyObject *obj)
{
	void *ptr = pytalloc_get_ptr(obj);
	if (ptr == NULL) {
		return "non-talloc object";
	}
	return talloc_get_name(ptr);
}