File: stgdict.c

package info (click to toggle)
python2.5 2.5-5%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 52,452 kB
  • ctags: 96,908
  • sloc: ansic: 352,790; python: 317,047; sh: 17,465; asm: 6,564; makefile: 4,300; lisp: 3,678; perl: 3,674; xml: 894; objc: 756; sed: 2
file content (484 lines) | stat: -rw-r--r-- 12,811 bytes parent folder | download | duplicates (2)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*****************************************************************
  This file should be kept compatible with Python 2.3, see PEP 291.
 *****************************************************************/

#include "Python.h"
#include <ffi.h>
#ifdef MS_WIN32
#include <windows.h>
#endif
#include "ctypes.h"

/******************************************************************/
/*
  StdDict - a dictionary subclass, containing additional C accessible fields

  XXX blabla more
*/

/* Seems we need this, otherwise we get problems when calling
 * PyDict_SetItem() (ma_lookup is NULL)
 */
static int
StgDict_init(StgDictObject *self, PyObject *args, PyObject *kwds)
{
	if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
		return -1;
	return 0;
}

static int
StgDict_clear(StgDictObject *self)
{
	Py_CLEAR(self->proto);
	Py_CLEAR(self->argtypes);
	Py_CLEAR(self->converters);
	Py_CLEAR(self->restype);
	Py_CLEAR(self->checker);
	return 0;
}

static void
StgDict_dealloc(StgDictObject *self)
{
	StgDict_clear(self);
	PyMem_Free(self->ffi_type_pointer.elements);
	PyDict_Type.tp_dealloc((PyObject *)self);
}

int
StgDict_clone(StgDictObject *dst, StgDictObject *src)
{
	char *d, *s;
	int size;

	StgDict_clear(dst);
	PyMem_Free(dst->ffi_type_pointer.elements);
	dst->ffi_type_pointer.elements = NULL;

	d = (char *)dst;
	s = (char *)src;
	memcpy(d + sizeof(PyDictObject),
	       s + sizeof(PyDictObject),
	       sizeof(StgDictObject) - sizeof(PyDictObject));

	Py_XINCREF(dst->proto);
	Py_XINCREF(dst->argtypes);
	Py_XINCREF(dst->converters);
	Py_XINCREF(dst->restype);
	Py_XINCREF(dst->checker);

	if (src->ffi_type_pointer.elements == NULL)
		return 0;
	size = sizeof(ffi_type *) * (src->length + 1);
	dst->ffi_type_pointer.elements = PyMem_Malloc(size);
	if (dst->ffi_type_pointer.elements == NULL)
		return -1;
	memcpy(dst->ffi_type_pointer.elements,
	       src->ffi_type_pointer.elements,
	       size);
	return 0;
}

PyTypeObject StgDict_Type = {
	PyObject_HEAD_INIT(NULL)
	0,
	"StgDict",
	sizeof(StgDictObject),
	0,
	(destructor)StgDict_dealloc,		/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
	0,					/* tp_repr */
	0,					/* tp_as_number */
	0,					/* tp_as_sequence */
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
	0,					/* tp_getattro */
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
	0,					/* tp_doc */
	0,					/* tp_traverse */
	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	0,					/* tp_methods */
	0,					/* tp_members */
	0,					/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	(initproc)StgDict_init,			/* tp_init */
	0,					/* tp_alloc */
	0,					/* tp_new */
	0,					/* tp_free */
};

/* May return NULL, but does not set an exception! */
StgDictObject *
PyType_stgdict(PyObject *obj)
{
	PyTypeObject *type;

	if (!PyType_Check(obj))
		return NULL;
	type = (PyTypeObject *)obj;
	if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS))
		return NULL;
	if (!type->tp_dict || !StgDict_CheckExact(type->tp_dict))
		return NULL;
	return (StgDictObject *)type->tp_dict;
}

/* May return NULL, but does not set an exception! */
/*
  This function should be as fast as possible, so we don't call PyType_stgdict
  above but inline the code, and avoid the PyType_Check().
*/
StgDictObject *
PyObject_stgdict(PyObject *self)
{
	PyTypeObject *type = self->ob_type;
	if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS))
		return NULL;
	if (!type->tp_dict || !StgDict_CheckExact(type->tp_dict))
		return NULL;
	return (StgDictObject *)type->tp_dict;
}

/* descr is the descriptor for a field marked as anonymous.  Get all the
 _fields_ descriptors from descr->proto, create new descriptors with offset
 and index adjusted, and stuff them into type.
 */
static int
MakeFields(PyObject *type, CFieldObject *descr,
	   Py_ssize_t index, Py_ssize_t offset)
{
	Py_ssize_t i;
	PyObject *fields;
	PyObject *fieldlist;

	fields = PyObject_GetAttrString(descr->proto, "_fields_");
	if (fields == NULL)
		return -1;
	fieldlist = PySequence_Fast(fields, "_fields_ must be a sequence");
	Py_DECREF(fields);
	if (fieldlist == NULL)
		return -1;

	for (i = 0; i < PySequence_Fast_GET_SIZE(fieldlist); ++i) {
		PyObject *pair = PySequence_Fast_GET_ITEM(fieldlist, i); /* borrowed */
		PyObject *fname, *ftype, *bits;
		CFieldObject *fdescr;
		CFieldObject *new_descr;
		/* Convert to PyArg_UnpackTuple... */
		if (!PyArg_ParseTuple(pair, "OO|O", &fname, &ftype, &bits)) {
			Py_DECREF(fieldlist);
			return -1;
		}
		fdescr = (CFieldObject *)PyObject_GetAttr(descr->proto, fname);
		if (fdescr == NULL) {
			Py_DECREF(fieldlist);
			return -1;
		}
		if (fdescr->ob_type != &CField_Type) {
			PyErr_SetString(PyExc_TypeError, "unexpected type");
			Py_DECREF(fdescr);
			Py_DECREF(fieldlist);
			return -1;
		}
		if (fdescr->anonymous) {
			int rc = MakeFields(type, fdescr,
					    index + fdescr->index,
					    offset + fdescr->offset);
			Py_DECREF(fdescr);
			if (rc == -1) {
				Py_DECREF(fieldlist);
				return -1;
			}
			continue;
		}
 		new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&CField_Type, NULL);
		if (new_descr == NULL) {
			Py_DECREF(fdescr);
			Py_DECREF(fieldlist);
			return -1;
		}
		assert(new_descr->ob_type == &CField_Type);
 		new_descr->size = fdescr->size;
 		new_descr->offset = fdescr->offset + offset;
 		new_descr->index = fdescr->index + index;
 		new_descr->proto = fdescr->proto;
 		Py_XINCREF(new_descr->proto);
 		new_descr->getfunc = fdescr->getfunc;
 		new_descr->setfunc = fdescr->setfunc;

  		Py_DECREF(fdescr);
		
		if (-1 == PyObject_SetAttr(type, fname, (PyObject *)new_descr)) {
			Py_DECREF(fieldlist);
			Py_DECREF(new_descr);
			return -1;
		}
		Py_DECREF(new_descr);
	}
	Py_DECREF(fieldlist);
	return 0;
}

/* Iterate over the names in the type's _anonymous_ attribute, if present,
 */
static int
MakeAnonFields(PyObject *type)
{
	PyObject *anon;
	PyObject *anon_names;
	Py_ssize_t i;

	anon = PyObject_GetAttrString(type, "_anonymous_");
	if (anon == NULL) {
		PyErr_Clear();
		return 0;
	}
	anon_names = PySequence_Fast(anon, "_anonymous_ must be a sequence");
	Py_DECREF(anon);
	if (anon_names == NULL)
		return -1;

	for (i = 0; i < PySequence_Fast_GET_SIZE(anon_names); ++i) {
		PyObject *fname = PySequence_Fast_GET_ITEM(anon_names, i); /* borrowed */
		CFieldObject *descr = (CFieldObject *)PyObject_GetAttr(type, fname);
		if (descr == NULL) {
			Py_DECREF(anon_names);
			return -1;
		}
		assert(descr->ob_type == &CField_Type);
		descr->anonymous = 1;

		/* descr is in the field descriptor. */
		if (-1 == MakeFields(type, (CFieldObject *)descr,
				     ((CFieldObject *)descr)->index,
				     ((CFieldObject *)descr)->offset)) {
			Py_DECREF(descr);
			Py_DECREF(anon_names);
			return -1;
		}
		Py_DECREF(descr);
	}

	Py_DECREF(anon_names);
	return 0;
}

/*
  Retrive the (optional) _pack_ attribute from a type, the _fields_ attribute,
  and create an StgDictObject.  Used for Structure and Union subclasses.
*/
int
StructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct)
{
	StgDictObject *stgdict, *basedict;
	int len, offset, size, align, i;
	int union_size, total_align;
	int field_size = 0;
	int bitofs;
	PyObject *isPacked;
	int pack = 0;
	int ffi_ofs;
	int big_endian;

	/* HACK Alert: I cannot be bothered to fix ctypes.com, so there has to
	   be a way to use the old, broken sematics: _fields_ are not extended
	   but replaced in subclasses.
	   
	   XXX Remove this in ctypes 1.0!
	*/
	int use_broken_old_ctypes_semantics;

	if (fields == NULL)
		return 0;

#ifdef WORDS_BIGENDIAN
	big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 0 : 1;
#else
	big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 1 : 0;
#endif

	use_broken_old_ctypes_semantics = \
		PyObject_HasAttrString(type, "_use_broken_old_ctypes_structure_semantics_");

	isPacked = PyObject_GetAttrString(type, "_pack_");
	if (isPacked) {
		pack = PyInt_AsLong(isPacked);
		if (pack < 0 || PyErr_Occurred()) {
			Py_XDECREF(isPacked);
			PyErr_SetString(PyExc_ValueError,
					"_pack_ must be a non-negative integer");
			return -1;
		}
		Py_DECREF(isPacked);
	} else
		PyErr_Clear();

	len = PySequence_Length(fields);
	if (len == -1) {
		PyErr_SetString(PyExc_TypeError,
				"'_fields_' must be a sequence of pairs");
		return -1;
	}

	stgdict = PyType_stgdict(type);
	if (!stgdict)
		return -1;
	if (stgdict->flags & DICTFLAG_FINAL) {/* is final ? */
		PyErr_SetString(PyExc_AttributeError,
				"_fields_ is final");
		return -1;
	}
	/* XXX This should probably be moved to a point when all this
	   stuff is sucessfully finished. */
	stgdict->flags |= DICTFLAG_FINAL;	/* set final */

	if (stgdict->ffi_type_pointer.elements)
		PyMem_Free(stgdict->ffi_type_pointer.elements);

	basedict = PyType_stgdict((PyObject *)((PyTypeObject *)type)->tp_base);
	if (basedict && !use_broken_old_ctypes_semantics) {
		size = offset = basedict->size;
		align = basedict->align;
		union_size = 0;
		total_align = align ? align : 1;
		stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
		stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (basedict->length + len + 1));
		memset(stgdict->ffi_type_pointer.elements, 0,
		       sizeof(ffi_type *) * (basedict->length + len + 1));
		memcpy(stgdict->ffi_type_pointer.elements,
		       basedict->ffi_type_pointer.elements,
		       sizeof(ffi_type *) * (basedict->length));
		ffi_ofs = basedict->length;
	} else {
		offset = 0;
		size = 0;
		align = 0;
		union_size = 0;
		total_align = 1;
		stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
		stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (len + 1));
		memset(stgdict->ffi_type_pointer.elements, 0,
		       sizeof(ffi_type *) * (len + 1));
		ffi_ofs = 0;
	}

#define realdict ((PyObject *)&stgdict->dict)
	for (i = 0; i < len; ++i) {
		PyObject *name = NULL, *desc = NULL;
		PyObject *pair = PySequence_GetItem(fields, i);
		PyObject *prop;
		StgDictObject *dict;
		int bitsize = 0;

		if (!pair || !PyArg_ParseTuple(pair, "OO|i", &name, &desc, &bitsize)) {
			PyErr_SetString(PyExc_AttributeError,
					"'_fields_' must be a sequence of pairs");
			Py_XDECREF(pair);
			return -1;
		}
		dict = PyType_stgdict(desc);
		if (dict == NULL) {
			Py_DECREF(pair);
			PyErr_Format(PyExc_TypeError,
				     "second item in _fields_ tuple (index %d) must be a C type",
				     i);
			return -1;
		}
		stgdict->ffi_type_pointer.elements[ffi_ofs + i] = &dict->ffi_type_pointer;
		dict->flags |= DICTFLAG_FINAL; /* mark field type final */
		if (PyTuple_Size(pair) == 3) { /* bits specified */
			switch(dict->ffi_type_pointer.type) {
			case FFI_TYPE_UINT8:
			case FFI_TYPE_UINT16:
			case FFI_TYPE_UINT32:
			case FFI_TYPE_SINT64:
			case FFI_TYPE_UINT64:
				break;

			case FFI_TYPE_SINT8:
			case FFI_TYPE_SINT16:
			case FFI_TYPE_SINT32:
				if (dict->getfunc != getentry("c")->getfunc
#ifdef CTYPES_UNICODE
				    && dict->getfunc != getentry("u")->getfunc
#endif
					)
					break;
				/* else fall through */
			default:
				PyErr_Format(PyExc_TypeError,
					     "bit fields not allowed for type %s",
					     ((PyTypeObject *)desc)->tp_name);
				Py_DECREF(pair);
				return -1;
			}
			if (bitsize <= 0 || bitsize > dict->size * 8) {
				PyErr_SetString(PyExc_ValueError,
						"number of bits invalid for bit field");
				Py_DECREF(pair);
				return -1;
			}
		} else
			bitsize = 0;
		if (isStruct) {
			prop = CField_FromDesc(desc, i,
					       &field_size, bitsize, &bitofs,
					       &size, &offset, &align,
					       pack, big_endian);
		} else /* union */ {
			size = 0;
			offset = 0;
			align = 0;
			prop = CField_FromDesc(desc, i,
					       &field_size, bitsize, &bitofs,
					       &size, &offset, &align,
					       pack, big_endian);
			union_size = max(size, union_size);
		}
		total_align = max(align, total_align);

		if (!prop) {
			Py_DECREF(pair);
			Py_DECREF((PyObject *)stgdict);
			return -1;
		}
		if (-1 == PyDict_SetItem(realdict, name, prop)) {
			Py_DECREF(prop);
			Py_DECREF(pair);
			Py_DECREF((PyObject *)stgdict);
			return -1;
		}
		Py_DECREF(pair);
		Py_DECREF(prop);
	}
#undef realdict
	if (!isStruct)
		size = union_size;

	/* Adjust the size according to the alignment requirements */
	size = ((size + total_align - 1) / total_align) * total_align;

	stgdict->ffi_type_pointer.alignment = total_align;
	stgdict->ffi_type_pointer.size = size;

	stgdict->size = size;
	stgdict->align = total_align;
	stgdict->length = len;	/* ADD ffi_ofs? */
	return MakeAnonFields(type);
}